| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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<MainNewsPage> createState() => _MainNewsPageState();
- }
- final newsCategoryProvider =
- NotifierProvider<NewsCategoryProvider, List<NewsCategoryModel>>(() {
- return NewsCategoryProvider();
- });
- final tabIndexProvider = StateProvider<int>((ref) => 0);
- final List<Color> _startColorList = [
- color343090,
- color5F59F7,
- color6592FD,
- color8C61FF,
- color44C2FD,
- color5F59F7,
- ];
- final List<Color> _middleColorList = [
- color343090,
- color5F59F7,
- color6592FD,
- color8C61FF,
- color44C2FD,
- color6379FB,
- ];
- class _MainNewsPageState extends ConsumerState<MainNewsPage>
- 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<Widget> getListPage(List<NewsCategoryModel> items) {
- List<Widget> list = [];
- items.asMap().entries.forEach((element) {
- list.add(NewsChildMainPage(tabIndex: element.key));
- });
- return list;
- }
- @override
- bool get wantKeepAlive => true;
- }
|