From 58f6fd0f15f93034140e8556038a0266d05133da Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 15 Apr 2020 11:39:29 +1000 Subject: [PATCH] Make a 'refreshable_state' class which makes API requests easier --- lib/widget/location_display.dart | 161 ++++++++++++------------------ lib/widget/refreshable_state.dart | 61 +++++++++++ 2 files changed, 123 insertions(+), 99 deletions(-) create mode 100644 lib/widget/refreshable_state.dart diff --git a/lib/widget/location_display.dart b/lib/widget/location_display.dart index 6da793a8..7bb71d7c 100644 --- a/lib/widget/location_display.dart +++ b/lib/widget/location_display.dart @@ -8,10 +8,10 @@ import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +import 'package:InvenTree/widget/refreshable_state.dart'; + class LocationDisplayWidget extends StatefulWidget { - - LocationDisplayWidget(this.location, {Key key}) : super(key: key); final InvenTreeStockLocation location; @@ -22,21 +22,15 @@ class LocationDisplayWidget extends StatefulWidget { _LocationDisplayState createState() => _LocationDisplayState(location); } - -class _LocationDisplayState extends State { - - BuildContext context; - - _LocationDisplayState(this.location) { - } - - void initState() { - super.initState(); - WidgetsBinding.instance.addPostFrameCallback((_) => _requestData(context)); - } +class _LocationDisplayState extends RefreshableState { final InvenTreeStockLocation location; + @override + String app_bar_title = "Stock Location"; + + _LocationDisplayState(this.location) {} + List _sublocations = List(); String _locationFilter = ''; @@ -52,27 +46,8 @@ class _LocationDisplayState extends State { List _items = List(); - String get _title { - - if (location == null) { - return "Stock Locations"; - } else { - return "Stock Location - ${location.name}"; - } - } - - Future _refresh() async { - await _requestData(context); - } - - /* - * Request data from the server. - * It will be displayed once loaded - * - * - List of sublocations under this one - * - List of stock items at this location - */ - Future _requestData(BuildContext context) async { + @override + Future request(BuildContext context) async { int pk = location?.pk ?? -1; @@ -146,75 +121,63 @@ class _LocationDisplayState extends State { } @override - Widget build(BuildContext context) { + Widget getBody(BuildContext context) { - // Save the context - this.context = context; - - return Scaffold( - appBar: AppBar( - title: Text(_title), - ), - drawer: new InvenTreeDrawer(context), - body: new RefreshIndicator( - onRefresh: _refresh, - child: ListView( - children: [ - locationDescriptionCard(), - ExpansionPanelList( - expansionCallback: (int index, bool isExpanded) { - setState(() { - switch (index) { - case 0: - InvenTreePreferences().expandLocationList = !isExpanded; - break; - case 1: - InvenTreePreferences().expandStockList = !isExpanded; - break; - default: - break; - } - }); + return ListView( + children: [ + locationDescriptionCard(), + ExpansionPanelList( + expansionCallback: (int index, bool isExpanded) { + setState(() { + switch (index) { + case 0: + InvenTreePreferences().expandLocationList = !isExpanded; + break; + case 1: + InvenTreePreferences().expandStockList = !isExpanded; + break; + default: + break; + } + }); + }, + children: [ + ExpansionPanel( + headerBuilder: (BuildContext context, bool isExpanded) { + return ListTile( + title: Text("Sublocations"), + leading: FaIcon(FontAwesomeIcons.mapMarkerAlt), + trailing: Text("${_sublocations.length}"), + onTap: () { + setState(() { + InvenTreePreferences().expandLocationList = !InvenTreePreferences().expandLocationList; + }); + }, + ); }, - children: [ - ExpansionPanel( - headerBuilder: (BuildContext context, bool isExpanded) { - return ListTile( - title: Text("Sublocations"), - leading: FaIcon(FontAwesomeIcons.mapMarkerAlt), - trailing: Text("${_sublocations.length}"), - onTap: () { - setState(() { - InvenTreePreferences().expandLocationList = !InvenTreePreferences().expandLocationList; - }); - }, - ); - }, - body: SublocationList(_sublocations), - isExpanded: InvenTreePreferences().expandLocationList && _sublocations.length > 0, - ), - ExpansionPanel( - headerBuilder: (BuildContext context, bool isExpanded) { - return ListTile( - title: Text("Stock Items"), - leading: FaIcon(FontAwesomeIcons.boxes), - trailing: Text("${_items.length}"), - onTap: () { - setState(() { - InvenTreePreferences().expandStockList = !InvenTreePreferences().expandStockList; - }); - }, - ); - }, - body: StockList(_items), - isExpanded: InvenTreePreferences().expandStockList && _items.length > 0, - ) - ] + body: SublocationList(_sublocations), + isExpanded: InvenTreePreferences().expandLocationList && _sublocations.length > 0, ), + ExpansionPanel( + headerBuilder: (BuildContext context, bool isExpanded) { + return ListTile( + title: Text("Stock Items"), + leading: FaIcon(FontAwesomeIcons.boxes), + trailing: Text("${_items.length}"), + onTap: () { + setState(() { + InvenTreePreferences().expandStockList = !InvenTreePreferences().expandStockList; + }); + }, + ); + }, + body: StockList(_items), + isExpanded: InvenTreePreferences().expandStockList && _items.length > 0, + ) ] - ) - ) + ), + ] ); } } diff --git a/lib/widget/refreshable_state.dart b/lib/widget/refreshable_state.dart new file mode 100644 index 00000000..426254d6 --- /dev/null +++ b/lib/widget/refreshable_state.dart @@ -0,0 +1,61 @@ +import 'package:InvenTree/widget/drawer.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; + +import 'package:InvenTree/widget/drawer.dart'; + + +abstract class RefreshableState extends State { + + // Storage for context once "Build" is called + BuildContext context; + + String app_bar_title = "App Bar Title"; + + void initState() { + super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) => request(context)); + } + + // Function to request data for this page + Future request(BuildContext context) async { + return; + } + + Future refresh() async { + await request(context); + setState(() {}); + } + + // Function to construct an appbar (override if needed) + AppBar getAppBar(BuildContext context) { + return AppBar( + title: Text(app_bar_title) + ); + } + + // Function to construct a drawer (override if needed) + Widget getDrawer(BuildContext context) { + return InvenTreeDrawer(context); + } + + // Function to construct a body (MUST BE PROVIDED) + Widget getBody(BuildContext context); + + @override + Widget build(BuildContext context) { + + // Save the context for future use + this.context = context; + + return Scaffold( + appBar: getAppBar(context), + drawer: getDrawer(context), + body: RefreshIndicator( + onRefresh: refresh, + child: getBody(context) + ) + ); + } +} \ No newline at end of file