class MyFormPage extends StatelessWidget { @override Widget build(BuildContext context) { return MultiBlocProvider( providers: [ BlocProvider( create: (context) => getIt(), ), BlocProvider( create: (context) => getIt(), ) ], child: MyForm(), ); } } class MyForm extends StatefulWidget { @override _MyFormState createState() => _MyFormState(); } class _MyFormState extends State { File file; int selectedValue; final _formKey = GlobalKey(); @override void initState() { context .read() .add(const MyFormProviderEvent.readMyFormProvider()); } @override Widget build(BuildContext context) { return Scaffold( key: UniqueKey(), appBar: AppBar( elevation: 0, backgroundColor: Colors.white, automaticallyImplyLeading: true, actions: [ SizedBox( width: context.widthPx * .2, child: CustomFlatButton( name: "SAVE", color: ColorsCustom.primaryRed, onPressed: () { if ((context .read() .state .imageFileName == '') || !_formKey.currentState.validate()) { context.read().add( const MyFormProviderEvent .showErrorMessage( showErrorMessage: true)); } else { context.read().add( const MyFormProviderEvent .showErrorMessage( showErrorMessage: false)); context.read().add( const MyFormProviderEvent .addMyFormProvider()); } }, ), ) ], leading: IconButton( icon: const Icon( Icons.arrow_back_ios, color: Color(0xFF3a3737), ), onPressed: () => ExtendedNavigator.of(context).pop(), ), title: const Text("My Form", style: TextStyles.textStyle6), ), body: Padding( padding: const EdgeInsets.symmetric(horizontal: 8.0), child: SingleChildScrollView( child: BlocConsumer( cubit: BlocProvider.of(context), listenWhen: (previous, current) => (previous.saveMyFormProviderFailureOrSuccessOption != current.saveMyFormProviderFailureOrSuccessOption), listener: (context, state) { state.saveMyFormProviderFailureOrSuccessOption.fold(() => null, (a) { a.fold((l) { Fluttertoast.showToast( msg: "Failure", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, timeInSecForIosWeb: 1, backgroundColor: Colors.red, textColor: Colors.white, fontSize: 16.0); }, (r) { Fluttertoast.showToast( msg: "Success", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER, timeInSecForIosWeb: 1, backgroundColor: Colors.red, textColor: Colors.white, fontSize: 16.0); // ExtendedNavigator.of(context).pop(); }); }); }, builder: (context, state) { return (state.isSubmitting || state.readMyFormProviderFailureOrSuccessOption.isNone()) ? Container( width: context.widthPx, height: context.heightPx, child: const Center( child: CircularProgressIndicator(), ), ) : Form( key: _formKey, // autovalidate: false, autovalidate: state.showErrorMessages, child: Column( children: [ const SizedBox( height: 20, ), BlocConsumer( listener: (context, state) {}, builder: (context, state) { state.imageSuccessOrFail.fold(() => null, (a) { a.fold((failure) { final snackBar = SnackBar( behavior: SnackBarBehavior.floating, content: Text(FailureMessages() .mapFailureToMessage(failure))); // Scaffold.of(context).showSnackBar(snackBar); }, (pickedFile) { // _image1 = File(pickedFile.path); if (pickedFile != null) { File(pickedFile.path); print("PickedFile" + pickedFile.path); file = File(pickedFile.path); } }); }); state.imageSuccessOrFail.fold(() => null, (a) { a.fold((failure) { final snackBar = SnackBar( behavior: SnackBarBehavior.floating, content: Text(FailureMessages() .mapFailureToMessage(failure))); Scaffold.of(context).showSnackBar(snackBar); }, (pickedFile) { // _image1 = File(pickedFile.path); if (pickedFile != null) { File(pickedFile.path); print("PickedFile" + pickedFile.path); //pickedFile.path add to bloc context.read().add( MyFormProviderEvent.imageChanged( image: pickedFile.path)); } }); }); }), TextFormField( initialValue: state.name, onChanged: (value) => context .read() .add( MyFormProviderEvent.nameChanged(name: value)), validator: (_) => context .read() .state .name .validateNotNull .fold( (failure) => FailureMessages() .mapFailureToMessage(failure), (r) => null, ), showCursor: true, decoration: InputDecoration( border: OutlineInputBorder( borderSide: BorderSide( width: 0, style: BorderStyle.solid, ), ), hintStyle: TextStyles.textStyle, hintText: "Kitchen Name", ), ), const SizedBox( height: 10, ), TextFormField( initialValue: state.subCategory, showCursor: true, onChanged: (value) => context .read() .add(MyFormProviderEvent.subCategoryChanged( subCategory: value)), //subcategory=cuisine validator: (_) => context .read() .state .subCategory .validateNotNull .fold( (failure) => FailureMessages() .mapFailureToMessage(failure), (r) => null, ), decoration: InputDecoration( border: OutlineInputBorder( borderSide: BorderSide( width: 0, style: BorderStyle.solid, ), ), hintStyle: TextStyles.textStyle, hintText: "Cuisine", ), ), SizedBox( height: 10, ), TextFormField( initialValue: state.availableBy, showCursor: true, onChanged: (value) => context .read() .add(MyFormProviderEvent.availableByChanged( changeAvailableBy: value)), //subcategory=cuisine validator: (_) => context .read() .state .availableBy .validateNotNull .fold( (failure) => FailureMessages() .mapFailureToMessage(failure), (r) => null, ), decoration: InputDecoration( border: OutlineInputBorder( borderSide: BorderSide( width: 0, style: BorderStyle.solid, ), ), hintStyle: TextStyles.textStyle, hintText: "Available By ", ), ), ], ), ); }, ), ), ), ); } }