2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-06-15 03:35:28 +00:00

Looks like dropdown_search is the way to go!

This commit is contained in:
Oliver
2021-07-26 17:21:17 +10:00
parent ca4297ae6d
commit d07b704014
7 changed files with 97 additions and 87 deletions

View File

@ -93,7 +93,7 @@ class InvenTreeFileService extends FileService {
class InvenTreeAPI {
// Minimum required API version for server
static const _minApiVersion = 6;
static const _minApiVersion = 7;
// Endpoint for requesting an API token
static const _URL_GET_TOKEN = "user/token/";

View File

@ -1,15 +1,15 @@
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:dropdown_search/dropdown_search.dart';
import 'package:inventree/api.dart';
import 'package:inventree/app_colors.dart';
import 'package:inventree/widget/dialogs.dart';
import 'package:inventree/widget/fields.dart';
import 'package:inventree/l10.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:inventree/widget/snacks.dart';
import 'package:one_context/one_context.dart';
/*
@ -18,6 +18,8 @@ import 'package:one_context/one_context.dart';
*/
class APIFormField {
final _controller = TextEditingController();
// Constructor
APIFormField(this.name, this.data);
@ -96,16 +98,65 @@ class APIFormField {
// Construct an input for a related field
Widget _constructRelatedField() {
return AutocompleteFormField(
required ? label + "*" : label,
api_url,
filters: filters,
return DropdownSearch<dynamic>(
mode: Mode.BOTTOM_SHEET,
showSelectedItem: true,
onFind: (String filter) async {
Map<String, String> _filters = {};
if (filters != null) {
for (String key in filters) {
_filters[key] = filters[key].toString();
}
}
_filters["search"] = filter;
_filters["offset"] = "0";
_filters["limit"] = "25";
final APIResponse response = await InvenTreeAPI().get(
api_url,
params: _filters
);
if (response.isValid()) {
List<dynamic> results = [];
for (var result in response.data['results'] ?? []) {
results.add(result);
}
print("Results:");
print(results);
return results;
} else {
return [];
}
},
label: label,
hint: helpText,
renderer: null,
onChanged: print,
showClearButton: !required,
itemAsString: (dynamic item) {
return item['pathstring'];
},
isFilteredOnline: true,
showSearchBox: true,
compareFn: (dynamic item, dynamic selectedItem) {
if (item == null || selectedItem == null) {
return false;
}
return item['pk'] == selectedItem['pk'];
}
);
}
// Consturct a string input element
// Construct a string input element
Widget _constructString() {
return TextFormField(

View File

@ -116,82 +116,6 @@ class CheckBoxField extends FormField<bool> {
);
}
class AutocompleteFormField extends TypeAheadFormField {
final String label;
final _controller = TextEditingController();
final String url;
dynamic filters = {};
AutocompleteFormField(
this.label,
this.url,
{
this.filters,
Widget Function(dynamic)? renderer,
String? hint,
}) :
super(
textFieldConfiguration: TextFieldConfiguration(
autofocus: true,
decoration: InputDecoration(
hintText: hint,
border: OutlineInputBorder(),
),
),
suggestionsCallback: (String pattern) async {
Map<String, String> _filters = {};
if (filters != null) {
for (String key in filters) {
_filters[key] = filters[key].toString();
}
}
_filters["search"] = pattern;
_filters["offset"] = "0";
_filters["limit"] = "25";
final APIResponse response = await InvenTreeAPI().get(
url,
params: _filters
);
if (response.isValid()) {
List<dynamic> results = [];
for (var result in response.data['results'] ?? []) {
results.add(result);
}
return results;
} else {
return [];
}
},
itemBuilder: (context, suggestion) {
print("item builder: " + suggestion.toString());
return ListTile(
title: Text(suggestion['name']),
);
},
onSuggestionSelected: (suggestion) {
// TODO
},
onSaved: (value) {
// TODO
}
);
}
class StringField extends TextFormField {
StringField({String label = "", String? hint, String? initial, Function(String?)? onSaved, Function? validator, bool allowEmpty = false, bool isEnabled = true}) :