mirror of
https://github.com/inventree/inventree-app.git
synced 2025-04-29 05:56:47 +00:00
Make search widget "refreshable"
- Return results for Part - Return results for StockItem
This commit is contained in:
parent
ad1997cf22
commit
83ac6041cb
@ -182,7 +182,7 @@ class InvenTreeModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return list of objects from the database, with optional filters
|
// Return list of objects from the database, with optional filters
|
||||||
Future<List<InvenTreeModel>> list(BuildContext context, {Map<String, String> filters}) async {
|
Future<List<InvenTreeModel>> list(BuildContext context, {Map<String, String> filters, bool dialog=true}) async {
|
||||||
|
|
||||||
if (filters == null) {
|
if (filters == null) {
|
||||||
filters = {};
|
filters = {};
|
||||||
@ -201,18 +201,25 @@ class InvenTreeModel {
|
|||||||
// TODO - Add "timeout"
|
// TODO - Add "timeout"
|
||||||
// TODO - Add error catching
|
// TODO - Add error catching
|
||||||
|
|
||||||
showProgressDialog(context, "Requesting Data", "Requesting ${NAME} data from server");
|
if (dialog) {
|
||||||
|
showProgressDialog(context, "Requesting Data", "Requesting ${NAME} data from server");
|
||||||
|
}
|
||||||
|
|
||||||
var response = await api.get(URL, params:params)
|
var response = await api.get(URL, params:params)
|
||||||
.timeout(Duration(seconds: 10))
|
.timeout(Duration(seconds: 10))
|
||||||
.catchError((e) {
|
.catchError((e) {
|
||||||
|
|
||||||
hideProgressDialog(context);
|
if (dialog) {
|
||||||
|
hideProgressDialog(context);
|
||||||
|
}
|
||||||
|
|
||||||
if (e is TimeoutException) {
|
if (e is TimeoutException) {
|
||||||
showErrorDialog(context, "Timeout", "No response from server");
|
if (dialog) {
|
||||||
|
showErrorDialog(context, "Timeout", "No response from server");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
showErrorDialog(context, "Error", e.toString());
|
// Re-throw the error
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@ -222,7 +229,9 @@ class InvenTreeModel {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
hideProgressDialog(context);
|
if (dialog) {
|
||||||
|
hideProgressDialog(context);
|
||||||
|
}
|
||||||
|
|
||||||
// A list of "InvenTreeModel" items
|
// A list of "InvenTreeModel" items
|
||||||
List<InvenTreeModel> results = new List<InvenTreeModel>();
|
List<InvenTreeModel> results = new List<InvenTreeModel>();
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import 'package:InvenTree/api.dart';
|
import 'package:InvenTree/api.dart';
|
||||||
import 'package:InvenTree/inventree/stock.dart';
|
import 'package:InvenTree/inventree/stock.dart';
|
||||||
import 'package:InvenTree/preferences.dart';
|
import 'package:InvenTree/preferences.dart';
|
||||||
import 'package:InvenTree/widget/drawer.dart';
|
|
||||||
import 'package:InvenTree/widget/stock_detail.dart';
|
import 'package:InvenTree/widget/stock_detail.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
|
|
||||||
import 'package:InvenTree/widget/drawer.dart';
|
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||||
|
|
||||||
|
import 'package:InvenTree/widget/refreshable_state.dart';
|
||||||
|
import 'package:InvenTree/inventree/part.dart';
|
||||||
|
import 'package:InvenTree/inventree/stock.dart';
|
||||||
|
|
||||||
|
|
||||||
class SearchWidget extends StatefulWidget {
|
class SearchWidget extends StatefulWidget {
|
||||||
|
|
||||||
@ -10,24 +15,81 @@ class SearchWidget extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class _SearchState extends State<SearchWidget> {
|
class _SearchState extends RefreshableState<SearchWidget> {
|
||||||
|
|
||||||
|
String _searchText = "";
|
||||||
|
|
||||||
|
List<InvenTreePart> _parts = List<InvenTreePart>();
|
||||||
|
List<InvenTreeStockItem> _stockItems = List<InvenTreeStockItem>();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
String getAppBarTitle(BuildContext context) {
|
||||||
|
return "Search";
|
||||||
|
}
|
||||||
|
|
||||||
return Scaffold(
|
Future<void> _search(BuildContext context) {
|
||||||
appBar: AppBar(
|
print("Search: $_searchText}");
|
||||||
title: Text("Search"),
|
|
||||||
),
|
|
||||||
drawer: new InvenTreeDrawer(context),
|
|
||||||
body: Center(
|
|
||||||
child: ListView(
|
|
||||||
children: <Widget>[
|
|
||||||
|
|
||||||
],
|
// Ignore if the search text is empty
|
||||||
)
|
if (_searchText.isNotEmpty) {
|
||||||
|
|
||||||
|
// Search for parts
|
||||||
|
InvenTreePart().list(context, filters: {"search": _searchText}).then((var parts) {
|
||||||
|
setState(() {
|
||||||
|
_parts.clear();
|
||||||
|
for (var part in parts) {
|
||||||
|
if (part is InvenTreePart) {
|
||||||
|
_parts.add(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print("Matched ${_parts.length} parts");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Search for stock items
|
||||||
|
InvenTreeStockItem().list(context, filters: {"search": _searchText}).then((var items) {
|
||||||
|
setState(() {
|
||||||
|
_stockItems.clear();
|
||||||
|
for (var item in items) {
|
||||||
|
if (item is InvenTreeStockItem) {
|
||||||
|
_stockItems.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print("Matched ${_stockItems.length} stock items");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> request(BuildContext context) async {
|
||||||
|
_search(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget getBody(BuildContext context) {
|
||||||
|
|
||||||
|
return Center(
|
||||||
|
child: ListView(
|
||||||
|
children: <Widget>[
|
||||||
|
TextField(
|
||||||
|
decoration: InputDecoration(
|
||||||
|
hintText: "Search"
|
||||||
|
),
|
||||||
|
onChanged: (String text) {
|
||||||
|
_searchText = text;
|
||||||
|
}
|
||||||
|
),
|
||||||
|
RaisedButton(
|
||||||
|
child: Text("Search"),
|
||||||
|
onPressed: () {
|
||||||
|
_search(context);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user