esiribiz

second on edit

Apr 7th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 13.46 KB | None | 0 0
  1. import 'dart:io';
  2.  
  3. import 'package:food_app/api/place_api.dart';
  4. import 'package:food_app/model/place.dart';
  5. import 'package:food_app/notifier/place_notifier.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:image_picker/image_picker.dart';
  8. import 'package:provider/provider.dart';
  9.  
  10. class FoodForm extends StatefulWidget {
  11.   final bool isUpdating;
  12.  
  13.   FoodForm({@required this.isUpdating});
  14.  
  15.   @override
  16.   _FoodFormState createState() => _FoodFormState();
  17. }
  18.  
  19. class _FoodFormState extends State<FoodForm> {
  20.   final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  21.   final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  22.  
  23.   //List _subingredients = [];
  24.   List _features = [];
  25.   Food _currentFood;
  26.   String _imageUrl;
  27.   File _imageFile;
  28.   // TextEditingController subingredientController = new TextEditingController();
  29.   TextEditingController featuresController = new TextEditingController();
  30.  
  31.   @override
  32.   void initState() {
  33.     super.initState();
  34.     FoodNotifier foodNotifier =
  35.         Provider.of<FoodNotifier>(context, listen: false);
  36.  
  37.     if (foodNotifier.currentFood != null) {
  38.       _currentFood = foodNotifier.currentFood;
  39.     } else {
  40.       _currentFood = Food();
  41.     }
  42.  
  43.     // _subingredients.addAll(_currentFood.subIngredients);
  44.     _features.addAll(_currentFood.features);
  45.     _imageUrl = _currentFood.image;
  46.   }
  47.  
  48.   _showImage() {
  49.     if (_imageFile == null && _imageUrl == null) {
  50.       return Text("Your image here");
  51.     } else if (_imageFile != null) {
  52.       print('showing image from local file');
  53.  
  54.       return Stack(
  55.         alignment: AlignmentDirectional.bottomCenter,
  56.         children: <Widget>[
  57.           Image.file(
  58.             _imageFile,
  59.             fit: BoxFit.cover,
  60.             height: 250,
  61.           ),
  62.           FlatButton(
  63.             padding: EdgeInsets.all(16),
  64.             color: Colors.black54,
  65.             child: Text(
  66.               'Change Image',
  67.               style: TextStyle(
  68.                   color: Colors.white,
  69.                   fontSize: 22,
  70.                   fontWeight: FontWeight.w400),
  71.             ),
  72.             onPressed: () => _getLocalImage(),
  73.           )
  74.         ],
  75.       );
  76.     } else if (_imageUrl != null) {
  77.       print('showing image from url');
  78.  
  79.       return Stack(
  80.         alignment: AlignmentDirectional.bottomCenter,
  81.         children: <Widget>[
  82.           Image.network(
  83.             _imageUrl,
  84.             width: MediaQuery.of(context).size.width,
  85.             fit: BoxFit.cover,
  86.             height: 250,
  87.           ),
  88.           FlatButton(
  89.             padding: EdgeInsets.all(16),
  90.             color: Colors.black54,
  91.             child: Text(
  92.               'Change Image',
  93.               style: TextStyle(
  94.                   color: Colors.white,
  95.                   fontSize: 22,
  96.                   fontWeight: FontWeight.w400),
  97.             ),
  98.             onPressed: () => _getLocalImage(),
  99.           )
  100.         ],
  101.       );
  102.     }
  103.   }
  104.  
  105.   _getLocalImage() async {
  106.     File imageFile = await ImagePicker.pickImage(
  107.         source: ImageSource.gallery, imageQuality: 50, maxWidth: 400);
  108.  
  109.     if (imageFile != null) {
  110.       setState(() {
  111.         _imageFile = imageFile;
  112.       });
  113.     }
  114.   }
  115.  
  116.   _getCameraImage() async {
  117.     File imageFile = await ImagePicker.pickImage(
  118.         source: ImageSource.camera, imageQuality: 50, maxWidth: 400);
  119.  
  120.     if (imageFile != null) {
  121.       setState(() {
  122.         _imageFile = imageFile;
  123.       });
  124.     }
  125.   }
  126.  
  127.   Widget _buildNameField() {
  128.     return TextFormField(
  129.       decoration: InputDecoration(labelText: 'Name'),
  130.       initialValue: _currentFood.name,
  131.       keyboardType: TextInputType.text,
  132.       textCapitalization: TextCapitalization.words,
  133.       style: TextStyle(fontSize: 20),
  134.       validator: (String value) {
  135.         if (value.isEmpty) {
  136.           return 'Name is required';
  137.         }
  138.  
  139.         if (value.length < 3 || value.length > 20) {
  140.           return 'Name must be more than 3 and less than 20';
  141.         }
  142.  
  143.         return null;
  144.       },
  145.       onSaved: (String value) {
  146.         _currentFood.name = value;
  147.       },
  148.     );
  149.   }
  150.  
  151.   Widget _buildAddressField() {
  152.     return TextFormField(
  153.       decoration: InputDecoration(labelText: 'Address'),
  154.       initialValue: _currentFood.address,
  155.       keyboardType: TextInputType.text,
  156.       textCapitalization: TextCapitalization.words,
  157.       style: TextStyle(fontSize: 20),
  158.       validator: (String value) {
  159.         if (value.isEmpty) {
  160.           return 'Address is required';
  161.         }
  162.  
  163.         if (value.length < 7 || value.length > 50) {
  164.           return 'Address must be more than 7 and less than 50';
  165.         }
  166.  
  167.         return null;
  168.       },
  169.       onSaved: (String value) {
  170.         _currentFood.address = value;
  171.       },
  172.     );
  173.   }
  174.  
  175.   Widget _buildCityField() {
  176.     return TextFormField(
  177.       decoration: InputDecoration(labelText: 'City'),
  178.       initialValue: _currentFood.city,
  179.       keyboardType: TextInputType.text,
  180.       style: TextStyle(fontSize: 20),
  181.       validator: (String value) {
  182.         if (value.isEmpty) {
  183.           return 'City is required';
  184.         }
  185.  
  186.         return null;
  187.       },
  188.       onSaved: (String value) {
  189.         _currentFood.city = value;
  190.       },
  191.     );
  192.   }
  193.  
  194.   Widget _buildStateField() {
  195.     return TextFormField(
  196.       decoration: InputDecoration(labelText: 'State'),
  197.       initialValue: _currentFood.state,
  198.       keyboardType: TextInputType.text,
  199.       textCapitalization: TextCapitalization.words,
  200.       style: TextStyle(fontSize: 20),
  201.       validator: (String value) {
  202.         if (value.isEmpty) {
  203.           return 'State is required';
  204.         }
  205.  
  206.         return null;
  207.       },
  208.       onSaved: (String value) {
  209.         _currentFood.state = value;
  210.       },
  211.     );
  212.   }
  213.  
  214.   Widget _buildPriceField() {
  215.     return TextFormField(
  216.       decoration: InputDecoration(labelText: 'Price'),
  217.       initialValue: _currentFood.price,
  218.       keyboardType: TextInputType.number,
  219.       style: TextStyle(fontSize: 20),
  220.       validator: (String value) {
  221.         if (value.isEmpty) {
  222.           return 'Price is required';
  223.         }
  224.         return null;
  225.       },
  226.       onSaved: (String value) {
  227.         _currentFood.price = value;
  228.       },
  229.     );
  230.   }
  231.  
  232.   Widget _buildSizeField() {
  233.     return TextFormField(
  234.       decoration: InputDecoration(labelText: 'Size (m²)'),
  235.       initialValue: _currentFood.price,
  236.       keyboardType: TextInputType.number,
  237.       style: TextStyle(fontSize: 20),
  238.       validator: (String value) {
  239.         if (value.isEmpty) {
  240.           return 'Size is required';
  241.         }
  242.         return null;
  243.       },
  244.       onSaved: (String value) {
  245.         _currentFood.price = value;
  246.       },
  247.     );
  248.   }
  249.  
  250.   Widget _buildDescriptionField() {
  251.     return TextFormField(
  252.       decoration: InputDecoration(labelText: 'Description'),
  253.       initialValue: _currentFood.description,
  254.       keyboardType: TextInputType.multiline,
  255.       style: TextStyle(fontSize: 20),
  256.       validator: (String value) {
  257.         if (value.isEmpty) {
  258.           return 'Description is required';
  259.         }
  260.         return null;
  261.       },
  262.       onSaved: (String value) {
  263.         _currentFood.description = value;
  264.       },
  265.     );
  266.   }
  267.  
  268.   Widget _buildCategoryField() {
  269.     return TextFormField(
  270.       decoration: InputDecoration(labelText: 'Category'),
  271.       initialValue: _currentFood.category,
  272.       keyboardType: TextInputType.text,
  273.       style: TextStyle(fontSize: 20),
  274.       validator: (String value) {
  275.         if (value.isEmpty) {
  276.           return 'Category is required';
  277.         }
  278.  
  279.         if (value.length < 3 || value.length > 20) {
  280.           return 'Category must be more than 3 and less than 20';
  281.         }
  282.  
  283.         return null;
  284.       },
  285.       onSaved: (String value) {
  286.         _currentFood.category = value;
  287.       },
  288.     );
  289.   }
  290.  
  291. /*   _buildSubingredientField() {
  292.     return SizedBox(
  293.       width: 200,
  294.       child: TextField(
  295.         controller: subingredientController,
  296.         keyboardType: TextInputType.text,
  297.         decoration: InputDecoration(labelText: 'Subingredient'),
  298.         style: TextStyle(fontSize: 20),
  299.       ),
  300.     );
  301.   } */
  302.  
  303.   _buildFeaturesField() {
  304.     return SizedBox(
  305.       width: 200,
  306.       child: TextField(
  307.         controller: featuresController,
  308.         keyboardType: TextInputType.text,
  309.         decoration: InputDecoration(labelText: 'Features'),
  310.         style: TextStyle(fontSize: 20),
  311.       ),
  312.     );
  313.   }
  314.  
  315.   _onFoodUploaded(Food food) {
  316.     FoodNotifier foodNotifier =
  317.         Provider.of<FoodNotifier>(context, listen: false);
  318.     foodNotifier.addFood(food);
  319.     Navigator.pop(context);
  320.   }
  321.  
  322. /*   _addSubingredient(String text) {
  323.     if (text.isNotEmpty) {
  324.       setState(() {
  325.         _subingredients.add(text);
  326.       });
  327.       subingredientController.clear();
  328.     }
  329.   } */
  330.  
  331.   _addFeatures(String text) {
  332.     if (text.isNotEmpty) {
  333.       setState(() {
  334.         _features.add(text);
  335.       });
  336.       featuresController.clear();
  337.     }
  338.   }
  339.  
  340.   _saveFood() {
  341.     print('saveFood Called');
  342.     if (!_formKey.currentState.validate()) {
  343.       return;
  344.     }
  345.  
  346.     _formKey.currentState.save();
  347.  
  348.     print('form saved');
  349.  
  350.     // _currentFood.subIngredients = _subingredients;
  351.     _currentFood.features = _features;
  352.  
  353.     uploadFoodAndImage(
  354.         _currentFood, widget.isUpdating, _imageFile, _onFoodUploaded);
  355.   }
  356.  
  357.   @override
  358.   Widget build(BuildContext context) {
  359.     return Scaffold(
  360.       key: _scaffoldKey,
  361.       appBar: AppBar(title: Text('Food Form')),
  362.       body: SingleChildScrollView(
  363.         padding: EdgeInsets.all(32),
  364.         child: Form(
  365.           key: _formKey,
  366.           autovalidate: true,
  367.           child: Column(children: <Widget>[
  368.             _showImage(),
  369.             SizedBox(height: 16),
  370.             Text(
  371.               widget.isUpdating ? "Edit Venue Space" : "Create Venue Space",
  372.               textAlign: TextAlign.center,
  373.               style: TextStyle(fontSize: 26),
  374.             ),
  375.             SizedBox(height: 16),
  376.             _imageFile == null && _imageUrl == null
  377.                 ? ButtonTheme(
  378.                     child: RaisedButton(
  379.                       onPressed: () => _getLocalImage(),
  380.                       child: Text(
  381.                         'Add Image',
  382.                         style: TextStyle(color: Colors.white),
  383.                       ),
  384.                     ),
  385.                   )
  386.                 : SizedBox(height: 0),
  387.             _buildNameField(),
  388.             _buildAddressField(),
  389.             _buildCityField(),
  390.             _buildStateField(),
  391.             _buildPriceField(),
  392.             _buildSizeField(),
  393.             _buildDescriptionField(),
  394.             _buildCategoryField(),
  395. /*             Row(
  396.                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
  397.                 children: <Widget>[
  398.                   _buildSubingredientField(),
  399.                   ButtonTheme(
  400.                     child: RaisedButton(
  401.                       child: Text('Add', style: TextStyle(color: Colors.white)),
  402.                       onPressed: () =>
  403.                           _addSubingredient(subingredientController.text),
  404.                     ),
  405.                   ),
  406.                 ]), */
  407.             Row(
  408.               mainAxisAlignment: MainAxisAlignment.spaceBetween,
  409.               children: <Widget>[
  410.                 _buildFeaturesField(),
  411.                 ButtonTheme(
  412.                   child: RaisedButton(
  413.                     child: Text('Add', style: TextStyle(color: Colors.white)),
  414.                     onPressed: () => _addFeatures(featuresController.text),
  415.                   ),
  416.                 )
  417.               ],
  418.             ),
  419.             /*      SizedBox(height: 16),
  420.              GridView.count(
  421.               shrinkWrap: true,
  422.               scrollDirection: Axis.vertical,
  423.               padding: EdgeInsets.all(8),
  424.               crossAxisCount: 3,
  425.               crossAxisSpacing: 4,
  426.               mainAxisSpacing: 4,
  427.               children: _subingredients
  428.                   .map(
  429.                     (ingredient) => Card(
  430.                       color: Colors.black54,
  431.                       child: Center(
  432.                         child: Text(
  433.                           ingredient,
  434.                           style: TextStyle(color: Colors.white, fontSize: 14),
  435.                         ),
  436.                       ),
  437.                     ),
  438.                   )
  439.                   .toList(),
  440.             ),*/
  441.             SizedBox(height: 16),
  442.             GridView.count(
  443.               shrinkWrap: true,
  444.               scrollDirection: Axis.vertical,
  445.               padding: EdgeInsets.all(8),
  446.               crossAxisCount: 5,
  447.               crossAxisSpacing: 4,
  448.               mainAxisSpacing: 4,
  449.               children: _features
  450.                   .map(
  451.                     (features) => Card(
  452.                       color: Colors.brown[200],
  453.                       child: Center(
  454.                         child: Text(
  455.                           features,
  456.                           style: TextStyle(color: Colors.white, fontSize: 14),
  457.                         ),
  458.                       ),
  459.                     ),
  460.                   )
  461.                   .toList(),
  462.             )
  463.           ]),
  464.         ),
  465.       ),
  466.       floatingActionButton: FloatingActionButton(
  467.         onPressed: () {
  468.           FocusScope.of(context).requestFocus(new FocusNode());
  469.           _saveFood();
  470.         },
  471.         child: Icon(Icons.save),
  472.         foregroundColor: Colors.white,
  473.       ),
  474.     );
  475.   }
  476. }
Advertisement
Add Comment
Please, Sign In to add comment