Guest User

Untitled

a guest
Nov 29th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. class ProCategories extends StatefulWidget {
  2.  
  3. final FirebaseUser fireBaseuser;
  4.  
  5. ProCategories(this.fireBaseuser);
  6.  
  7. get categories => List<Categories>();
  8.  
  9. @override
  10. _ProCategoriesState createState() => new _ProCategoriesState(fireBaseuser);
  11. }
  12.  
  13. class _ProCategoriesState extends State<ProCategories> {
  14.  
  15. final FirebaseAuth _auth = FirebaseAuth.instance;
  16.  
  17. final FirebaseUser fireBaseUser;
  18.  
  19. User user;
  20.  
  21. GlobalKey<AutoCompleteTextFieldState<Categories>> key = new GlobalKey();
  22.  
  23. AutoCompleteTextField textField;
  24.  
  25. String currentText = "";
  26.  
  27. List<Categories> added = [];
  28.  
  29. _ProCategoriesState(this.fireBaseUser);
  30.  
  31. void _getUser() async {
  32. this.user = await getUser(fireBaseUser);
  33. }
  34.  
  35.  
  36. @override
  37. void initState() {
  38. textField = AutoCompleteTextField<Categories>
  39. (style: new TextStyle(
  40. color: Colors.white,
  41. fontSize: 16.0),
  42. decoration: new InputDecoration(
  43. suffixIcon: Container(
  44. width: 85.0,
  45. height: 60.0,
  46. color:Colors.green,
  47. child: new IconButton(
  48. icon: new Image.asset('assets/search_icon_ivory.png',color: Colors.white,
  49. height: 18.0,),
  50. onPressed: (){},
  51. ),
  52. ),
  53. fillColor: Colors.black,
  54. contentPadding: EdgeInsets.fromLTRB(10.0, 30.0, 10.0, 20.0),
  55. filled: true,
  56. hintText: 'What Do You Need Help With?',
  57. hintStyle: TextStyle(
  58. color: Colors.white
  59. )
  60. ),
  61. itemSubmitted: (item) {
  62. setState(() {
  63. currentText = item.autocompleteterm;
  64. added.add(item);
  65. currentText = "";
  66. });
  67. },
  68. submitOnSuggestionTap: true,
  69. clearOnSubmit: true,
  70. textChanged: (item) {
  71. currentText = item;
  72. },
  73. key: key,
  74. suggestions: CategoryViewModel.categories,
  75. textInputAction: TextInputAction.go,
  76. itemBuilder: (context, item) {
  77. return new Padding(
  78. padding: EdgeInsets.all(8.0), child: new Text(item.autocompleteterm),
  79. );
  80. },
  81. itemSorter: (a,b) {
  82. return a.autocompleteterm.compareTo(b.autocompleteterm);
  83. },
  84. itemFilter: (item, query){
  85. return item.autocompleteterm.toLowerCase().startsWith(query.toLowerCase());
  86. });
  87. super.initState();
  88. _getUser();
  89. }
  90.  
  91. @override
  92. Widget build(BuildContext context) {
  93. Column body = new Column(
  94. children: <Widget>[
  95. ListTile(
  96. title: textField,
  97. trailing: new IconButton(
  98. icon: new Icon(Icons.add),
  99. onPressed: () {
  100. setState(() {
  101. if (currentText != "") {
  102. added.add(CategoryViewModel.categories.firstWhere((i) => i.autocompleteterm.toLowerCase().contains(currentText)));
  103. textField.clear();
  104. currentText = "";
  105. }
  106. });
  107. },
  108. ),
  109. )
  110. ],
  111. );
  112. body.children.addAll(added.map((item) {
  113. return ListTile(title: Text(item.autocompleteterm));
  114. }
  115. )
  116. );
  117.  
  118.  
  119. return Scaffold(
  120. resizeToAvoidBottomPadding: false,
  121. backgroundColor: Color(0xFF13212C),
  122. appBar: AppBar(
  123. title: Text('QuickCarl'),
  124. ),
  125. drawer: appDrawer(),
  126. body: new Center(
  127. child: new Column(
  128. crossAxisAlignment: CrossAxisAlignment.stretch,
  129. children: <Widget>[
  130. new Column(
  131. children: <Widget>[
  132. textField,
  133. ]
  134. ),
  135. ]
  136. )
  137. )
  138. );
  139. }
  140. }
  141.  
  142. ************
  143.  
  144. // Services model class
  145.  
  146. import 'dart:convert';
  147.  
  148. import 'package:flutter/services.dart';
  149.  
  150. class Categories {
  151. String serviceCategory;
  152. String servCategoryDesc;
  153. int id;
  154. String autocompleteterm;
  155. String category;
  156. String desc;
  157.  
  158. Categories({
  159. this.serviceCategory,
  160. this.servCategoryDesc,
  161. this.id,
  162. this.autocompleteterm,
  163. this.category,
  164. this.desc
  165. });
  166.  
  167. factory Categories.fromJson(Map<String, dynamic> parsedJson) {
  168. return Categories(
  169. serviceCategory: parsedJson['serviceCategory'] as String,
  170. servCategoryDesc: parsedJson['serviceCategoryDesc'] as String,
  171. id: parsedJson['serviceCategoryId'],
  172. autocompleteterm: parsedJson['autocompleteTerm'] as String,
  173. category: parsedJson['category'] as String,
  174. desc: parsedJson['description'] as String
  175. );
  176. }
  177. }
  178.  
  179. class CategoryViewModel {
  180. static List<Categories> categories;
  181.  
  182. static Future loadCategories() async {
  183. try {
  184. categories = new List<Categories>();
  185. String jsonString = await rootBundle.loadString('assets/services.json');
  186. Map parsedJson = json.decode(jsonString);
  187. var categoryJson = parsedJson['data'] as List;
  188. for (int i = 0; i < categoryJson.length; i++) {
  189. categories.add(new Categories.fromJson(categoryJson[i]));
  190. }
  191. } catch (e) {
  192. print(e);
  193. }
  194. }
  195. }
Add Comment
Please, Sign In to add comment