Facebook
From Morose Penguin, 3 Years ago, written in Plain Text.
This paste is a reply to Untitled from Sharp Lizard - view diff
Embed
Download Paste or View Raw
Hits: 60
  1. class MyFormPage extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4.   return MultiBlocProvider(
  5.     providers: [
  6.       BlocProvider<ImageBlocBloc>(
  7.         create: (context) => getIt<ImageBlocBloc>(),
  8.       ),
  9.       BlocProvider<MyFormProviderBloc>(
  10.           create: (context) => getIt<MyFormProviderBloc>(),
  11.         )
  12.     ],
  13.     child: MyForm(),
  14.   );
  15. }
  16. }
  17.  
  18.  
  19.  
  20.  
  21. class MyForm extends StatefulWidget {
  22.   @override
  23.   _MyFormState createState() => _MyFormState();
  24. }
  25.  
  26. class _MyFormState extends State<MyForm> {
  27.   File file;
  28.   int selectedValue;
  29.   final _formKey = GlobalKey<FormState>();
  30.  
  31.   @override
  32.   void initState() {
  33.     context
  34.         .read<MyFormProviderBloc>()
  35.         .add(const MyFormProviderEvent.readMyFormProvider());
  36.   }
  37.  
  38.   @override
  39.   Widget build(BuildContext context) {
  40.     return Scaffold(
  41.       key: UniqueKey(),
  42.       appBar: AppBar(
  43.         elevation: 0,
  44.         backgroundColor: Colors.white,
  45.         automaticallyImplyLeading: true,
  46.         actions: [
  47.           SizedBox(
  48.             width: context.widthPx * .2,
  49.             child: CustomFlatButton(
  50.               name: "SAVE",
  51.               color: ColorsCustom.primaryRed,
  52.               onPressed: () {
  53.                 if ((context
  54.                     .read<MyFormProviderBloc>()
  55.                     .state
  56.                     .imageFileName ==
  57.                     '') ||
  58.  
  59.                     !_formKey.currentState.validate()) {
  60.                  
  61.  
  62.                   context.read<MyFormProviderBloc>().add(
  63.                       const MyFormProviderEvent
  64.                           .showErrorMessage(
  65.                           showErrorMessage: true));
  66.                 } else {
  67.                   context.read<MyFormProviderBloc>().add(
  68.                       const MyFormProviderEvent
  69.                           .showErrorMessage(
  70.                           showErrorMessage: false));
  71.  
  72.                   context.read<MyFormProviderBloc>().add(
  73.                       const MyFormProviderEvent
  74.                           .addMyFormProvider());
  75.                 }
  76.               },
  77.             ),
  78.           )
  79.         ],
  80.  
  81.         leading: IconButton(
  82.           icon: const Icon(
  83.             Icons.arrow_back_ios,
  84.             color: Color(0xFF3a3737),
  85.           ),
  86.           onPressed: () => ExtendedNavigator.of(context).pop(),
  87.         ),
  88.         title: const Text("My Form", style: TextStyles.textStyle6),
  89.       ),
  90.       body: Padding(
  91.         padding: const EdgeInsets.symmetric(horizontal: 8.0),
  92.         child: SingleChildScrollView(
  93.           child: BlocConsumer<MyFormProviderBloc, MyFormProviderState>(
  94.             cubit: BlocProvider.of(context),
  95.             listenWhen: (previous, current) =>
  96.             (previous.saveMyFormProviderFailureOrSuccessOption !=
  97.                 current.saveMyFormProviderFailureOrSuccessOption),
  98.             listener: (context, state) {
  99.               state.saveMyFormProviderFailureOrSuccessOption.fold(() => null,
  100.                       (a) {
  101.                     a.fold((l) {
  102.                       Fluttertoast.showToast(
  103.                           msg: "Failure",
  104.                           toastLength: Toast.LENGTH_SHORT,
  105.                           gravity: ToastGravity.CENTER,
  106.                           timeInSecForIosWeb: 1,
  107.                           backgroundColor: Colors.red,
  108.                           textColor: Colors.white,
  109.                           fontSize: 16.0);
  110.                     }, (r) {
  111.                       Fluttertoast.showToast(
  112.                           msg: "Success",
  113.                           toastLength: Toast.LENGTH_SHORT,
  114.                           gravity: ToastGravity.CENTER,
  115.                           timeInSecForIosWeb: 1,
  116.                           backgroundColor: Colors.red,
  117.                           textColor: Colors.white,
  118.                           fontSize: 16.0);
  119.  
  120.                       // ExtendedNavigator.of(context).pop();
  121.                     });
  122.                   });
  123.             },
  124.             builder: (context, state) {
  125.               return (state.isSubmitting ||
  126.                   state.readMyFormProviderFailureOrSuccessOption.isNone())
  127.                   ? Container(
  128.                 width: context.widthPx,
  129.                 height: context.heightPx,
  130.                 child: const Center(
  131.                   child: CircularProgressIndicator(),
  132.                 ),
  133.               )
  134.                   : Form(
  135.                 key: _formKey,
  136.                 // autovalidate: false,
  137.                 autovalidate: state.showErrorMessages,
  138.                 child: Column(
  139.                   children: [
  140.                     const SizedBox(
  141.                       height: 20,
  142.                     ),
  143.                     BlocConsumer<ImageBlocBloc, ImageBlocState>(
  144.                         listener: (context, state) {},
  145.                         builder: (context, state) {
  146.                           state.imageSuccessOrFail.fold(() => null, (a) {
  147.                             a.fold((failure) {
  148.                               final snackBar = SnackBar(
  149.                                   behavior: SnackBarBehavior.floating,
  150.                                   content: Text(FailureMessages()
  151.                                       .mapFailureToMessage(failure)));
  152.                               //   Scaffold.of(context).showSnackBar(snackBar);
  153.                             }, (pickedFile) {
  154.                               // _image1 = File(pickedFile.path);
  155.                               if (pickedFile != null) {
  156.                                 File(pickedFile.path);
  157.                                 print("PickedFile" + pickedFile.path);
  158.  
  159.                                 file = File(pickedFile.path);
  160.                               }
  161.                             });
  162.                           });
  163.                           state.imageSuccessOrFail.fold(() => null, (a) {
  164.                             a.fold((failure) {
  165.                               final snackBar = SnackBar(
  166.                                   behavior: SnackBarBehavior.floating,
  167.                                   content: Text(FailureMessages()
  168.                                       .mapFailureToMessage(failure)));
  169.                               Scaffold.of(context).showSnackBar(snackBar);
  170.                             }, (pickedFile) {
  171.                               // _image1 = File(pickedFile.path);
  172.                               if (pickedFile != null) {
  173.                                 File(pickedFile.path);
  174.                                 print("PickedFile" + pickedFile.path);
  175.  
  176.                                 //pickedFile.path add to bloc
  177.                                 context.read<MyFormProviderBloc>().add(
  178.                                     MyFormProviderEvent.imageChanged(
  179.                                         image: pickedFile.path));
  180.                               }
  181.                             });
  182.                           });
  183.  
  184.                      
  185.                         }),
  186.                    
  187.                     TextFormField(
  188.  
  189.                       initialValue: state.name,
  190.                       onChanged: (value) =>
  191.                           context
  192.                               .read<MyFormProviderBloc>()
  193.                               .add(
  194.                               MyFormProviderEvent.nameChanged(name: value)),
  195.                       validator: (_) =>
  196.                           context
  197.                               .read<MyFormProviderBloc>()
  198.                               .state
  199.                               .name
  200.                               .validateNotNull
  201.                               .fold(
  202.                                 (failure) =>
  203.                                 FailureMessages()
  204.                                     .mapFailureToMessage(failure),
  205.                                 (r) => null,
  206.                           ),
  207.                       showCursor: true,
  208.                       decoration: InputDecoration(
  209.                         border: OutlineInputBorder(
  210.                           borderSide: BorderSide(
  211.                             width: 0,
  212.                             style: BorderStyle.solid,
  213.                           ),
  214.                         ),
  215.                         hintStyle: TextStyles.textStyle,
  216.                         hintText: "Kitchen Name",
  217.                       ),
  218.                     ),
  219.                     const SizedBox(
  220.                       height: 10,
  221.                     ),
  222.                     TextFormField(
  223.  
  224.                       initialValue: state.subCategory,
  225.                       showCursor: true,
  226.                       onChanged: (value) =>
  227.                           context
  228.                               .read<MyFormProviderBloc>()
  229.                               .add(MyFormProviderEvent.subCategoryChanged(
  230.                               subCategory: value)),
  231.                       //subcategory=cuisine
  232.                       validator: (_) =>
  233.                           context
  234.                               .read<MyFormProviderBloc>()
  235.                               .state
  236.                               .subCategory
  237.                               .validateNotNull
  238.                               .fold(
  239.                                 (failure) =>
  240.                                 FailureMessages()
  241.                                     .mapFailureToMessage(failure),
  242.                                 (r) => null,
  243.                           ),
  244.                       decoration: InputDecoration(
  245.                         border: OutlineInputBorder(
  246.                           borderSide: BorderSide(
  247.                             width: 0,
  248.                             style: BorderStyle.solid,
  249.                           ),
  250.                         ),
  251.                         hintStyle: TextStyles.textStyle,
  252.                         hintText: "Cuisine",
  253.                       ),
  254.                     ),
  255.                     SizedBox(
  256.                       height: 10,
  257.                     ),
  258.                     TextFormField(
  259.  
  260.                       initialValue: state.availableBy,
  261.                       showCursor: true,
  262.                       onChanged: (value) =>
  263.                           context
  264.                               .read<MyFormProviderBloc>()
  265.                               .add(MyFormProviderEvent.availableByChanged(
  266.                               changeAvailableBy: value)),
  267.                       //subcategory=cuisine
  268.                       validator: (_) =>
  269.                           context
  270.                               .read<MyFormProviderBloc>()
  271.                               .state
  272.                               .availableBy
  273.                               .validateNotNull
  274.                               .fold(
  275.                                 (failure) =>
  276.                                 FailureMessages()
  277.                                     .mapFailureToMessage(failure),
  278.                                 (r) => null,
  279.                           ),
  280.                       decoration: InputDecoration(
  281.                         border: OutlineInputBorder(
  282.                           borderSide: BorderSide(
  283.                             width: 0,
  284.                             style: BorderStyle.solid,
  285.                           ),
  286.                         ),
  287.                         hintStyle: TextStyles.textStyle,
  288.                         hintText: "Available By ",
  289.                       ),
  290.                     ),
  291.                  
  292.                   ],
  293.                 ),
  294.               );
  295.             },
  296.           ),
  297.         ),
  298.       ),
  299.     );
  300.   }
  301.  
  302.  
  303. }
  304.