diff --git a/lib/inventree/company.dart b/lib/inventree/company.dart index 31a1d14c..9dbd5c41 100644 --- a/lib/inventree/company.dart +++ b/lib/inventree/company.dart @@ -10,6 +10,8 @@ class InvenTreeCompany extends InvenTreeModel { InvenTreeCompany() : super(); + String get image => jsondata['image'] ?? ''; + InvenTreeCompany.fromJson(Map json) : super.fromJson(json) { // TODO } diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 38680ba7..7b62ac0e 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -172,7 +172,16 @@ class InvenTreeModel { // TODO - Define a 'save' / 'update' function // Override this function for each sub-class - bool matchAgainstString(String filter) => false; + bool matchAgainstString(String filter) { + // Default implementation matches name and description + // Override this behaviour in sub-class if required + + if (name.toLowerCase().contains(filter)) return true; + if (description.toLowerCase().contains(filter)) return true; + + // No matches! + return false; + } // Filter this item against a list of provided filters // Each filter must be matched diff --git a/lib/inventree/stock.dart b/lib/inventree/stock.dart index 60aaaecd..2365b74b 100644 --- a/lib/inventree/stock.dart +++ b/lib/inventree/stock.dart @@ -195,13 +195,4 @@ class InvenTreeStockLocation extends InvenTreeModel { return loc; } - @override - bool matchAgainstString(String filter) { - - if (name.toLowerCase().contains(filter)) return true; - - if (description.toLowerCase().contains(filter)) return true; - - return false; - } } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 553df335..8967d61a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:InvenTree/inventree/stock.dart'; import 'package:InvenTree/widget/category_display.dart'; +import 'package:InvenTree/widget/company_list.dart'; import 'package:InvenTree/widget/location_display.dart'; import 'package:InvenTree/widget/drawer.dart'; import 'package:flutter/cupertino.dart'; @@ -207,12 +208,17 @@ class _MyHomePageState extends State { } void _stock() { - if (!InvenTreeAPI().checkConnection(context)) return; Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(null))); } + void _suppliers() { + if (!InvenTreeAPI().checkConnection(context)) return; + + Navigator.push(context, MaterialPageRoute(builder: (context) => CompanyListWidget())); + } + void _unsupported() { showDialog( context: context, @@ -311,7 +317,7 @@ class _MyHomePageState extends State { IconButton( icon: new FaIcon(FontAwesomeIcons.industry), tooltip: 'Suppliers', - onPressed: _unsupported, + onPressed: _suppliers, ), Text("Suppliers"), ] diff --git a/lib/widget/company_list.dart b/lib/widget/company_list.dart new file mode 100644 index 00000000..e55b90d5 --- /dev/null +++ b/lib/widget/company_list.dart @@ -0,0 +1,106 @@ + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import 'package:InvenTree/api.dart'; +import 'package:InvenTree/inventree/company.dart'; +import 'package:InvenTree/widget/drawer.dart'; + +class CompanyListWidget extends StatefulWidget { + + @override + _CompanyListState createState() => _CompanyListState(); + +} + + +class _CompanyListState extends State { + + var _companies = new List(); + + var _filteredCompanies = new List(); + + var _title = "Companies"; + + _CompanyListState() { + _requestData(); + } + + void _requestData() { + + InvenTreeCompany().list().then((var companies) { + + _companies.clear(); + + for (var c in companies) { + if (c is InvenTreeCompany) { + _companies.add(c); + } + } + + setState(() { + _filterResults(""); + }); + + }); + } + + void _filterResults(String text) { + + if (text.isEmpty) { + _filteredCompanies = _companies; + } else { + _filteredCompanies = _companies.where((c) => c.filter(text)).toList(); + } + } + + Widget _showCompany(BuildContext context, int index) { + + InvenTreeCompany company = _filteredCompanies[index]; + + return ListTile( + title: Text("${company.name}"), + subtitle: Text("${company.description}"), + leading: Image( + image: InvenTreeAPI().getImage(company.image), + width: 40, + ), + onTap: () { + if (company.pk > 0) { + InvenTreeCompany().get(company.pk).then((var c) { + print("Retrieved company: ${c.name}"); + }); + } + }, + ); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text("$_title"), + ), + drawer: new InvenTreeDrawer(context), + body: ListView( + children: [ + TextField( + decoration: InputDecoration( + hintText: 'Filter results', + ), + onChanged: (String text) { + setState(() { + _filterResults(text); + }); + }, + ), + ListView.builder( + shrinkWrap: true, + physics: ClampingScrollPhysics(), + itemBuilder: _showCompany, itemCount: _filteredCompanies.length) + ], + ) + ); + } + +} \ No newline at end of file