dropdownbutton still not working fully

...but it works a bit more than before!
This commit is contained in:
Felix 2019-07-19 15:15:04 +01:00
parent 2e0802ac73
commit 525a092f40
No known key found for this signature in database
GPG key ID: 130EF6DC43E4DD07
3 changed files with 122 additions and 77 deletions

View file

@ -70,7 +70,9 @@ Future<List<Category>> getCategories() async { // confusing name
// }); // });
// print(categories[10].name.toString()); // prints "Banana" // print(categories[10].name.toString()); // prints "Banana"
return categories;
return categories; // categories is List<Category>
// Category is defined at the top^^
} else { } else {
// not successful // not successful
return null; return null;

View file

@ -1,35 +1,25 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:local_spend/common/apifunctions/categories.dart';
import 'dart:async';
class DropDownMenu extends StatefulWidget { Future<List<DropdownMenuItem>> getDropDownItems(String type) async {
List<DropdownMenuItem<String>> items = new List<DropdownMenuItem<String>>();
final List<DropdownMenuItem> items; // if (type == "categories") {
final Function onChanged; var categories = await getCategories();
final String value;
DropDownMenu({
this.items,
this.onChanged,
this.value,
});
@override categories.forEach((thisValue) {
_DropDownMenuState createState() => _DropDownMenuState(items: this.items, onNewValue: this.onChanged, value: this.value); items.add(
new DropdownMenuItem(
child: new Text(thisValue.name),
value: thisValue.index,
key: Key(thisValue.index),
),
);
});
print("oof");
return items;
// }
} }
class _DropDownMenuState extends State<DropDownMenu> {
final List<DropdownMenuItem> 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),);
}
}

View file

@ -29,10 +29,12 @@ class ReceiptPageState extends State<ReceiptPage> {
final TextEditingController _amountController = TextEditingController(); final TextEditingController _amountController = TextEditingController();
final TextEditingController _essentialController = TextEditingController(); final TextEditingController _essentialController = TextEditingController();
final TextEditingController _recurringController = TextEditingController(); final TextEditingController _recurringController = TextEditingController();
final TextEditingController _categoryController = TextEditingController(); // TODO: fix this!!
TextEditingController _categoryController; // TODO: fix this!!
final TextEditingController _orgController = TextEditingController(); final TextEditingController _orgController = TextEditingController();
final OrganizationController _organizationController = OrganizationController(); final OrganizationController _organizationController = OrganizationController();
List<DropdownMenuItem<String>> _categoryDropDownItems; DropdownButton _dropDownMenu = new DropdownButton(items: null, onChanged: null);
String _category;
FocusNode focusNode; FocusNode focusNode;
@ -53,9 +55,16 @@ class ReceiptPageState extends State<ReceiptPage> {
@override @override
void initState() { void initState() {
getCategoriesList().then((value) { // getCategoriesList().then((value) {
setState(() { // setState(() {
_categoryDropDownItems = value; // _categoryDropDownItems = value;
// });
// });
// loadDropDownMenu();
getDropDownMenu().then((value) {
setState((){
_dropDownMenu = value;
}); });
}); });
@ -65,7 +74,7 @@ class ReceiptPageState extends State<ReceiptPage> {
focusNode = FocusNode(); focusNode = FocusNode();
_recurringController.text = "None"; _recurringController.text = "None";
_categoryController.text = "None"; // _categoryController.text = null;
} }
@override @override
@ -82,7 +91,57 @@ class ReceiptPageState extends State<ReceiptPage> {
// this file is getting really messy sorry everyone // 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<T> item) => item.value == value).length == 1': is not true.
///
getDropDownItems("categories").then((categories) {
// _category = categories[0];
// _dropDownMenu = new DropdownButton<String>(/*value : _categoryController.text, */items: categories, onChanged: (newValue) => _categoryController.text = newValue);
setState(() {
_dropDownMenu = new DropdownButton(items: categories, onChanged: (newValue) => _category = newValue, value: _category);
});
});
}
Future<DropdownButton> getDropDownMenu() async {
List<DropdownMenuItem> 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<String>(
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'); SystemChannels.textInput.invokeMethod('TextInput.hide');
if (organisation == null) { if (organisation == null) {
@ -185,27 +244,27 @@ class ReceiptPageState extends State<ReceiptPage> {
return "false"; return "false";
} }
Future<List<DropdownMenuItem<String>>> getCategoriesList() async { // Future<List<DropdownMenuItem<String>>> getCategoriesList() async {
//TODO: Return a list of [String, String] where {1} is categoryName and {2} is categoryValue for request // //TODO: Return a list of [String, String] where {1} is categoryName and {2} is categoryValue for request
var categoriesList = List<DropdownMenuItem>(); // var categoriesList = List<DropdownMenuItem>();
//
var categories = await getCategories(); //future<list<cat>> // var categories = await getCategories(); //future<list<cat>>
//
categories.forEach((thisCategory) { // categories.forEach((thisCategory) {
// print(thisCategory.name); //// print(thisCategory.name);
var thisMap = new DropdownMenuItem( // var thisMap = new DropdownMenuItem(
child: new Text(thisCategory.name), // child: new Text(thisCategory.name),
value: thisCategory.index, // value: thisCategory.index,
); // );
//
categoriesList.add(thisMap); // categoriesList.add(thisMap);
}); // });
//
// print(categoriesStrings[10]); // prints 'Banana' //// print(categoriesStrings[10]); // prints 'Banana'
// print(categoriesStrings.toString()); //// print(categoriesStrings.toString());
//
return categoriesList; // return categoriesList;
} // }
List<String> getRecurringOptions() { List<String> getRecurringOptions() {
var options = new List<String>(7); var options = new List<String>(7);
@ -531,7 +590,6 @@ class ReceiptPageState extends State<ReceiptPage> {
), ),
), ),
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0,7,0.0,0.0), padding: EdgeInsets.fromLTRB(0.0,7,0.0,0.0),
@ -554,23 +612,18 @@ class ReceiptPageState extends State<ReceiptPage> {
Container( Container(
padding: const EdgeInsets.fromLTRB(29, 0, 0, 0), padding: const EdgeInsets.fromLTRB(29, 0, 0, 0),
child: DropdownButton( child: _dropDownMenu,
// items: <DropdownMenuItem>[ // child: DropdownButton(
// DropdownMenuItem(child: Text("eeeee"), value: "eeeee"), // items: _categoryDropDownItems,
// DropdownMenuItem(child: Text("Cucumbers"), value: "Cucumbers"), //
// DropdownMenuItem(child: Text("Mar shmellows"), value: "Marshmellows"), // value: _categoryController.text,
// DropdownMenuItem(child: Text("Pickled Sardines"), value: "Pickled Sardines"), //// value: "skip skap skop",
// ].toList(), // onChanged: (newValue) {
items: _categoryDropDownItems, // setState(() {
// _categoryController.text = newValue;
value: _categoryController.text, // });
// value: "skip skap skop", // }
onChanged: (newValue) { // ),
setState(() {
_categoryController.text = newValue;
});
}
),
), ),
// Container( // Container(