import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:news_app/ui/news/news_child_main_page.dart'; import 'package:news_app/ui/search/search_bar_widget.dart'; import '../../constant/color_res.dart'; import '../../gen/assets.gen.dart'; import '../../model/news_category_model.dart'; import '../../provider/news_category_provider.dart'; /// @author: bo.zeng /// @email: cnhbwds@gmail.com /// @date: 2025 2025/4/9 16:00 /// @description: class MainNewsPage extends ConsumerStatefulWidget { const MainNewsPage({super.key}); @override ConsumerState createState() => _MainNewsPageState(); } final newsCategoryProvider = NotifierProvider>(() { return NewsCategoryProvider(); }); final tabIndexProvider = StateProvider((ref) => 0); final List _startColorList = [ color343090, color5F59F7, color6592FD, color8C61FF, color44C2FD, color5F59F7, ]; final List _middleColorList = [ color343090, color5F59F7, color6592FD, color8C61FF, color44C2FD, color6379FB, ]; class _MainNewsPageState extends ConsumerState with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin { TabController? _tabController; @override void initState() { super.initState(); ref.read(newsCategoryProvider.notifier).fetchNewsCategoryList(); } @override void dispose() { super.dispose(); _tabController?.dispose(); } @override Widget build(BuildContext context) { super.build(context); final result = ref.watch(newsCategoryProvider); var categoryList = result; if (result.length > 6) { categoryList = result.take(6).toList(); } final tabIndex = ref.watch(tabIndexProvider); if (categoryList.isEmpty) { return const SizedBox.shrink(); } // 初始化 tabController(只初始化一次) if (_tabController == null) { _tabController = TabController(length: categoryList.length, vsync: this); _tabController?.addListener(() { ref.read(tabIndexProvider.notifier).state = _tabController?.index ?? 0; }); } return Scaffold( appBar: AppBar( backgroundColor: _startColorList[tabIndex], systemOverlayStyle: SystemUiOverlayStyle( statusBarColor: _middleColorList[tabIndex], statusBarIconBrightness: Brightness.light, // 状态栏图标颜色 ), title: Row( spacing: 10.w, children: [ Image.asset(Assets.images.logo.path, width: 70.w), Expanded(child: SearchBarWidget(tabIndex: 0)), ], ), bottom: TabBar( controller: _tabController, tabAlignment: TabAlignment.center, isScrollable: true, indicatorColor: Colors.white, dividerHeight: 0, labelColor: Colors.white, // 选中标签颜色 unselectedLabelColor: Colors.white, // 未选中标签颜色 labelStyle: TextStyle( fontWeight: FontWeight.bold, // 选中标签加粗 fontSize: 16.0.sp, ), unselectedLabelStyle: TextStyle( fontWeight: FontWeight.normal, // 未选中标签不加粗 fontSize: 16.0.sp, ), tabs: categoryList.map((item) => Tab(text: item.name)).toList(), ), ), body: Stack( children: [ Positioned( child: Container( height: 100.h, decoration: BoxDecoration( gradient: LinearGradient( colors: [ _startColorList[tabIndex], _middleColorList[tabIndex], Colors.white, ], begin: Alignment.topCenter, end: Alignment.bottomCenter, ), ), ), ), TabBarView( controller: _tabController, children: getListPage(categoryList), ), ], ), ); } List getListPage(List items) { List list = []; items.asMap().entries.forEach((element) { list.add(NewsChildMainPage(tabIndex: element.key)); }); return list; } @override bool get wantKeepAlive => true; }