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:go_router/go_router.dart'; import 'package:news_app/constant/size_res.dart'; import 'package:news_app/main.dart'; import 'package:news_app/model/user_model.dart'; import 'package:news_app/widget/load_image.dart'; import 'package:news_app/widget/my_txt.dart'; import '../../constant/color_res.dart'; import '../../constant/config.dart'; import '../../gen/assets.gen.dart'; /// @author: bo.zeng /// @email: cnhbwds@gmail.com /// @date: 2025 2025/4/9 16:00 /// @description: class MainUserPage extends ConsumerStatefulWidget { const MainUserPage({super.key}); @override ConsumerState createState() => _MainUserPageState(); } class _MainUserPageState extends ConsumerState with AutomaticKeepAliveClientMixin { @override void initState() { super.initState(); //获取用户积分信息 ref.read(globalUserProvider.notifier).fetchUserInfo(); eventBus.on().listen((event){ if (event == "mainCall") { ref.read(globalUserProvider.notifier).fetchUserInfo(); } }); } @override Widget build(BuildContext context) { super.build(context); UserModel user = ref.watch(globalUserProvider); return Scaffold( backgroundColor: Colors.white, appBar: AppBar( // 移除阴影 scrolledUnderElevation: 0, // 禁用滚动时的阴影变化 title: myTxt(text: "我的", fontSize: 18.sp, fontWeight: FontWeight.bold), centerTitle: true, systemOverlayStyle: SystemUiOverlayStyle( statusBarColor: colorDCEAFE, statusBarIconBrightness: Brightness.dark, ), backgroundColor: colorDCEAFE, ), body: SingleChildScrollView( child: Stack( children: [ Container( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [colorDCEAFE, colorDCEAFE, colorFDFEFF], ), ), width: screenWidth, height: 500.h, ), Column( children: [ // 用户信息区域 _buildUserInfoSection(context, ref), // 我的积分、积分商城卡片 if (user.isShow == 1) Padding( padding: EdgeInsets.symmetric(horizontal: 5.w), child: Row( children: [ Expanded(child: _buildScoreCard()), Expanded(child: _buildScoreShopCard()), ], ), ), // 功能列表 _buildFunctionList(), _buildFunctionList2(), ], ), ], ), ), ); } // 构建用户信息区域 Widget _buildUserInfoSection(BuildContext context, WidgetRef ref) { UserModel user = ref.watch(globalUserProvider); return GestureDetector( onTap: () { context.push("/user/editProfile"); }, child: Container( padding: EdgeInsets.all(20.w), color: colorDCEAFE, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( children: [ Container( width: 66.w, height: 66.w, decoration: BoxDecoration( border: Border.all(color: Colors.white, width: 2.w), borderRadius: BorderRadius.circular(33.r), ), child: ClipOval( child: LoadImage( key: Key(Uri.encodeFull(user.avatar ?? '')), user.avatar ?? '', width: 66.w, height: 66.w, holderImg: "user_avatar", ), ), // CircleAvatar( // radius: 30, // backgroundImage: AssetImage( // Assets.images.userAvatar.path, // ), // 替换为实际头像路径 // ), ), const SizedBox(width: 16), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( user.nickName ?? "", style: TextStyle( fontSize: 16.sp, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 4), Text( user.description ?? "", style: TextStyle( fontSize: 12.sp, color: Colors.grey[600], ), ), ], ), ], ), Icon(Icons.arrow_forward_ios_rounded, color: color666666), ], ), ), ); } Widget _buildScoreCard() { UserModel user = ref.watch(globalUserProvider); return GestureDetector( onTap: () { context.push("/user/score"); }, child: Stack( alignment: Alignment.centerLeft, children: [ Image.asset( Assets.images.userScoreBg.path, height: 98.h, fit: BoxFit.fill, ), Positioned( left: 10.w, right: 10.w, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Column( mainAxisAlignment: MainAxisAlignment.start, children: [ Row( children: [ SizedBox(width: 12.w), Image.asset(Assets.images.starIcon.path, width: 20.w), SizedBox(width: 5.w), myTxt( text: "我的积分", color: Colors.white, fontSize: 14.sp, ), ], ), myTxt( text: user.credit ?? '13045', color: Colors.white, fontWeight: FontWeight.bold, fontSize: 24.sp, ), ], ), Icon(Icons.chevron_right, color: Colors.white, size: 30.w), ], ), ), ], ), ); } Widget _buildScoreShopCard() { return GestureDetector( onTap: () { context.push("/user/shop"); }, child: Stack( alignment: Alignment.centerLeft, children: [ Image.asset( Assets.images.userScoreShop.path, height: 98.h, fit: BoxFit.fill, ), Positioned( left: 10.w, right: 10.w, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ SizedBox(width: 12.w), Image.asset(Assets.images.starIcon.path, width: 20.w), SizedBox(width: 5.w), myTxt( text: "积分商城", color: Colors.white, fontSize: 14.sp, ), ], ), Padding( padding: EdgeInsets.only(top: 10.h, left: 10.w), child: myTxt( text: '海量礼品等你来兑', color: Colors.white, fontWeight: FontWeight.bold, fontSize: 12.sp, ), ), ], ), Icon(Icons.chevron_right, color: Colors.white, size: 30.w), ], ), ), ], ), ); } // 构建功能列表 Widget _buildFunctionList() { final List> functionItems = [ {'icon': Assets.images.activityIcon.path, 'title': '我的活动'}, {'icon': Assets.images.messageIcon.path, 'title': '消息中心'}, {'icon': Assets.images.favoriteIcon.path, 'title': '我的收藏'}, {'icon': Assets.images.favoriteIcon.path, 'title': '阅读历史'}, //{'icon': Icons.history_outlined, 'title': '历史阅读'}, // {'icon': Icons.settings_outlined, 'title': '设置'}, // {'icon': Icons.share, 'title': '分享APP'}, // {'icon': Icons.feedback_outlined, 'title': '意见反馈'}, ]; return Card( margin: EdgeInsets.all(10.w), child: ListView.separated( physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, padding: EdgeInsets.symmetric(horizontal: 15.w), itemCount: functionItems.length, separatorBuilder: (context, index) => Divider(height: 0.5.h, color: Colors.grey[200]), itemBuilder: (context, index) { final item = functionItems[index]; return GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { _itemClick(item["title"]); }, child: SizedBox( height: 45.h, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( children: [ Image.asset( item['icon'], color: color333333, width: 24.w, ), SizedBox(width: 5.w), myTxt( text: item['title'], fontSize: 16.sp, color: color333333, ), ], ), Icon(Icons.chevron_right, color: Colors.grey), ], ), ), ); }, ), ); } Widget _buildFunctionList2() { final List> functionItems = [ // {'icon': Icons.card_giftcard, 'title': '我的活动'}, // {'icon': Icons.message_outlined, 'title': '消息中心'}, // {'icon': Icons.collections_bookmark_outlined, 'title': '我的收藏'}, //{'icon': Icons.history_outlined, 'title': '历史阅读'}, {'icon': Assets.images.settingIcon.path, 'title': '设置'}, {'icon': Assets.images.shareIcon.path, 'title': '分享APP'}, {'icon': Assets.images.feedbackIcon.path, 'title': '意见反馈'}, ]; return Card( margin: EdgeInsets.all(10.w), child: ListView.separated( physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, padding: EdgeInsets.symmetric(horizontal: 15.w), itemCount: functionItems.length, separatorBuilder: (context, index) => Divider(height: 0.5.h, color: Colors.grey[200]), itemBuilder: (context, index) { final item = functionItems[index]; return GestureDetector( behavior: HitTestBehavior.opaque, onTap: () { _itemClick(item["title"]); }, child: SizedBox( height: 45.h, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( children: [ Image.asset( item['icon'], color: color333333, width: 24.w, ), SizedBox(width: 5.w), myTxt( text: item['title'], fontSize: 16.sp, color: color333333, ), ], ), Icon(Icons.chevron_right, color: Colors.grey), ], ), ), ); }, ), ); } void _itemClick(String title) { switch (title) { case "我的活动": context.push("/user/activity"); break; case "消息中心": context.push('/user/msgCenter'); break; case "我的收藏": context.push("/user/favorite"); break; case "阅读历史": context.push("/user/readHistory"); break; case "设置": context.push("/user/setting"); break; case "分享APP": context.push("/user/share"); break; case "意见反馈": context.push("/user/feedback"); break; } } @override bool get wantKeepAlive => true; }