From 525a092f40b845047da227cf790bc7dc2f90ffa5 Mon Sep 17 00:00:00 2001 From: Felix Date: Fri, 19 Jul 2019 15:15:04 +0100 Subject: [PATCH] dropdownbutton still not working fully ...but it works a bit more than before! --- lib/common/apifunctions/categories.dart | 4 +- lib/common/widgets/future_dropdownmenu.dart | 50 +++---- lib/pages/receipt_page.dart | 145 +++++++++++++------- 3 files changed, 122 insertions(+), 77 deletions(-) diff --git a/lib/common/apifunctions/categories.dart b/lib/common/apifunctions/categories.dart index a329309..6cdf153 100644 --- a/lib/common/apifunctions/categories.dart +++ b/lib/common/apifunctions/categories.dart @@ -70,7 +70,9 @@ Future> getCategories() async { // confusing name // }); // print(categories[10].name.toString()); // prints "Banana" - return categories; + + return categories; // categories is List + // Category is defined at the top^^ } else { // not successful return null; diff --git a/lib/common/widgets/future_dropdownmenu.dart b/lib/common/widgets/future_dropdownmenu.dart index b364b30..85573fb 100644 --- a/lib/common/widgets/future_dropdownmenu.dart +++ b/lib/common/widgets/future_dropdownmenu.dart @@ -1,35 +1,25 @@ import 'package:flutter/material.dart'; +import 'package:local_spend/common/apifunctions/categories.dart'; +import 'dart:async'; -class DropDownMenu extends StatefulWidget { +Future> getDropDownItems(String type) async { + List> items = new List>(); - final List items; - final Function onChanged; - final String value; - - DropDownMenu({ - this.items, - this.onChanged, - this.value, - }); +// if (type == "categories") { + var categories = await getCategories(); - @override - _DropDownMenuState createState() => _DropDownMenuState(items: this.items, onNewValue: this.onChanged, value: this.value); + categories.forEach((thisValue) { + items.add( + new DropdownMenuItem( + child: new Text(thisValue.name), + value: thisValue.index, + key: Key(thisValue.index), + ), + ); + }); + + print("oof"); + + return items; +// } } - -class _DropDownMenuState extends State { - - final List items; - final Function onNewValue; - final String value; - - _DropDownMenuState({ - this.items, - this.onNewValue, - this.value - }); - - @override - Widget build(BuildContext context) { - return new DropdownButton(value: this.value, items: this.items, onChanged: (newValue) => onNewValue(newValue),); - } -} \ No newline at end of file diff --git a/lib/pages/receipt_page.dart b/lib/pages/receipt_page.dart index 4f87047..b37601d 100644 --- a/lib/pages/receipt_page.dart +++ b/lib/pages/receipt_page.dart @@ -29,10 +29,12 @@ class ReceiptPageState extends State { final TextEditingController _amountController = TextEditingController(); final TextEditingController _essentialController = TextEditingController(); final TextEditingController _recurringController = TextEditingController(); - final TextEditingController _categoryController = TextEditingController(); // TODO: fix this!! + + TextEditingController _categoryController; // TODO: fix this!! final TextEditingController _orgController = TextEditingController(); final OrganizationController _organizationController = OrganizationController(); - List> _categoryDropDownItems; + DropdownButton _dropDownMenu = new DropdownButton(items: null, onChanged: null); + String _category; FocusNode focusNode; @@ -53,9 +55,16 @@ class ReceiptPageState extends State { @override void initState() { - getCategoriesList().then((value) { - setState(() { - _categoryDropDownItems = value; +// getCategoriesList().then((value) { +// setState(() { +// _categoryDropDownItems = value; +// }); +// }); +// loadDropDownMenu(); + + getDropDownMenu().then((value) { + setState((){ + _dropDownMenu = value; }); }); @@ -65,7 +74,7 @@ class ReceiptPageState extends State { focusNode = FocusNode(); _recurringController.text = "None"; - _categoryController.text = "None"; +// _categoryController.text = null; } @override @@ -82,7 +91,57 @@ class ReceiptPageState extends State { // this file is getting really messy sorry everyone - void submitReceipt(String amount, String time, Organisation organisation, String recurring, String category, String essential) async { + void loadDropDownMenu() async { +// _dropDownMenu = await getDropDownMenu( +// "categories", +// setCategory, +// ); + + /// + /// so the below code works when 'value' is not specified but then, obviously, the value of the dropdownbutton can't be set. + /// when it is specified, it doesn't work: + /// [VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: 'package:flutter/src/material/dropdown.dart': Failed assertion: line 608 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true. + /// + + getDropDownItems("categories").then((categories) { +// _category = categories[0]; +// _dropDownMenu = new DropdownButton(/*value : _categoryController.text, */items: categories, onChanged: (newValue) => _categoryController.text = newValue); + setState(() { + _dropDownMenu = new DropdownButton(items: categories, onChanged: (newValue) => _category = newValue, value: _category); + }); + }); + } + + Future getDropDownMenu() async { + List categories = await getDropDownItems("categories"); + + categories.forEach((thisOne) { + print("This value: " + thisOne.value); + }); +// _categoryController = new TextEditingController(text: null); + return new DropdownButton( + items: categories, + onChanged: (newValue) { + _category = newValue; + setState(() { + _category = newValue; + }); + }, + + /* + items: categories.map((dynamic value) { + return DropdownMenuItem( + value : value.value, + child: new Text(value.value) + ); + }).toList(), + */ + + value: _category); + } + + void submitReceipt(String amount, String time, Organisation organisation, String recurring, String category, String essential) async + { SystemChannels.textInput.invokeMethod('TextInput.hide'); if (organisation == null) { @@ -185,27 +244,27 @@ class ReceiptPageState extends State { return "false"; } - Future>> getCategoriesList() async { - //TODO: Return a list of [String, String] where {1} is categoryName and {2} is categoryValue for request - var categoriesList = List(); - - var categories = await getCategories(); //future> - - categories.forEach((thisCategory) { -// print(thisCategory.name); - var thisMap = new DropdownMenuItem( - child: new Text(thisCategory.name), - value: thisCategory.index, - ); - - categoriesList.add(thisMap); - }); - -// print(categoriesStrings[10]); // prints 'Banana' -// print(categoriesStrings.toString()); - - return categoriesList; - } +// Future>> getCategoriesList() async { +// //TODO: Return a list of [String, String] where {1} is categoryName and {2} is categoryValue for request +// var categoriesList = List(); +// +// var categories = await getCategories(); //future> +// +// categories.forEach((thisCategory) { +//// print(thisCategory.name); +// var thisMap = new DropdownMenuItem( +// child: new Text(thisCategory.name), +// value: thisCategory.index, +// ); +// +// categoriesList.add(thisMap); +// }); +// +//// print(categoriesStrings[10]); // prints 'Banana' +//// print(categoriesStrings.toString()); +// +// return categoriesList; +// } List getRecurringOptions() { var options = new List(7); @@ -531,7 +590,6 @@ class ReceiptPageState extends State { ), ), - Padding( padding: EdgeInsets.fromLTRB(0.0,7,0.0,0.0), @@ -554,23 +612,18 @@ class ReceiptPageState extends State { Container( padding: const EdgeInsets.fromLTRB(29, 0, 0, 0), - child: DropdownButton( -// items: [ -// DropdownMenuItem(child: Text("eeeee"), value: "eeeee"), -// DropdownMenuItem(child: Text("Cucumbers"), value: "Cucumbers"), -// DropdownMenuItem(child: Text("Mar shmellows"), value: "Marshmellows"), -// DropdownMenuItem(child: Text("Pickled Sardines"), value: "Pickled Sardines"), -// ].toList(), - items: _categoryDropDownItems, - - value: _categoryController.text, -// value: "skip skap skop", - onChanged: (newValue) { - setState(() { - _categoryController.text = newValue; - }); - } - ), + child: _dropDownMenu, +// child: DropdownButton( +// items: _categoryDropDownItems, +// +// value: _categoryController.text, +//// value: "skip skap skop", +// onChanged: (newValue) { +// setState(() { +// _categoryController.text = newValue; +// }); +// } +// ), ), // Container(