123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- 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<UserCenterPage> createState() => _UserCenterPageState();
- }
- class _UserCenterPageState extends BaseLifecycleState<UserCenterPage> with AutomaticKeepAliveClientMixin {
- final userNotify = null.notifier<UserRsp?>();
- void startAccountManage() {
- MyRouter.startAccountManage();
- }
- void startSetting() {
- MyRouter.startSetting();
- }
- Future<void> 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: Stack(
- children: [
- 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),
- ),
- ],
- ),
- ],
- ),
- Positioned(
- right: 0,
- child: 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;
- }
|