account_page.dart 12.4 KB
import 'package:appframe/bloc/setting/account_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class AccountPage extends StatelessWidget {
  const AccountPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => AccountCubit(AccountState()),
      child: BlocConsumer<AccountCubit, AccountState>(
        builder: (context, state) {
          var childInfoTable = (state.className.isNotEmpty && state.stuName.isNotEmpty)
              ? [
                  SizedBox(height: 24.0),
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 4.0),
                      child: Text(
                        '孩子信息',
                        style: TextStyle(
                          fontSize: 12.0,
                          color: Colors.black87,
                        ),
                      ),
                    ),
                  ),
                  SizedBox(height: 8.0),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 4.0),
                    decoration: BoxDecoration(
                      color: Color(0xFFF7F9FF),
                      borderRadius: BorderRadius.circular(8.0),
                      border: Border.all(color: Color(0xFFE0E6F7), width: 1),
                    ),
                    child: Column(
                      children: [
                        _buildInfoRow('班级', state.className, Icons.school),
                        Divider(height: 1, color: Color(0xFFE0E6F7)),
                        _buildInfoRow('学生', state.stuName, Icons.person),
                      ],
                    ),
                  ),
                ]
              : [];

          return Scaffold(
            backgroundColor: Colors.white,
            appBar: AppBar(
              title: Text('账号与安全', style: TextStyle(color: Colors.white, fontSize: 18)),
              centerTitle: true,
              backgroundColor: Color(0xFF7691FA),
              iconTheme: IconThemeData(
                color: Colors.white, // 设置返回图标按钮的颜色
              ),
            ),
            body: state.loaded
                ? (state.bindStu
                    ? Column(
                        children: [
                          // 用户头像和昵称部分
                          Container(
                            padding: EdgeInsets.all(16.0),
                            child: Column(
                              children: [
                                CircleAvatar(
                                  radius: 26.0,
                                  backgroundImage: NetworkImage(state.imgIcon),
                                ),
                                SizedBox(height: 16.0),
                                Text(
                                  state.name,
                                  style: TextStyle(
                                    fontSize: 18.0,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                                Text(
                                  state.nickname,
                                  style: TextStyle(fontSize: 12.0),
                                ),
                                ...childInfoTable,
                              ],
                            ),
                          ),
                          // 设置项列表
                          Expanded(
                            child: Padding(
                              padding: EdgeInsets.all(20),
                              child: ListView(
                                children: [
                                  Card(
                                    color: Color(0xFFF7F9FF),
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(6),
                                    ),
                                    child: ListTile(
                                      leading: Icon(Icons.person),
                                      title: Text('用户信息设置'),
                                      subtitle: Text(
                                        '点击设置用户信息',
                                        style: TextStyle(
                                          fontSize: 14.0,
                                          color: Colors.grey,
                                        ),
                                      ),
                                      trailing: Icon(Icons.arrow_forward_ios, size: 14),
                                      onTap: () {
                                        context.read<AccountCubit>().goSetUserInfo();
                                      },
                                    ),
                                  ),
                                  Card(
                                    color: Color(0xFFF7F9FF),
                                    shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(6),
                                    ),
                                    child: ListTile(
                                      leading: Icon(Icons.mobile_friendly),
                                      title: Text('手机号绑定'),
                                      subtitle: Text(
                                        state.phone != ''
                                            ? '${state.phone.substring(0, 3)}****${state.phone.substring(7, 11)}'
                                            : '未绑定',
                                        style: TextStyle(
                                          fontSize: 14.0,
                                          color: Colors.grey,
                                        ),
                                      ),
                                      trailing: Icon(Icons.arrow_forward_ios, size: 14),
                                      onTap: () {
                                        context.read<AccountCubit>().goBind();
                                      },
                                    ),
                                  ),
                                  SizedBox(height: 16),
                                  Padding(
                                    padding: EdgeInsets.all(80),
                                    child: SizedBox(
                                      width: double.infinity,
                                      height: 47,
                                      child: ElevatedButton(
                                        onPressed: () async {
                                          // 判断是否有绑定手机号
                                          if (state.phone != '') {
                                            context.read<AccountCubit>().goLogoff();
                                            return;
                                          }

                                          final accountCubit = context.read<AccountCubit>();
                                          bool? confirm = await showDialog<bool>(
                                            context: context,
                                            builder: (BuildContext ctx) {
                                              return AlertDialog(
                                                title: Text('确认注销'),
                                                content: Text('您确定要注销当前用户吗?'),
                                                actions: [
                                                  TextButton(
                                                    onPressed: () {
                                                      Navigator.of(ctx).pop(false);
                                                    },
                                                    child: Text('取消'),
                                                  ),
                                                  TextButton(
                                                    onPressed: () {
                                                      Navigator.of(ctx).pop(true);
                                                    },
                                                    child: Text('确认'),
                                                  ),
                                                ],
                                              );
                                            },
                                          );

                                          if (confirm == true) {
                                            ScaffoldMessenger.of(context).showSnackBar(
                                              SnackBar(
                                                content: Text('已申请注销,等待流程处理'),
                                                backgroundColor: Colors.green,
                                              ),
                                            );
                                            accountCubit.unbind();
                                          }
                                        },
                                        style: ElevatedButton.styleFrom(
                                          backgroundColor: Color(0xFFE74C3C),
                                          foregroundColor: Colors.white,
                                          textStyle: TextStyle(fontSize: 19),
                                          shape: RoundedRectangleBorder(
                                            borderRadius: BorderRadius.circular(23.5),
                                          ),
                                        ),
                                        child: Text(
                                          '注销用户',
                                          style: TextStyle(
                                            fontSize: 19,
                                            fontWeight: FontWeight.w400,
                                            color: Color(0xFFFFFFFF),
                                          ),
                                          strutStyle: StrutStyle(height: 22 / 19),
                                        ),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      )
                    : Container(
                        alignment: Alignment(0, -0.6),
                        child: Text(
                          '暂未关联孩子信息\n\n请在首页操作绑定孩子信息',
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            fontSize: 16.0,
                            color: Colors.grey,
                          ),
                        ),
                      ))
                : const Center(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        CircularProgressIndicator(color: Color(0xFF7691FA)),
                        SizedBox(height: 16),
                        Text('加载中...'),
                      ],
                    ),
                  ),
          );
        },
        listener: (context, state) {},
      ),
    );
  }

  Widget _buildInfoRow(String label, String value, IconData icon) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
      child: Row(
        children: [
          Icon(
            icon,
            size: 18.0,
            color: Color(0xFF7691FA),
          ),
          SizedBox(width: 8.0),
          SizedBox(
            width: 60,
            child: Text(
              label,
              style: TextStyle(
                fontSize: 14.0,
                color: Colors.grey[600],
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          Expanded(
            child: Text(
              value,
              style: TextStyle(
                fontSize: 14.0,
                color: Colors.black87,
              ),
            ),
          ),
        ],
      ),
    );
  }
}