2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-04-28 13:36:50 +00:00
inventree-app/lib/widget/refreshable_state.dart
Oliver 13ebaf43e1
List refactor (#179)
* Catch paginator bug if widget is disposed before request returns

* Refactoring paginated query widget

- Add option to enable / disable search filters

* Major refactor of paginated search widget

- Learned something new.. a state can access widget.<attribute>
- THIS CHANGES EVERTHING

* Preferences: Add code for tri-state values

- Also improve unit testing for preferences code

* Allow boolean form fields to be optionally tristate

* paginator: Allow custom boolean filters

* Remove outdated filtering preferences

* Refactor filter options

- Allow specification of more detailed options

* Add custom filters for "part" list

* filter tweaks

* Remove legacy "SublocationList" widget

* Add filtering option for locationlist

* Updates for stock location widget

* Refactor category display widget

* More widget refactoring

* Update main search widget

* Fix unit tests

* Improve filtering on BOM display page
2022-07-19 23:29:01 +10:00

124 lines
2.9 KiB
Dart

import "package:inventree/widget/back.dart";
import "package:inventree/widget/drawer.dart";
import "package:flutter/material.dart";
/*
* 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 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>();
// Storage for context once "Build" is called
late BuildContext? _context;
// Current tab index (used for widgets which display bottom tabs)
int tabIndex = 0;
// Bool indicator
bool loading = false;
bool get loaded => !loading;
// Update current tab selection
void onTabSelectionChanged(int index) {
setState(() {
tabIndex = index;
});
}
@override
void initState() {
super.initState();
WidgetsBinding.instance?.addPostFrameCallback((_) => onBuild(_context!));
}
// Function called after the widget is first build
Future<void> onBuild(BuildContext context) async {
refresh(context);
}
// Function to request data for this page
Future<void> request(BuildContext context) async {
return;
}
Future<void> refresh(BuildContext context) async {
setState(() {
loading = true;
});
await request(context);
setState(() {
loading = false;
});
}
@override
Widget build(BuildContext context) {
// Save the context for future use
_context = context;
return Scaffold(
key: refreshableKey,
appBar: buildAppBar(context, refreshableKey),
drawer: getDrawer(context),
body: Builder(
builder: (BuildContext context) {
return RefreshIndicator(
onRefresh: () async {
refresh(context);
},
child: getBody(context)
);
}
),
bottomNavigationBar: getBottomNavBar(context),
);
}
}