Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. class Item {
  2. String _weight;
  3. String _name;
  4. String _id;
  5.  
  6. Item(this._weight, this._name, this._id);
  7.  
  8. Item.fromJson(Map<String, dynamic> m) {
  9. _weight = m['weight'] as String;
  10. _name = m['name'] as String;
  11. _id = m['id'] as String;
  12. }
  13.  
  14. String get id => _id;
  15.  
  16. String get name => _name;
  17.  
  18. String get weight => _weight;
  19.  
  20. Map<String, dynamic> toJson() => {
  21. 'weight': _weight,
  22. 'name': _name,
  23. 'id': _id,
  24. };
  25. }
  26.  
  27. mixin ListItem on Model {
  28. String itemKey = 'itemKey';
  29. List<Item> _items = [];
  30.  
  31. List<Item> get items {
  32. return List.from(_items);
  33. }
  34.  
  35. Future<Null> readList() async {
  36. final SharedPreferences prefs = await SharedPreferences.getInstance();
  37.  
  38. final data = json.decode(prefs.getString(itemKey).toString());
  39. final item = List<Item>.from(data.map((i) => Item.fromJson(i)));
  40.  
  41. _items = item;
  42. print(jsonDecode(prefs.getString(itemKey)));
  43. notifyListeners();
  44. }
  45.  
  46. Future<Null> addItem({String id, String name, String weight}) async {
  47. final SharedPreferences prefs = await SharedPreferences.getInstance();
  48. final Item item = Item(
  49. id,
  50. name,
  51. weight,
  52. );
  53. _items.add(item);
  54. prefs.setString(itemKey, jsonEncode(_items));
  55. notifyListeners();
  56. }
  57.  
  58. Future<Null> deleteProduct() async {
  59. notifyListeners();
  60. }
  61. }
  62.  
  63. class _ListItemsState extends State<ListItems> {
  64. final MainModel _model = MainModel();
  65.  
  66. final TextEditingController controller = TextEditingController();
  67.  
  68. @override
  69. void initState() {
  70. _model.readList();
  71. super.initState();
  72. }
  73.  
  74. @override
  75. Widget build(BuildContext context) {
  76. return ScopedModelDescendant<MainModel>(
  77. builder: (BuildContext context, Widget child, MainModel model) {
  78. return Scaffold(
  79. appBar: AppBar(),
  80. body: CustomScrollView(
  81. slivers: <Widget>[
  82. SliverList(
  83. delegate: SliverChildListDelegate([
  84. TextField(
  85. controller: controller,
  86. ),
  87. FlatButton(
  88. child: Text('Submit'),
  89. onPressed: () {
  90. model.addItem(
  91. id: controller.text,
  92. name: controller.text,
  93. weight: controller.text,
  94. );
  95. },
  96. ),
  97. ]),
  98. ),
  99. model.items.length > 0
  100. ? SliverList(
  101. delegate: SliverChildBuilderDelegate(
  102. (BuildContext context, int index) {
  103. return Dismissible(
  104. key: Key(model.items[index].id),
  105. background: Container(
  106. color: Colors.redAccent,
  107. ),
  108. onDismissed: (DismissDirection direction) {
  109. model.deleteProduct();
  110. },
  111. child: ListTile(
  112. leading: Text(model.items[index].name),
  113. trailing: Text(model.items[index].weight),
  114. onTap: () {},
  115. ),
  116. );
  117. },
  118. childCount: model.items.length,
  119. ),
  120. )
  121. : SliverFillRemaining(),
  122. ],
  123. ));
  124. },
  125. );
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement