Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'package:food_app/api/place_api.dart';
- import 'package:food_app/model/place.dart';
- import 'package:food_app/notifier/place_notifier.dart';
- import 'package:flutter/material.dart';
- import 'package:image_picker/image_picker.dart';
- import 'package:provider/provider.dart';
- class FoodForm extends StatefulWidget {
- final bool isUpdating;
- FoodForm({@required this.isUpdating});
- @override
- _FoodFormState createState() => _FoodFormState();
- }
- class _FoodFormState extends State<FoodForm> {
- final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
- final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
- //List _subingredients = [];
- List _features = [];
- Food _currentFood;
- String _imageUrl;
- File _imageFile;
- // TextEditingController subingredientController = new TextEditingController();
- TextEditingController featuresController = new TextEditingController();
- @override
- void initState() {
- super.initState();
- FoodNotifier foodNotifier =
- Provider.of<FoodNotifier>(context, listen: false);
- if (foodNotifier.currentFood != null) {
- _currentFood = foodNotifier.currentFood;
- } else {
- _currentFood = Food();
- }
- // _subingredients.addAll(_currentFood.subIngredients);
- _features.addAll(_currentFood.features);
- _imageUrl = _currentFood.image;
- }
- _showImage() {
- if (_imageFile == null && _imageUrl == null) {
- return Text("Your image here");
- } else if (_imageFile != null) {
- print('showing image from local file');
- return Stack(
- alignment: AlignmentDirectional.bottomCenter,
- children: <Widget>[
- Image.file(
- _imageFile,
- fit: BoxFit.cover,
- height: 250,
- ),
- FlatButton(
- padding: EdgeInsets.all(16),
- color: Colors.black54,
- child: Text(
- 'Change Image',
- style: TextStyle(
- color: Colors.white,
- fontSize: 22,
- fontWeight: FontWeight.w400),
- ),
- onPressed: () => _getLocalImage(),
- )
- ],
- );
- } else if (_imageUrl != null) {
- print('showing image from url');
- return Stack(
- alignment: AlignmentDirectional.bottomCenter,
- children: <Widget>[
- Image.network(
- _imageUrl,
- width: MediaQuery.of(context).size.width,
- fit: BoxFit.cover,
- height: 250,
- ),
- FlatButton(
- padding: EdgeInsets.all(16),
- color: Colors.black54,
- child: Text(
- 'Change Image',
- style: TextStyle(
- color: Colors.white,
- fontSize: 22,
- fontWeight: FontWeight.w400),
- ),
- onPressed: () => _getLocalImage(),
- )
- ],
- );
- }
- }
- _getLocalImage() async {
- File imageFile = await ImagePicker.pickImage(
- source: ImageSource.gallery, imageQuality: 50, maxWidth: 400);
- if (imageFile != null) {
- setState(() {
- _imageFile = imageFile;
- });
- }
- }
- _getCameraImage() async {
- File imageFile = await ImagePicker.pickImage(
- source: ImageSource.camera, imageQuality: 50, maxWidth: 400);
- if (imageFile != null) {
- setState(() {
- _imageFile = imageFile;
- });
- }
- }
- Widget _buildNameField() {
- return TextFormField(
- decoration: InputDecoration(labelText: 'Name'),
- initialValue: _currentFood.name,
- keyboardType: TextInputType.text,
- textCapitalization: TextCapitalization.words,
- style: TextStyle(fontSize: 20),
- validator: (String value) {
- if (value.isEmpty) {
- return 'Name is required';
- }
- if (value.length < 3 || value.length > 20) {
- return 'Name must be more than 3 and less than 20';
- }
- return null;
- },
- onSaved: (String value) {
- _currentFood.name = value;
- },
- );
- }
- Widget _buildAddressField() {
- return TextFormField(
- decoration: InputDecoration(labelText: 'Address'),
- initialValue: _currentFood.address,
- keyboardType: TextInputType.text,
- textCapitalization: TextCapitalization.words,
- style: TextStyle(fontSize: 20),
- validator: (String value) {
- if (value.isEmpty) {
- return 'Address is required';
- }
- if (value.length < 7 || value.length > 50) {
- return 'Address must be more than 7 and less than 50';
- }
- return null;
- },
- onSaved: (String value) {
- _currentFood.address = value;
- },
- );
- }
- Widget _buildCityField() {
- return TextFormField(
- decoration: InputDecoration(labelText: 'City'),
- initialValue: _currentFood.city,
- keyboardType: TextInputType.text,
- style: TextStyle(fontSize: 20),
- validator: (String value) {
- if (value.isEmpty) {
- return 'City is required';
- }
- return null;
- },
- onSaved: (String value) {
- _currentFood.city = value;
- },
- );
- }
- Widget _buildStateField() {
- return TextFormField(
- decoration: InputDecoration(labelText: 'State'),
- initialValue: _currentFood.state,
- keyboardType: TextInputType.text,
- textCapitalization: TextCapitalization.words,
- style: TextStyle(fontSize: 20),
- validator: (String value) {
- if (value.isEmpty) {
- return 'State is required';
- }
- return null;
- },
- onSaved: (String value) {
- _currentFood.state = value;
- },
- );
- }
- Widget _buildPriceField() {
- return TextFormField(
- decoration: InputDecoration(labelText: 'Price'),
- initialValue: _currentFood.price,
- keyboardType: TextInputType.number,
- style: TextStyle(fontSize: 20),
- validator: (String value) {
- if (value.isEmpty) {
- return 'Price is required';
- }
- return null;
- },
- onSaved: (String value) {
- _currentFood.price = value;
- },
- );
- }
- Widget _buildSizeField() {
- return TextFormField(
- decoration: InputDecoration(labelText: 'Size (m²)'),
- initialValue: _currentFood.price,
- keyboardType: TextInputType.number,
- style: TextStyle(fontSize: 20),
- validator: (String value) {
- if (value.isEmpty) {
- return 'Size is required';
- }
- return null;
- },
- onSaved: (String value) {
- _currentFood.price = value;
- },
- );
- }
- Widget _buildDescriptionField() {
- return TextFormField(
- decoration: InputDecoration(labelText: 'Description'),
- initialValue: _currentFood.description,
- keyboardType: TextInputType.multiline,
- style: TextStyle(fontSize: 20),
- validator: (String value) {
- if (value.isEmpty) {
- return 'Description is required';
- }
- return null;
- },
- onSaved: (String value) {
- _currentFood.description = value;
- },
- );
- }
- Widget _buildCategoryField() {
- return TextFormField(
- decoration: InputDecoration(labelText: 'Category'),
- initialValue: _currentFood.category,
- keyboardType: TextInputType.text,
- style: TextStyle(fontSize: 20),
- validator: (String value) {
- if (value.isEmpty) {
- return 'Category is required';
- }
- if (value.length < 3 || value.length > 20) {
- return 'Category must be more than 3 and less than 20';
- }
- return null;
- },
- onSaved: (String value) {
- _currentFood.category = value;
- },
- );
- }
- /* _buildSubingredientField() {
- return SizedBox(
- width: 200,
- child: TextField(
- controller: subingredientController,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(labelText: 'Subingredient'),
- style: TextStyle(fontSize: 20),
- ),
- );
- } */
- _buildFeaturesField() {
- return SizedBox(
- width: 200,
- child: TextField(
- controller: featuresController,
- keyboardType: TextInputType.text,
- decoration: InputDecoration(labelText: 'Features'),
- style: TextStyle(fontSize: 20),
- ),
- );
- }
- _onFoodUploaded(Food food) {
- FoodNotifier foodNotifier =
- Provider.of<FoodNotifier>(context, listen: false);
- foodNotifier.addFood(food);
- Navigator.pop(context);
- }
- /* _addSubingredient(String text) {
- if (text.isNotEmpty) {
- setState(() {
- _subingredients.add(text);
- });
- subingredientController.clear();
- }
- } */
- _addFeatures(String text) {
- if (text.isNotEmpty) {
- setState(() {
- _features.add(text);
- });
- featuresController.clear();
- }
- }
- _saveFood() {
- print('saveFood Called');
- if (!_formKey.currentState.validate()) {
- return;
- }
- _formKey.currentState.save();
- print('form saved');
- // _currentFood.subIngredients = _subingredients;
- _currentFood.features = _features;
- uploadFoodAndImage(
- _currentFood, widget.isUpdating, _imageFile, _onFoodUploaded);
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- key: _scaffoldKey,
- appBar: AppBar(title: Text('Food Form')),
- body: SingleChildScrollView(
- padding: EdgeInsets.all(32),
- child: Form(
- key: _formKey,
- autovalidate: true,
- child: Column(children: <Widget>[
- _showImage(),
- SizedBox(height: 16),
- Text(
- widget.isUpdating ? "Edit Venue Space" : "Create Venue Space",
- textAlign: TextAlign.center,
- style: TextStyle(fontSize: 26),
- ),
- SizedBox(height: 16),
- _imageFile == null && _imageUrl == null
- ? ButtonTheme(
- child: RaisedButton(
- onPressed: () => _getLocalImage(),
- child: Text(
- 'Add Image',
- style: TextStyle(color: Colors.white),
- ),
- ),
- )
- : SizedBox(height: 0),
- _buildNameField(),
- _buildAddressField(),
- _buildCityField(),
- _buildStateField(),
- _buildPriceField(),
- _buildSizeField(),
- _buildDescriptionField(),
- _buildCategoryField(),
- /* Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- _buildSubingredientField(),
- ButtonTheme(
- child: RaisedButton(
- child: Text('Add', style: TextStyle(color: Colors.white)),
- onPressed: () =>
- _addSubingredient(subingredientController.text),
- ),
- ),
- ]), */
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- _buildFeaturesField(),
- ButtonTheme(
- child: RaisedButton(
- child: Text('Add', style: TextStyle(color: Colors.white)),
- onPressed: () => _addFeatures(featuresController.text),
- ),
- )
- ],
- ),
- /* SizedBox(height: 16),
- GridView.count(
- shrinkWrap: true,
- scrollDirection: Axis.vertical,
- padding: EdgeInsets.all(8),
- crossAxisCount: 3,
- crossAxisSpacing: 4,
- mainAxisSpacing: 4,
- children: _subingredients
- .map(
- (ingredient) => Card(
- color: Colors.black54,
- child: Center(
- child: Text(
- ingredient,
- style: TextStyle(color: Colors.white, fontSize: 14),
- ),
- ),
- ),
- )
- .toList(),
- ),*/
- SizedBox(height: 16),
- GridView.count(
- shrinkWrap: true,
- scrollDirection: Axis.vertical,
- padding: EdgeInsets.all(8),
- crossAxisCount: 5,
- crossAxisSpacing: 4,
- mainAxisSpacing: 4,
- children: _features
- .map(
- (features) => Card(
- color: Colors.brown[200],
- child: Center(
- child: Text(
- features,
- style: TextStyle(color: Colors.white, fontSize: 14),
- ),
- ),
- ),
- )
- .toList(),
- )
- ]),
- ),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: () {
- FocusScope.of(context).requestFocus(new FocusNode());
- _saveFood();
- },
- child: Icon(Icons.save),
- foregroundColor: Colors.white,
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment