2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-14 11:15:26 +00:00

Major overhaul of "paginated list" widget class

- Simplify implementation
- Create mixin class for code reuse
- Allow custom app-bar
- Allow custom ordering / sorting options
- Improve code commenting / readability
This commit is contained in:
Oliver Walters
2022-07-06 20:24:40 +10:00
parent 979f950129
commit 7301243ed6
6 changed files with 143 additions and 134 deletions

View File

@ -3,7 +3,53 @@ import "package:inventree/widget/drawer.dart";
import "package:flutter/material.dart";
abstract class RefreshableState<T extends StatefulWidget> extends State<T> {
/*
* Simple mixin class which defines simple methods for defining widget properties
*/
mixin BaseWidgetProperties {
// Return a list of appBar actions (default = None)
List<Widget> getAppBarActions(BuildContext context) {
return [];
}
// Return a title for the appBar
String getAppBarTitle(BuildContext context) { return "--- app bar ---"; }
// 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) {
// Default return is an empty ListView
return ListView();
}
Widget? getBottomNavBar(BuildContext context) {
return null;
}
AppBar? buildAppBar(BuildContext context, GlobalKey<ScaffoldState> key) {
return AppBar(
title: Text(getAppBarTitle(context)),
actions: getAppBarActions(context),
leading: backButton(context, key),
);
}
}
/*
* Abstract base class which provides generic "refresh" functionality.
*
* - Drag down and release to 'refresh' the widget
* - Define some method which runs to 'refresh' the widget state
*/
abstract class RefreshableState<T extends StatefulWidget> extends State<T> with BaseWidgetProperties {
final refreshableKey = GlobalKey<ScaffoldState>();
@ -25,12 +71,6 @@ abstract class RefreshableState<T extends StatefulWidget> extends State<T> {
});
}
List<Widget> getAppBarActions(BuildContext context) {
return [];
}
String getAppBarTitle(BuildContext context) { return "App Bar Title"; }
@override
void initState() {
super.initState();
@ -60,34 +100,6 @@ abstract class RefreshableState<T extends StatefulWidget> extends State<T> {
});
}
// 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) {
// Default return is an empty ListView
return ListView();
}
Widget? getBottomNavBar(BuildContext context) {
return null;
}
Widget? getFab(BuildContext context) {
return null;
}
AppBar? buildAppBar(BuildContext context) {
return AppBar(
title: Text(getAppBarTitle(context)),
actions: getAppBarActions(context),
leading: backButton(context, refreshableKey),
);
}
@override
Widget build(BuildContext context) {
@ -96,9 +108,8 @@ abstract class RefreshableState<T extends StatefulWidget> extends State<T> {
return Scaffold(
key: refreshableKey,
appBar: buildAppBar(context),
appBar: buildAppBar(context, refreshableKey),
drawer: getDrawer(context),
floatingActionButton: getFab(context),
body: Builder(
builder: (BuildContext context) {
return RefreshIndicator(