From 2797bc19124c5fd3b0712b4a232fbdef3c44152b Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 2 Apr 2020 10:30:27 +1100 Subject: [PATCH] Request list of parts from the server, and display the results - Also allow custom filtering of the returned results! --- lib/main.dart | 126 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 109 insertions(+), 17 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 2510f707..f5d7d697 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,10 +1,12 @@ +import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; // import 'package:preferences/preferences.dart'; import 'settings.dart'; import 'api.dart'; import 'preferences.dart'; -import 'inventree_object.dart'; + +import 'package:InvenTree/inventree/part.dart'; void main() async { @@ -14,22 +16,7 @@ void main() async { String password = "password"; String server = "http://127.0.0.1:8000"; - await InvenTreeAPI().connect(server, username, password); - - print("Connected! Requesting data"); - - InvenTreePart().list(filters: {"category": "2"}).then((var parts) { - - print("Received list"); - - print("Found " + parts.length.toString() + " parts"); - - for (var part in parts) { - if (part is InvenTreePart) { - print("Part: " + part.name + ", " + part.description); - } - } - }); + InvenTreeAPI().connect(server, username, password); runApp(MyApp()); } @@ -58,9 +45,39 @@ class MyApp extends StatelessWidget { } } + +class ProductList extends StatelessWidget { + final List _parts; + + ProductList(this._parts); + + Widget _buildPart(BuildContext context, int index) { + InvenTreePart part; + + if (index < _parts.length) { + part = _parts[index]; + } + + return Card( + child: Column( + children: [ + Text('${part.name} - ${part.description}'), + ] + ) + ); + } + + @override + Widget build(BuildContext context) { + return ListView.builder(itemBuilder: _buildPart, itemCount: _parts.length); + } +} + + class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); + // This widget is the home page of your application. It is stateful, meaning // that it has a State object (defined below) that contains fields that affect // how it looks. @@ -79,6 +96,69 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { int _counter = 0; + // List of parts + List _parts = List(); + + String _filter = ''; + + void _filterParts(String text) { + // Filtering is case-insensitive + _filter = text.trim().toLowerCase(); + + // Update state + setState(() {}); + } + + List get parts { + + if (_filter.isNotEmpty) { + + List filtered = List(); + for (var part in _parts) { + + var name = part.name.toLowerCase() + ' ' + part.description.toLowerCase(); + + bool match = true; + + for (var txt in _filter.split(' ')) { + if (!name.contains(txt)) { + match = false; + break; + } + } + + if (match) { + filtered.add(part); + } + } + + return filtered; + } else { + + // No filtering + return _parts; + } + } + + _MyHomePageState() : super() { + + // Request list of parts from the server, + // and display the results when they are received + InvenTreePart().list(filters: {"search": "0805"}).then((var parts) { + _parts.clear(); + + for (var part in parts) { + if (part is InvenTreePart) { + _parts.add(part); + } + + // Update state! + setState(() { + }); + } + }); + } + void _incrementCounter() { setState(() { // This call to setState tells the Flutter framework that something has @@ -153,6 +233,14 @@ class _MyHomePageState extends State { // horizontal). mainAxisAlignment: MainAxisAlignment.center, children: [ + TextField( + decoration: InputDecoration( + hintText: 'Filter Results', + ), + onChanged: (text) { + _filterParts(text); + }, + ), Text( 'You have pushed the button this many times:', ), @@ -160,6 +248,10 @@ class _MyHomePageState extends State { '$_counter', style: Theme.of(context).textTheme.display1, ), + Text( + 'hello world', + ), + Expanded(child: ProductList(parts)), ], ), ),