Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ProCategories extends StatefulWidget {
- final FirebaseUser fireBaseuser;
- ProCategories(this.fireBaseuser);
- get categories => List<Categories>();
- @override
- _ProCategoriesState createState() => new _ProCategoriesState(fireBaseuser);
- }
- class _ProCategoriesState extends State<ProCategories> {
- final FirebaseAuth _auth = FirebaseAuth.instance;
- final FirebaseUser fireBaseUser;
- User user;
- GlobalKey<AutoCompleteTextFieldState<Categories>> key = new GlobalKey();
- AutoCompleteTextField textField;
- String currentText = "";
- List<Categories> added = [];
- _ProCategoriesState(this.fireBaseUser);
- void _getUser() async {
- this.user = await getUser(fireBaseUser);
- }
- @override
- void initState() {
- textField = AutoCompleteTextField<Categories>
- (style: new TextStyle(
- color: Colors.white,
- fontSize: 16.0),
- decoration: new InputDecoration(
- suffixIcon: Container(
- width: 85.0,
- height: 60.0,
- color:Colors.green,
- child: new IconButton(
- icon: new Image.asset('assets/search_icon_ivory.png',color: Colors.white,
- height: 18.0,),
- onPressed: (){},
- ),
- ),
- fillColor: Colors.black,
- contentPadding: EdgeInsets.fromLTRB(10.0, 30.0, 10.0, 20.0),
- filled: true,
- hintText: 'What Do You Need Help With?',
- hintStyle: TextStyle(
- color: Colors.white
- )
- ),
- itemSubmitted: (item) {
- setState(() {
- currentText = item.autocompleteterm;
- added.add(item);
- currentText = "";
- });
- },
- submitOnSuggestionTap: true,
- clearOnSubmit: true,
- textChanged: (item) {
- currentText = item;
- },
- key: key,
- suggestions: CategoryViewModel.categories,
- textInputAction: TextInputAction.go,
- itemBuilder: (context, item) {
- return new Padding(
- padding: EdgeInsets.all(8.0), child: new Text(item.autocompleteterm),
- );
- },
- itemSorter: (a,b) {
- return a.autocompleteterm.compareTo(b.autocompleteterm);
- },
- itemFilter: (item, query){
- return item.autocompleteterm.toLowerCase().startsWith(query.toLowerCase());
- });
- super.initState();
- _getUser();
- }
- @override
- Widget build(BuildContext context) {
- Column body = new Column(
- children: <Widget>[
- ListTile(
- title: textField,
- trailing: new IconButton(
- icon: new Icon(Icons.add),
- onPressed: () {
- setState(() {
- if (currentText != "") {
- added.add(CategoryViewModel.categories.firstWhere((i) => i.autocompleteterm.toLowerCase().contains(currentText)));
- textField.clear();
- currentText = "";
- }
- });
- },
- ),
- )
- ],
- );
- body.children.addAll(added.map((item) {
- return ListTile(title: Text(item.autocompleteterm));
- }
- )
- );
- return Scaffold(
- resizeToAvoidBottomPadding: false,
- backgroundColor: Color(0xFF13212C),
- appBar: AppBar(
- title: Text('QuickCarl'),
- ),
- drawer: appDrawer(),
- body: new Center(
- child: new Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: <Widget>[
- new Column(
- children: <Widget>[
- textField,
- ]
- ),
- ]
- )
- )
- );
- }
- }
- ************
- // Services model class
- import 'dart:convert';
- import 'package:flutter/services.dart';
- class Categories {
- String serviceCategory;
- String servCategoryDesc;
- int id;
- String autocompleteterm;
- String category;
- String desc;
- Categories({
- this.serviceCategory,
- this.servCategoryDesc,
- this.id,
- this.autocompleteterm,
- this.category,
- this.desc
- });
- factory Categories.fromJson(Map<String, dynamic> parsedJson) {
- return Categories(
- serviceCategory: parsedJson['serviceCategory'] as String,
- servCategoryDesc: parsedJson['serviceCategoryDesc'] as String,
- id: parsedJson['serviceCategoryId'],
- autocompleteterm: parsedJson['autocompleteTerm'] as String,
- category: parsedJson['category'] as String,
- desc: parsedJson['description'] as String
- );
- }
- }
- class CategoryViewModel {
- static List<Categories> categories;
- static Future loadCategories() async {
- try {
- categories = new List<Categories>();
- String jsonString = await rootBundle.loadString('assets/services.json');
- Map parsedJson = json.decode(jsonString);
- var categoryJson = parsedJson['data'] as List;
- for (int i = 0; i < categoryJson.length; i++) {
- categories.add(new Categories.fromJson(categoryJson[i]));
- }
- } catch (e) {
- print(e);
- }
- }
- }
Add Comment
Please, Sign In to add comment