import 'package:cached_network_image/cached_network_image.dart'; import 'package:easy_refresh/easy_refresh.dart'; import 'package:flutter/material.dart'; import 'package:lszlgl/base/base_lifecycle_state.dart'; import 'package:lszlgl/base/base_state.dart'; import 'package:lszlgl/model/rsp/user_rsp.dart'; import 'package:lszlgl/service/user_service.dart'; import 'package:lszlgl/widget/card_item.dart'; import '../../main.dart'; import '../../network/my_api.dart'; /// 用户中心 class UserCenterPage extends StatefulWidget { const UserCenterPage({Key? key}) : super(key: key); @override State createState() => _UserCenterPageState(); } class _UserCenterPageState extends BaseLifecycleState with AutomaticKeepAliveClientMixin { final userNotify = null.notifier(); void startAccountManage() { MyRouter.startAccountManage(); } void startSetting() { MyRouter.startSetting(); } Future onRefresh() async { try { var value = await MyApi.get().userProfile(); if (value.data != null) userNotify.value = value.data; await UserService.get().saveUser(value.data); } catch (e) { logger.e(e); } } @override void onInit() { userNotify.value = UserService.get().getUser(); } @override Widget build(BuildContext context) { super.build(context); return myScaffold(child: buildBody()); } Widget buildBody() { return Column( children: [ myAppBar( title: '用户中心', autoLeading: false, naviBarColor: Colors.white, ), Expanded( child: EasyRefresh( onRefresh: onRefresh, child: CustomScrollView( slivers: [ SliverToBoxAdapter( child: Column( children: [ buildUserInfo(), const SizedBox(height: 24), buildList(), ], ), ) ], ), ), ), ], ); } Widget buildUserInfo() { return userNotify.builder((user) { return GestureDetector( onTap: startAccountManage, child: Container( margin: const EdgeInsets.symmetric(horizontal: 12), clipBehavior: Clip.hardEdge, decoration: BoxDecoration( borderRadius: const BorderRadius.all(Radius.circular(12)), boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.1), offset: const Offset(0, 5), blurRadius: 4)], image: const DecorationImage(image: AssetImage(imgHomeListBg), fit: BoxFit.fill), ), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Row( children: [ buildUserAvatar(user?.avatar ?? ''), Column( crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: [ Text( user?.nickname ?? '', style: const TextStyle(fontSize: 14, color: Color(0xFF333333), fontWeight: FontWeight.w500), ), const SizedBox(height: 14), Text( user?.username ?? '', style: const TextStyle(fontSize: 14, color: Color(0xFF333333), fontWeight: FontWeight.w500), ), ], ), ], ), ), buildDeptText(user?.dept?.name ?? ''), ], ), ), ); }); } Widget buildUserAvatar(String imageUrl) { return Container( width: 72, height: 72, margin: const EdgeInsets.symmetric(vertical: 24, horizontal: 12), clipBehavior: Clip.hardEdge, decoration: const BoxDecoration( color: Color(0xFF7085A1), borderRadius: BorderRadius.all(Radius.circular(200)), border: Border.fromBorderSide(BorderSide(color: Color(0xFF7085A1), width: 1, strokeAlign: 1.0)), ), child: CachedNetworkImage( fit: BoxFit.cover, imageUrl: imageUrl, placeholder: (_, __) => const Center(child: CircularProgressIndicator()), errorWidget: (context, url, error) => const Center(child: Icon(Icons.error, color: Colors.grey)), ), ); } Widget buildDeptText(String text) { return Padding( padding: const EdgeInsets.only(top: 12, right: 12), child: Text( text, style: const TextStyle( color: Color(0xFF333333), fontSize: 12, fontWeight: FontWeight.w500, ), ), ); } Widget buildList() { return Container( margin: const EdgeInsets.symmetric(horizontal: 12), clipBehavior: Clip.hardEdge, decoration: const BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10))), child: Column( children: [ CardItemWidget( '账号管理', onTap: startAccountManage, leading: Image.asset(imgUserCenterAccount, height: 16), trailing: Image.asset(imgItemArrowRight, height: 16), bottomLine: true, ), CardItemWidget( '设置', onTap: startSetting, leading: Image.asset(imgUserCenterSetting, height: 16), ), ], ), ); } @override bool get wantKeepAlive => true; }