Skip to content

Instantly share code, notes, and snippets.

@DK15
Created November 27, 2018 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DK15/b15f60127d705065d8b435c79a0964c1 to your computer and use it in GitHub Desktop.
Save DK15/b15f60127d705065d8b435c79a0964c1 to your computer and use it in GitHub Desktop.
search with autocomplete
class ProCategories extends StatefulWidget {
final FirebaseUser fireBaseuser;
ProCategories(this.fireBaseuser, this.categories);
final List<Categories> categories;
@override
_ProCategoriesState createState() => new _ProCategoriesState(fireBaseuser);
}
class _ProCategoriesState extends State<ProCategories> {
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseUser fireBaseUser;
User user;
List<Categories> added = [];
TextEditingController controller = new TextEditingController();
String currentText = "";
GlobalKey<AutoCompleteTextFieldState<String>> key = new GlobalKey();
AutoCompleteTextField textField;
_ProCategoriesState(this.fireBaseUser);
// Get json result and convert it to model. Then add
Future<String> getUserDetails() async {
String jsonData = await DefaultAssetBundle.of(context).loadString(
'assets/services.json');
Map data = json.decode(jsonData);
print(data);
setState(() {
final List<Categories> items = (data['data'] as List).map((
i) => new Categories.fromJson(i)).toList();
for (final item in items) {
print(item.autocompleteterm);
}
});
}
@override
void initState() {
textField = new AutoCompleteTextField<String>
(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: new EdgeInsets.fromLTRB(10.0, 30.0, 10.0, 20.0),
filled: true,
hintText: 'What Do You Need Help With ?',
hintStyle: new TextStyle(
color: Colors.white
)
),
submitOnSuggestionTap: true,
clearOnSubmit: true,
textChanged: (item) {
currentText = item;
},
textSubmitted: (item) {
setState(() {
currentText = item;
});
},
key: key,
suggestions: widget.categories, // <---- getting error here : argument type 'List<Categories>' can't be assigned to parameter type 'List<String>'
itemBuilder: (context, item) {
return new Padding(
padding: EdgeInsets.all(8.0),
child: new Text(item.autocompleteterm)); // <-- getter 'autocompleteterm' isn't defined for the class 'String'
},
itemSorter: (a, b) {
return a.compareTo(b);
},
itemFilter: (item, query) {
return item.toLowerCase().startsWith(query.toLowerCase());
}
);
super.initState();
}
@override
Widget build(BuildContext context) {
Column body = new Column(children: [
new GestureDetector(
child: new ListTile(
title: textField,
onTap: () {
setState(() {
if (currentText != "") {
added.add(widget.categories.firstWhere((i) =>
i.autocompleteterm.toLowerCase().contains(
currentText)));
textField.clear();
currentText = "";
}
});
}
)
)
]
);
body.children.addAll(added.map((item) {
return new ListTile(
title: new Text(item.autocompleteterm),
subtitle: Text(item.serviceCategory),
);
}));
return Scaffold(
resizeToAvoidBottomPadding: false,
backgroundColor: Color(0xFF13212C),
appBar: AppBar(
title: Text(''),
),
body: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Column(
children: <Widget>[
textField,
]
),
new ListView(shrinkWrap: true,
padding: const EdgeInsets.all(10.0),
children: <Widget>[
],
),
]
)
)
);
}
}
//////////
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
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment