From 696eb47fa8a9800add0f52105443fbe5c0599498 Mon Sep 17 00:00:00 2001 From: Scott Clampet Date: Fri, 3 May 2019 10:56:07 -0600 Subject: [PATCH] fix(edit_jar and add_jar forms): fix add category input and value bug re #111 --- fude_app/lib/pages/home/home_page_jar.dart | 20 ++--- fude_app/lib/pages/jars/jar.dart | 14 +++- fude_app/lib/pages/jars/jar_add.dart | 27 ++++--- fude_app/lib/pages/jars/jar_edit.dart | 4 +- fude_app/lib/scoped-models/jar.dart | 3 +- .../form-inputs/add_jar_category_input.dart | 23 +++--- fude_app/lib/widgets/forms/add_jar.dart | 73 ++++++++++++------- fude_app/lib/widgets/forms/edit_jar.dart | 32 ++++---- 8 files changed, 119 insertions(+), 77 deletions(-) diff --git a/fude_app/lib/pages/home/home_page_jar.dart b/fude_app/lib/pages/home/home_page_jar.dart index 248169c..318e75f 100644 --- a/fude_app/lib/pages/home/home_page_jar.dart +++ b/fude_app/lib/pages/home/home_page_jar.dart @@ -75,7 +75,7 @@ class HomePageJar extends StatelessWidget { iconSize: 26, color: Theme.of(context).textTheme.subhead.color, onPressed: () { - // print('jar pressed, ${jar['id']}'); + model.categoryChildren = []; model.getJarBySelectedId(jar.documentID); Navigator.push( context, @@ -95,20 +95,22 @@ class HomePageJar extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ IconButton( - icon: Icon(Icons.add), - iconSize: 70, - color: model.darkTheme - ? Color.fromRGBO(40, 40, 40, 1) - : Color.fromRGBO(242, 242, 242, 1), - onPressed: () => Navigator.push( + icon: Icon(Icons.add), + iconSize: 70, + color: model.darkTheme + ? Color.fromRGBO(40, 40, 40, 1) + : Color.fromRGBO(242, 242, 242, 1), + onPressed: () { + model.categoryChildren = []; + Navigator.push( context, PageTransition( curve: Curves.linear, type: PageTransitionType.downToUp, child: AddJarPage(), ), - ), - ), + ); + }), Text( title == null ? jar['title'].toUpperCase() : 'ADD JAR', style: TextStyle( diff --git a/fude_app/lib/pages/jars/jar.dart b/fude_app/lib/pages/jars/jar.dart index 2f92858..6440985 100644 --- a/fude_app/lib/pages/jars/jar.dart +++ b/fude_app/lib/pages/jars/jar.dart @@ -219,7 +219,7 @@ class _JarPageState extends State { child: AnimatedOpacity( opacity: _swiperVisible ? 1.0 : 0.0, duration: Duration(milliseconds: 1000), - child: Text( + child: model.selectedJar.data['categories'].length > 0 ?Text( 'SELECT CATEGORY', style: TextStyle( color: @@ -229,6 +229,16 @@ class _JarPageState extends State { letterSpacing: 3, ), textAlign: TextAlign.center, + ) : Text( + "YOU DON'T HAVE ANY CATEGORIES YET", + style: TextStyle( + color: + Theme.of(context).secondaryHeaderColor, + fontWeight: FontWeight.bold, + fontSize: 14, + letterSpacing: 3, + ), + textAlign: TextAlign.center, ), ), ), @@ -242,7 +252,7 @@ class _JarPageState extends State { opacity: _swiperVisible ? 1.0 : 0.0, duration: Duration(milliseconds: 500), child: PageView.builder( - reverse: true, + reverse: false, pageSnapping: true, scrollDirection: Axis.horizontal, controller: PageController( diff --git a/fude_app/lib/pages/jars/jar_add.dart b/fude_app/lib/pages/jars/jar_add.dart index b692e04..b8a1cf1 100644 --- a/fude_app/lib/pages/jars/jar_add.dart +++ b/fude_app/lib/pages/jars/jar_add.dart @@ -17,6 +17,7 @@ class AddJarPage extends StatefulWidget { class _JarPageState extends State { final GlobalKey formKey = GlobalKey(); int categoryCount = 0; + bool imageSelected = false; final Map _formData = { 'title': '', 'categories': [], @@ -31,7 +32,7 @@ class _JarPageState extends State { formKey.currentState.save(); // Save our form now. print(_formData); model.addJar(_formData); - + Navigator.pop(context); } } @@ -55,13 +56,17 @@ class _JarPageState extends State { void updateImage(File image) { print('image: $image'); setState(() { + imageSelected = true; _formData['image'] = image; }); } void updateCategoryCount() { - print(categoryCount); + print('categoryCount: $categoryCount'); setState(() { + if (imageSelected) { + imageSelected = false; + } categoryCount += 1; }); } @@ -70,6 +75,7 @@ class _JarPageState extends State { Widget build(BuildContext context) { final double height = MediaQuery.of(context).size.height; final double width = MediaQuery.of(context).size.width; + print('Building Jar_Add...'); return ScopedModelDescendant( builder: (BuildContext context, Widget child, MainModel model) { return Scaffold( @@ -102,14 +108,15 @@ class _JarPageState extends State { children: [ SizedBox(height: height * 0.04), AddJarForm( - formKey: formKey, - updateCategory: updateCategory, - updateTitle: updateTitle, - updateImage: updateImage, - updateCategoryCount: updateCategoryCount, - categoryCount: categoryCount, - categories: _formData['categories'], - ), + formKey: formKey, + imageSelected: imageSelected, + updateCategory: updateCategory, + updateTitle: updateTitle, + updateImage: updateImage, + updateCategoryCount: updateCategoryCount, + categoryCount: categoryCount, + categories: _formData['categories'], + model: model), SizedBox(height: height * 0.035), Row( mainAxisAlignment: MainAxisAlignment.start, diff --git a/fude_app/lib/pages/jars/jar_edit.dart b/fude_app/lib/pages/jars/jar_edit.dart index 645c94f..98e4c41 100644 --- a/fude_app/lib/pages/jars/jar_edit.dart +++ b/fude_app/lib/pages/jars/jar_edit.dart @@ -37,7 +37,7 @@ class _JarPageState extends State { @override void initState() { - _waitinForJarData(); + _waitingForJarData(); super.initState(); } @@ -133,7 +133,7 @@ class _JarPageState extends State { } } - void _waitinForJarData() { + void _waitingForJarData() { Timer(Duration(seconds: 1), () { setState(() { currentCategories = diff --git a/fude_app/lib/scoped-models/jar.dart b/fude_app/lib/scoped-models/jar.dart index a397aa8..574579f 100644 --- a/fude_app/lib/scoped-models/jar.dart +++ b/fude_app/lib/scoped-models/jar.dart @@ -1,5 +1,5 @@ import 'dart:async'; - +import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:validators/validators.dart'; import 'package:scoped_model/scoped_model.dart'; @@ -20,6 +20,7 @@ mixin JarModel on Model { 'categories': [] }; List _usersJars = []; + List categoryChildren = []; bool _isLoading = false; diff --git a/fude_app/lib/widgets/form-inputs/add_jar_category_input.dart b/fude_app/lib/widgets/form-inputs/add_jar_category_input.dart index 924f817..35601c8 100644 --- a/fude_app/lib/widgets/form-inputs/add_jar_category_input.dart +++ b/fude_app/lib/widgets/form-inputs/add_jar_category_input.dart @@ -11,15 +11,14 @@ class AddJarCategoryField extends StatelessWidget { final MainModel model; final List categories; - AddJarCategoryField({ - this.hint, - this.enabled, - this.needsAtLeastOneCategory, - this.updateCategory, - this.addCategoryToRemoveList, - this.model, - this.categories - }); + AddJarCategoryField( + {this.hint, + this.enabled, + this.needsAtLeastOneCategory, + this.updateCategory, + this.addCategoryToRemoveList, + this.model, + this.categories}); @override Widget build(BuildContext context) { final double height = MediaQuery.of(context).size.height; @@ -37,7 +36,8 @@ class AddJarCategoryField extends StatelessWidget { height: height * 0.08, width: width * 0.55, child: TextFormField( - controller: TextEditingController(text: hint != "Add Category" ? hint : null), + controller: TextEditingController( + text: hint != "Add Category" ? hint : null), textAlign: TextAlign.start, enabled: enabled, style: TextStyle( @@ -62,7 +62,8 @@ class AddJarCategoryField extends StatelessWidget { ), validator: (String val) { String finalVal = val.trim(); - if (needsAtLeastOneCategory == true || finalVal.isEmpty && categories.length < 1) { + if (needsAtLeastOneCategory == true || + finalVal.isEmpty && categories.length < 1) { return 'cannot be blank'; } }, diff --git a/fude_app/lib/widgets/forms/add_jar.dart b/fude_app/lib/widgets/forms/add_jar.dart index 29e6017..307113f 100644 --- a/fude_app/lib/widgets/forms/add_jar.dart +++ b/fude_app/lib/widgets/forms/add_jar.dart @@ -4,26 +4,31 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:fude/widgets/form-inputs/add_jar_name_input.dart'; import 'package:fude/widgets/form-inputs/add_jar_category_input.dart'; import 'package:fude/widgets/form-inputs/image.dart'; +import 'package:fude/scoped-models/main.dart'; class AddJarForm extends StatelessWidget { final GlobalKey formKey; final int categoryCount; + final bool imageSelected; final Function updateTitle; final Function updateCategory; final Function updateImage; final Function updateCategoryCount; final DocumentSnapshot jar; final List categories; + final MainModel model; AddJarForm( {this.formKey, this.jar, + this.imageSelected, this.categoryCount, this.updateTitle, this.updateCategory, this.updateImage, this.updateCategoryCount, - this.categories}); + this.categories, + this.model}); Widget _buildFormTitles(String title, BuildContext context) { return Row( @@ -54,25 +59,45 @@ class AddJarForm extends StatelessWidget { ); } - Column _addCategoryInputs(BuildContext context) { - var children = []; - for (var i = 0; i < categoryCount; i++) { - children.add( - Column( - children: [ - AddJarCategoryField( - hint: 'Add Category', - updateCategory: updateCategory, - enabled: true, - categories: categories, - + Column _initialCategoryInput() { + model.categoryChildren.length == 0 + ? model.categoryChildren.add( + Column( + children: [ + AddJarCategoryField( + hint: 'Add Category', + updateCategory: updateCategory, + model: model, + enabled: true, + categories: categories, + ), + ], ), - ], - ), - ); - } + ) + : Container(); + return Column( - children: children, + children: model.categoryChildren, + ); + } + + Column _addCategoryInputs() { + model.categoryChildren.add( + Column( + children: [ + AddJarCategoryField( + hint: 'Add Category', + updateCategory: updateCategory, + model: model, + enabled: true, + categories: categories, + ), + ], + ), + ); + + return Column( + children: model.categoryChildren, ); } @@ -99,13 +124,11 @@ class AddJarForm extends StatelessWidget { SizedBox(height: height * 0.035), _buildFormTitles("CATEGORIES", context), SizedBox(height: height * 0.01), - AddJarCategoryField( - hint: 'Add Category', - updateCategory: updateCategory, - enabled: true, - categories: categories, - ), - categoryCount > 0 ? _addCategoryInputs(context) : Container(), + categoryCount >= 0 && !imageSelected + ? _addCategoryInputs() + : Column( + children: model.categoryChildren, + ), SizedBox(height: height * 0.035), _buildFormTitles("JAR IMAGE", context), SizedBox(height: height * 0.03), diff --git a/fude_app/lib/widgets/forms/edit_jar.dart b/fude_app/lib/widgets/forms/edit_jar.dart index a6a1ca1..4cf93ef 100644 --- a/fude_app/lib/widgets/forms/edit_jar.dart +++ b/fude_app/lib/widgets/forms/edit_jar.dart @@ -90,24 +90,22 @@ class EditJarForm extends StatelessWidget { } Column _addCategoryInputs() { - var children = []; - for (var i = 0; i < categoryCount; i++) { - children.add( - Column( - children: [ - AddJarCategoryField( - hint: 'Add Category', - updateCategory: updateCategory, - model: model, - enabled: true, - categories: categories, - ), - ], - ), - ); - } + model.categoryChildren.add( + Column( + children: [ + AddJarCategoryField( + hint: 'Add Category', + updateCategory: updateCategory, + model: model, + enabled: true, + categories: categories, + ), + ], + ), + ); + return Column( - children: children, + children: model.categoryChildren, ); }