diff --git a/lib/common/apifunctions/categories.dart b/lib/common/apifunctions/categories.dart index 6f3f590..a329309 100644 --- a/lib/common/apifunctions/categories.dart +++ b/lib/common/apifunctions/categories.dart @@ -13,7 +13,7 @@ class Category { }); } -Future> getCategories() async { +Future> getCategories() async { // confusing name const url = "https://dev.peartrade.org/api/search/category"; var token; diff --git a/lib/common/widgets/future_dropdownmenu.dart b/lib/common/widgets/future_dropdownmenu.dart new file mode 100644 index 0000000..b364b30 --- /dev/null +++ b/lib/common/widgets/future_dropdownmenu.dart @@ -0,0 +1,35 @@ +import 'package:flutter/material.dart'; + +class DropDownMenu extends StatefulWidget { + + final List items; + final Function onChanged; + final String value; + + DropDownMenu({ + this.items, + this.onChanged, + this.value, + }); + + @override + _DropDownMenuState createState() => _DropDownMenuState(items: this.items, onNewValue: this.onChanged, value: this.value); +} + +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 878a624..4f87047 100644 --- a/lib/pages/receipt_page.dart +++ b/lib/pages/receipt_page.dart @@ -12,6 +12,7 @@ import 'package:datetime_picker_formfield/datetime_picker_formfield.dart'; import 'package:local_spend/common/apifunctions/find_organisations.dart'; import 'package:local_spend/common/widgets/popupListView.dart'; import 'package:local_spend/common/apifunctions/categories.dart'; +import 'package:local_spend/common/widgets/future_dropdownmenu.dart'; const URL = "https://flutter.io/"; const demonstration = false; @@ -31,7 +32,7 @@ class ReceiptPageState extends State { final TextEditingController _categoryController = TextEditingController(); // TODO: fix this!! final TextEditingController _orgController = TextEditingController(); final OrganizationController _organizationController = OrganizationController(); - List _categoryDropDownItems = List(); + List> _categoryDropDownItems; FocusNode focusNode; @@ -51,8 +52,11 @@ class ReceiptPageState extends State { @override void initState() { - getCategoriesStrings().then((value) { + + getCategoriesList().then((value) { + setState(() { _categoryDropDownItems = value; + }); }); super.initState(); @@ -61,10 +65,7 @@ class ReceiptPageState extends State { focusNode = FocusNode(); _recurringController.text = "None"; - _categoryController.text = ""; -// getCategoriesStrings().then((value) { -// _categoryDropDownItems = value; -// }); + _categoryController.text = "None"; } @override @@ -184,22 +185,26 @@ class ReceiptPageState extends State { return "false"; } - Future> getCategoriesStrings() async { - var categories = getCategories(); //future> - var categoriesStrings = List(); + Future>> getCategoriesList() async { + //TODO: Return a list of [String, String] where {1} is categoryName and {2} is categoryValue for request + var categoriesList = List(); - categories.then((val) { - val.forEach((thisCategory) { + var categories = await getCategories(); //future> + + categories.forEach((thisCategory) { // print(thisCategory.name); - categoriesStrings.add(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()); - _categoryDropDownItems = categoriesStrings; - return categoriesStrings; - }); + return categoriesList; } List getRecurringOptions() { @@ -237,7 +242,7 @@ class ReceiptPageState extends State { return (components[2] + "-" + components[1] + "-" + components[0] + "T" + components[3] + ":" + components[4] + ":00.000+01:00"); - // Yes, there is probably a function to convert dates, but I didn't + // Yes, there is a function to convert dates, but I didn't // know that before writing this and it's done now so I'm keeping it. } @@ -299,6 +304,13 @@ class ReceiptPageState extends State { @override Widget build(BuildContext context) { +// _categoryDropDownItems = [ +// Map.fromIterable(["wappa dappa doo", "1"]), +// Map.fromIterable(["interesting flip flops", "2"]), +// "gray skies", value: "3"), +// "fortified systems" value: "4"), +// ]; + return PlatformScaffold( appBar: AppBar( @@ -521,15 +533,13 @@ class ReceiptPageState extends State { Padding( - padding: EdgeInsets.fromLTRB(0.0,18,0.0,0.0), + padding: EdgeInsets.fromLTRB(0.0,7,0.0,0.0), child : Container ( height: 35, // width: 400, - child : ListView( - scrollDirection: Axis.horizontal, - + child : Row( children: [ Container( padding: const EdgeInsets.fromLTRB(0, 7, 0, 8), @@ -543,23 +553,44 @@ class ReceiptPageState extends State { ), Container( - padding: const EdgeInsets.fromLTRB(29, 0, 0, 0), - child: DropdownButton( - value: _categoryController.text, - onChanged: (String newValue) { - setState(() { - _categoryController.text = newValue; - }); - }, - items: _categoryDropDownItems.map>((String value) { - return DropdownMenuItem( - value: value, - child: Text(value), - ); - }).toList(), // fix errors here by [items] being an empty container while _categoryDropDownItems is null - ) + 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; + }); + } + ), ), +// Container( +// padding: const EdgeInsets.fromLTRB(29, 0, 0, 0), +// child: DropdownButton( +// value: _categoryController.text, +// onChanged: (String newValue) { +// setState(() { +// _categoryController.text = newValue; +// }); +// }, +// items: _categoryDropDownItems.map>((String value) { +// return DropdownMenuItem( +// value: value, +// child: Text(value), +// ); +// }).toList(), // fix errors here by [items] being an empty container while _categoryDropDownItems is null +// ) +// ), + ], ), ),