novyronst/lib/pages/first/widgets/home_first_view.dart

234 lines
7.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:novyronst/common/help/app_nav.dart';
import 'package:novyronst/common/help/help_time.dart';
import 'package:novyronst/common/model/ny_category_model.dart';
import 'package:novyronst/common/model/ny_drama_model.dart';
import 'package:novyronst/pages/first/pages/category_list_page.dart';
import 'package:novyronst/pages/first/player_detail/video_detail_page.dart';
import 'package:novyronst/pages/widgets/cut_corner_clipper.dart';
import 'package:novyronst/pages/widgets/ny_progress_widget.dart';
import 'package:novyronst/pages/widgets/ny_tag_widget.dart';
import 'package:novyronst/root/lib_export.dart';
import 'package:novyronst/tools/widgets/extend_widget.dart';
import 'package:novyronst/tools/widgets/novy_button_bg.dart';
import 'package:novyronst/tools/widgets/novy_com_widget.dart';
import 'package:novyronst/tools/widgets/novy_img_bg.dart';
import '../../../common/api/video_request.dart';
class HomeFirstView extends StatefulWidget {
const HomeFirstView({super.key, required this.categories});
final List<NyCategoryModel> categories;
@override
State<StatefulWidget> createState() {
return _HomeFirstViewState();
}
}
NyDramaModel? firstHistoryItem(List<NyDramaModel> items) {
if (items.isEmpty) return null;
return items.first;
}
double historyProgress(NyDramaModel item) {
final process = item.currentEpisode;
if (process == null) return 0;
return (process / item.episodeTotal).clamp(0, 1).toDouble();
}
String homeHistoryTag(NyDramaModel item) {
final category = item.category;
if (category != null && category.isNotEmpty) return category.first;
final categoryList = item.categoryList;
if (categoryList != null && categoryList.isNotEmpty) {
return categoryList.first.name;
}
return item.tagType;
}
class _HomeFirstViewState extends State<HomeFirstView> {
NyDramaModel? _historyItem;
@override
void initState() {
super.initState();
_getHistoryList();
}
void _getHistoryList() {
VideoRequest.getCollections().then((value) {
if (!mounted) return;
setState(() {
_historyItem = firstHistoryItem(value);
});
});
}
@override
Widget build(BuildContext context) {
const double space = 25;
final historyItem = _historyItem;
final hasHistory = historyItem != null;
final top = 151 - space;
final height = top + (hasHistory ? 204 + 16 : 0) + 45;
return Stack(
children: [
// 151
SizedBox(height: height),
HelpImage.asset(
ConstImage.homeFirst,
width: AppScreen.screenWidth,
fit: BoxFit.fill,
),
Positioned(
top: top - 20,
left: 16.w,
right: 16.w,
child: Column(
children: [
if (hasHistory) ...[
CutCornerClipper(
width: AppScreen.screenWidth - 32.w,
height: 204,
cutSize: 80.w,
child: _buildVideoItem(historyItem),
),
const SizedBox(height: 16),
],
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: List.generate(widget.categories.length, (index) {
final item = widget.categories[index];
return Container(
margin: const EdgeInsets.only(right: 8),
child: NovyImgBg(
imgBg: ConstImage.cateBg,
height: 52,
child: Stack(
children: [
NovyText(
item.name,
color: ConstColor.btGreen,
fontSize: 21,
textAlign: TextAlign.center,
fontFamily: ConstFont.montBlack,
shadow: Shadow(
offset: const Offset(1, 1), // X: 1, Y: 1
blurRadius: 0, // Blur: 0
color: Colors.black, // #000000
),
),
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(height: 2, color: Colors.black),
),
],
),
),
).addInkWell(
onTap: () {
AppNav.to(CategoryListPage(preCategory: item));
},
);
}),
),
),
],
),
),
],
);
}
Widget _buildVideoItem(NyDramaModel item) {
final tag = homeHistoryTag(item);
return Padding(
padding: const EdgeInsets.all(8),
child: Column(
children: [
Row(
children: [
NovyText(
'Current Viewing Progress',
color: Colors.black,
fontSize: 15.w,
fontFamily: ConstFont.montBold,
),
const SizedBox(width: 10),
HelpImage.asset(ConstImage.homeCurrentTag, height: 10.h),
],
),
const SizedBox(height: 10),
Row(
children: [
HelpImage.networkRadius(
item.imageUrl,
width: 60,
height: 90,
fit: BoxFit.cover,
borderRadius: BorderRadius.circular(2),
),
const SizedBox(width: 12),
Expanded(
child: SizedBox(
height: 90,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
NovyText(
item.name,
color: Colors.black,
fontSize: 13.w,
fontFamily: ConstFont.montMedium,
maxLines: 1,
),
const SizedBox(height: 4),
NovyText(
item.description,
color: Colors.grey,
fontSize: 10.w,
fontFamily: ConstFont.montRegular,
maxLines: 2,
),
const Spacer(),
Row(
children: [
if (tag.isNotEmpty) ...[
NyTagWidget(text: tag),
const SizedBox(width: 8),
],
NyHotWidget(
text: HelpTime.formatNum(item.watchTotal),
),
],
),
],
),
),
),
],
),
const SizedBox(height: 10),
NyProgressWidget(progress: historyProgress(item)),
const SizedBox(height: 6),
NovyButtonBg(text: 'Keep Watching', type: 2, width: 327.w).addInkWell(
onTap: () {
AppNav.to(VideoDetailPage(drama: item));
},
),
],
),
);
}
}