import 'dart:collection'; import 'package:aitrainer_app/bloc/account/account_bloc.dart'; import 'package:aitrainer_app/bloc/customer_change/customer_change_bloc.dart'; import 'package:aitrainer_app/model/cache.dart'; import 'package:aitrainer_app/util/enums.dart'; import 'package:aitrainer_app/util/trans.dart'; import 'package:aitrainer_app/widgets/app_bar_min.dart'; import 'package:aitrainer_app/widgets/app_bar_progress.dart'; import 'package:aitrainer_app/widgets/number_picker.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart'; import 'package:toggle_switch/toggle_switch.dart'; import '../library_keys.dart'; // ignore: must_be_immutable class CustomerModifyPage extends StatelessWidget with Trans { final GlobalKey _scaffoldKey = new GlobalKey(); bool fulldata = false; @override Widget build(BuildContext context) { dynamic arguments = ModalRoute.of(context)!.settings.arguments; if (arguments is HashMap && arguments['personal_data'] != null) { fulldata = arguments['personal_data']; } setContext(context); // ignore: close_sinks final accountBloc = BlocProvider.of(context); return BlocProvider( create: (context) => CustomerChangeBloc(customerRepository: accountBloc.customerRepository)..add(CustomerLoad()), child: Builder(builder: (context) { // ignore: close_sinks final customerBloc = BlocProvider.of(context); PreferredSizeWidget _bar = AppBarMin( back: true, ); if (!fulldata) { _bar = AppBarProgress(max: 25, min: 0); } return Scaffold( resizeToAvoidBottomInset: true, appBar: _bar, body: Container( decoration: BoxDecoration( image: DecorationImage( image: AssetImage('asset/image/WT_light_background.jpg'), fit: BoxFit.fill, alignment: Alignment.center, ), ), child: BlocConsumer( listener: (context, state) { if (state is CustomerSaveError) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( backgroundColor: Colors.orange, content: Text(t(state.message), style: TextStyle(color: Colors.white)))); } else if (state is CustomerSaveSuccess) { if (fulldata) { Navigator.of(context).pop(); } else { Navigator.of(context).pushNamed("customerGoalPage", arguments: customerBloc.customerRepository); } } }, builder: (context, state) { return ModalProgressHUD( child: loadForm(customerBloc), inAsyncCall: state is CustomerChangeLoading, opacity: 0.5, color: Colors.black54, progressIndicator: CircularProgressIndicator(), ); }, ))); })); } Widget loadForm(CustomerChangeBloc customerBloc) { return Form( key: _scaffoldKey, autovalidateMode: AutovalidateMode.always, child: SingleChildScrollView( scrollDirection: Axis.vertical, padding: EdgeInsets.only(top: 40, left: 25, right: 25, bottom: 250), child: Container( alignment: Alignment.center, child: Column( children: [ Text(t("Edit Profile"), style: GoogleFonts.inter(color: Colors.indigo, fontSize: 16), textAlign: TextAlign.center), Divider( color: Colors.transparent, ), Cache().getLoginType() == LoginType.email || fulldata ? TextFormField( key: LibraryKeys.loginEmailField, decoration: InputDecoration( contentPadding: EdgeInsets.only(left: 15, top: 15, bottom: 15), labelText: t('Email'), fillColor: Colors.white24, filled: true, border: OutlineInputBorder( gapPadding: 4.0, borderRadius: BorderRadius.circular(12.0), borderSide: BorderSide(color: Colors.green[50]!, width: 0.4), ), ), initialValue: customerBloc.customerRepository.customer.email, autovalidateMode: AutovalidateMode.always, validator: (val) { String? validator = customerBloc.emailValidation(val!); return validator == null ? null : t(validator); }, keyboardType: TextInputType.emailAddress, style: new TextStyle(fontSize: 16, color: Colors.indigo), ) : Offstage(), Divider( color: Colors.transparent, ), Cache().getLoginType() == LoginType.email ? TextFormField( key: LibraryKeys.loginPasswordField, obscureText: true, decoration: InputDecoration( labelStyle: TextStyle(fontSize: 14), contentPadding: EdgeInsets.only(left: 15, top: 15, bottom: 15), suffixIcon: IconButton( onPressed: () => {customerBloc.add(CustomerChangePasswordObscure())}, icon: Icon(Icons.remove_red_eye), ), labelText: t('Password (Leave empty if no change)'), fillColor: Colors.white24, filled: true, border: OutlineInputBorder( gapPadding: 1.0, borderRadius: BorderRadius.circular(12.0), borderSide: BorderSide(color: Colors.green[50]!, width: 0.4), ), ), initialValue: customerBloc.customerRepository.customer.password, autovalidateMode: AutovalidateMode.always, validator: (val) { String? validator = customerBloc.passwordValidation(val!); return validator == null ? null : t(validator); }, keyboardType: TextInputType.visiblePassword, style: new TextStyle(fontSize: 16, color: Colors.indigo), ) : Offstage(), Divider( color: Colors.transparent, ), Cache().getLoginType() != LoginType.apple || fulldata ? TextFormField( decoration: InputDecoration( contentPadding: EdgeInsets.only(left: 15, top: 15, bottom: 15), labelText: t('Name'), fillColor: Colors.white24, filled: true, border: OutlineInputBorder( gapPadding: 1.0, borderRadius: BorderRadius.circular(12.0), borderSide: BorderSide(color: Colors.green[50]!, width: 0.4), ), ), initialValue: customerBloc.customerRepository.customer.name, autovalidateMode: AutovalidateMode.always, validator: (val) { return customerBloc.nameValidation(val); }, keyboardType: TextInputType.name, style: new TextStyle(fontSize: 16, color: Colors.indigo), onChanged: (value) => {customerBloc.add(CustomerNameChange(name: value))}) : Offstage(), Divider( color: Colors.transparent, ), Cache().getLoginType() != LoginType.apple || fulldata ? TextFormField( decoration: InputDecoration( contentPadding: EdgeInsets.only(left: 15, top: 15, bottom: 15), labelText: t('First Name'), fillColor: Colors.white24, filled: true, border: OutlineInputBorder( gapPadding: 2.0, borderRadius: BorderRadius.circular(12.0), borderSide: BorderSide(color: Colors.green[50]!, width: 0.4), ), ), initialValue: customerBloc.customerRepository.customer.firstname, autovalidateMode: AutovalidateMode.always, validator: (val) { return customerBloc.nameValidation(val); }, keyboardType: TextInputType.name, style: new TextStyle(fontSize: 16, color: Colors.indigo), onChanged: (value) => {customerBloc.add(CustomerFirstNameChange(firstName: value))}) : Offstage(), Divider( color: Colors.transparent, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( flex: 4, child: Text(t("Birth Year"), style: TextStyle(fontWeight: FontWeight.normal, fontSize: 18)), ), NumberPickerWidget( minValue: 1930, maxValue: 2100, initalValue: customerBloc.year!.toInt(), unit: " ", color: Colors.indigo, onChange: (value) => {customerBloc.add(CustomerBirthYearChange(year: value.toInt()))}), SizedBox(width: 80), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Expanded( flex: 4, child: Text(t("Weight"), style: TextStyle(fontWeight: FontWeight.normal, fontSize: 18)), ), NumberPickerWidget( minValue: 0, maxValue: 200, initalValue: customerBloc.weight.toInt(), unit: " ", color: Colors.indigo, onChange: (value) => {customerBloc.add(CustomerWeightChange(weight: value.toInt()))}), SizedBox(width: 80), ], ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Expanded( flex: 4, child: Text(t("Height"), style: TextStyle(fontWeight: FontWeight.normal, fontSize: 18)), ), NumberPickerWidget( minValue: 0, maxValue: 230, initalValue: customerBloc.height.toInt(), unit: " ", color: Colors.indigo[300]!, onChange: (value) => {customerBloc.add(CustomerHeightChange(height: value.toInt()))}), SizedBox(width: 80), ], ), Divider(), ToggleSwitch( minWidth: 80.0, minHeight: 50.0, fontSize: 14.0, initialLabelIndex: customerBloc.customerRepository.customer.sex == "m" ? 0 : 1, activeBgColor: Colors.indigo, activeFgColor: Colors.white, inactiveBgColor: Colors.white30, inactiveFgColor: Colors.grey[900], labels: [t('Man'), t('Woman')], onToggle: (index) { customerBloc.add(CustomerGenderChange(gender: index)); }, ), Divider(), TextButton( onPressed: () => {customerBloc.add(CustomerSave())}, child: Stack( alignment: Alignment.center, children: [ Image.asset('asset/icon/gomb_orange_a.png', width: 140, height: 60), Text( fulldata ? t("Save") : t("Next"), style: TextStyle(fontSize: 16, color: Colors.white), ), ], ), ), ], ), ), ), ); } }