modify_user_information_page.dart 8.44 KB
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wow_english/common/core/assets_const.dart';
import 'package:wow_english/common/core/user_util.dart';
import 'package:wow_english/common/widgets/textfield_customer_widget.dart';
import 'package:wow_english/common/widgets/we_app_bar.dart';
import 'package:wow_english/pages/user/bloc/user_bloc.dart';
import 'package:wow_english/pages/user/modify/user_info_bloc/user_info_bloc.dart';
import 'package:wow_english/route/route.dart';
import 'package:wow_english/utils/toast_util.dart';

enum ModifyUserInformationType {
  avatar('头像'),
  name('名字'),
  age('年龄'),
  gender('性别');

  const ModifyUserInformationType(this.title);

  final String title;
}

class ModifyUserInformationPage extends StatelessWidget {
  final ModifyUserInformationType type;

  const ModifyUserInformationPage({super.key, required this.type});

  static push(BuildContext context, ModifyUserInformationType type) {
    Navigator.of(context).push(CupertinoPageRoute(builder: (context) {
      return ModifyUserInformationPage(type: type);
    }));
  }

  @override
  Widget build(BuildContext context) {
    String text = '';
    if (type == ModifyUserInformationType.age) {
      text = UserUtil.getUser()!.age.toString();
    } else if (type == ModifyUserInformationType.name) {
      text = UserUtil.getUser()!.name;
    }
    return BlocProvider(
      create: (context) => UserInfoBloc()..add(InitControllerTextEvent(text)),
      child: MultiBlocListener(
        listeners: [
          BlocListener<UserBloc,UserState>(listener: (context, state){
            if (state is UserInfoUpdated) {
              showToast('修改成功');
              popPage();
            }
          })
        ],
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: WEAppBar(
            titleText: '修改${type.title}',
          ),
          body: BlocBuilder<UserInfoBloc, UserInfoState>(
              builder: (context, state) {
                final bloc = BlocProvider.of<UserInfoBloc>(context);
                return SingleChildScrollView(
                    child: Column(
                      children: [
                        Padding(
                            padding: EdgeInsets.only(left: 65.w, right: 55.w, top: 38.h),
                            child: Row(
                              children: [
                                Text(
                                  type.title,
                                  style: TextStyle(
                                    fontWeight: FontWeight.w700,
                                    color: const Color(0xFF333333),
                                    fontSize: 21.sp,
                                  ),
                                ),
                                20.horizontalSpace,
                                // 输入框 or 选择框
                                _buildTextFieldWidget(context),
                                // 占位
                                Expanded(
                                  child: Container(),
                                ),
                                // 按钮
                                GestureDetector(
                                  onTap: () {
                                    var text = '';
                                    if (type == ModifyUserInformationType.name || type == ModifyUserInformationType.age){
                                      if(bloc.modifyTextController.text.isEmpty) {
                                        showToast('内容不能为空');
                                        return;
                                      }
                                      text = bloc.modifyTextController.text;
                                    }

                                    if (type == ModifyUserInformationType.gender) {
                                      text = bloc.gender.toString();
                                    }
                                    // 更新type类型的字段
                                    context.read<UserBloc>().add(UserUpdate(type,text));
                                  },
                                  child: Container(
                                    alignment: Alignment.center,
                                    width: 90.w,
                                    height: 44.h,
                                    decoration: const BoxDecoration(
                                      image: DecorationImage(image: AssetImage(AssetsConst.bgButtonBlue), fit: BoxFit.fill),
                                    ),
                                    child: Text(
                                      '确定',
                                      style: TextStyle(fontSize: 17.sp, color: Colors.white),
                                    ),
                                  ),
                                )
                              ],
                            )),
                        Stack(
                          alignment: Alignment.topRight,
                          children: [
                            Image.asset(
                              AssetsConst.bgEditUserInformation,
                              width: double.infinity,
                            ),
                            Positioned(
                              right: 125.w,
                              top: 10.h,
                              child: Image.asset(AssetsConst.bgIcSteveWrite, width: 161.w, height: 249.w),
                            ),
                          ],
                        ),
                      ],
                    ));
              }),
        ),
      ),
    );
  }

  Widget _buildTextFieldWidget(BuildContext context) {
    if (type == ModifyUserInformationType.gender) {
      return BlocBuilder<UserInfoBloc, UserInfoState>(
          builder: (context,state){
            final bloc = BlocProvider.of<UserInfoBloc>(context);
            return Row(
              children: <Widget>[
                GestureDetector(
                  onTap: () => bloc.add(ChangeGenderEvent(0)),
                  child: _buildSexWidget('男', bloc.gender == 0),
                ),
                70.horizontalSpace,
                GestureDetector(
                  onTap: () => bloc.add(ChangeGenderEvent(1)),
                  child: _buildSexWidget('女', bloc.gender == 1),
                )
              ],
            );
          });
    }

    var formatters = [
      LengthLimitingTextInputFormatter(12),
      FilteringTextInputFormatter.deny(RegExp('[ ]')),
    ];
    var inputType = TextInputType.name;
    // 当输入年龄时
    if (type == ModifyUserInformationType.age) {
      formatters = [
        LengthLimitingTextInputFormatter(2),
        FilteringTextInputFormatter.deny(RegExp('[ ]')),
      ];
      inputType = TextInputType.number;
    }

    return BlocBuilder<UserInfoBloc, UserInfoState>(
        builder: (context,state){
          final bloc = BlocProvider.of<UserInfoBloc>(context);
          return TextFieldCustomerWidget(
            height: 65.h,
            width: 222.w,
            textStyle: TextStyle(
              fontWeight: FontWeight.w700,
              color: const Color(0xFF333333),
              fontSize: 21.sp,
            ),
            bgImageName: 'Input_layer_up',
            textInputType: inputType,
            inputFormatters: formatters,
            controller: bloc.modifyTextController,
            /*onChangeValue: (String value) {
                          bloc.add(CodeNumberChangeEvent());
                        },*/
          );
        });
  }

  Widget _buildSexWidget(String title,bool isSelect) {
    return Row(
      children: [
        Container(
          width: 21.h,
          height: 21.h,
          padding: EdgeInsets.all(4.h),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(21.r),
              border: Border.all(
                  color: const Color(0xFF140C10),
                  width: 1.5
              )
          ),
          child: Offstage(
            offstage:!isSelect,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(13.r),
              child: Container(
                color: Colors.blue,
              ),
            ),
          ),
        ),
        5.horizontalSpace,
        Text(title)
      ],
    );
  }
}