|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:math'; |
| 3 | + |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter_plugin_example/data/base/api_response.dart'; |
| 6 | +import 'package:flutter_plugin_example/data/model/diet_plan.dart'; |
| 7 | +import 'package:flutter_plugin_example/data/repositories/diet_plan/contract_provider_diet_plan.dart'; |
| 8 | +import 'package:parse_server_sdk/parse_server_sdk.dart'; |
| 9 | + |
| 10 | +class HomePage extends StatefulWidget { |
| 11 | + const HomePage(this._dietPlanProvider); |
| 12 | + |
| 13 | + final DietPlanProviderContract _dietPlanProvider; |
| 14 | + |
| 15 | + @override |
| 16 | + _HomePageState createState() => _HomePageState(); |
| 17 | +} |
| 18 | + |
| 19 | +class _HomePageState extends State<HomePage> { |
| 20 | + List<DietPlan> randomDietPlans = <DietPlan>[]; |
| 21 | + |
| 22 | + @override |
| 23 | + void initState() { |
| 24 | + super.initState(); |
| 25 | + final List<dynamic> json = const JsonDecoder().convert(dietPlansToAdd); |
| 26 | + for (final Map<String, dynamic> element in json) { |
| 27 | + final DietPlan dietPlan = DietPlan().fromJson(element); |
| 28 | + randomDietPlans.add(dietPlan); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + Widget build(BuildContext context) { |
| 34 | + return WillPopScope( |
| 35 | + onWillPop: () async => false, |
| 36 | + child: Scaffold( |
| 37 | + appBar: AppBar( |
| 38 | + automaticallyImplyLeading: false, |
| 39 | + title: const Text('Parse Server demo'), |
| 40 | + actions: <Widget>[ |
| 41 | + FlatButton( |
| 42 | + child: Text('Logout', |
| 43 | + style: TextStyle(fontSize: 17.0, color: Colors.white)), |
| 44 | + onPressed: () async { |
| 45 | + final ParseUser user = await ParseUser.currentUser(); |
| 46 | + user.logout(deleteLocalUserData: true); |
| 47 | + Navigator.pop(context, true); |
| 48 | + }) |
| 49 | + ], |
| 50 | + ), |
| 51 | + body: _showDietList(), |
| 52 | + floatingActionButton: FloatingActionButton( |
| 53 | + onPressed: () async { |
| 54 | + final DietPlan dietPlan = |
| 55 | + randomDietPlans[Random().nextInt(randomDietPlans.length - 1)]; |
| 56 | + final ParseUser user = await ParseUser.currentUser(); |
| 57 | + dietPlan.set('user', user); |
| 58 | + await widget._dietPlanProvider.add(dietPlan); |
| 59 | + setState(() {}); |
| 60 | + }, |
| 61 | + tooltip: 'Add Diet Plans', |
| 62 | + child: const Icon(Icons.add), |
| 63 | + )), |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + Widget _showDietList() { |
| 68 | + return FutureBuilder<ApiResponse>( |
| 69 | + future: widget._dietPlanProvider.getAll(), |
| 70 | + builder: (BuildContext context, AsyncSnapshot<ApiResponse> snapshot) { |
| 71 | + if (snapshot.hasData) { |
| 72 | + if (snapshot.data.success) { |
| 73 | + if (snapshot.data.results == null || |
| 74 | + snapshot.data.results.isEmpty) { |
| 75 | + return Center( |
| 76 | + child: const Text('No Data'), |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + return ListView.builder( |
| 81 | + shrinkWrap: true, |
| 82 | + itemCount: snapshot.data.results.length, |
| 83 | + itemBuilder: (BuildContext context, int index) { |
| 84 | + final DietPlan dietPlan = snapshot.data.results[index]; |
| 85 | + final String id = dietPlan.objectId; |
| 86 | + final String name = dietPlan.name; |
| 87 | + final String description = dietPlan.description; |
| 88 | + final bool status = dietPlan.status; |
| 89 | + return Dismissible( |
| 90 | + key: Key(id), |
| 91 | + background: Container(color: Colors.red), |
| 92 | + onDismissed: (DismissDirection direction) async { |
| 93 | + widget._dietPlanProvider.remove(dietPlan); |
| 94 | + }, |
| 95 | + child: ListTile( |
| 96 | + title: Text( |
| 97 | + name, |
| 98 | + style: TextStyle(fontSize: 20.0), |
| 99 | + ), |
| 100 | + subtitle: Text(description), |
| 101 | + trailing: IconButton( |
| 102 | + icon: status |
| 103 | + ? const Icon( |
| 104 | + Icons.done_outline, |
| 105 | + color: Colors.green, |
| 106 | + size: 20.0, |
| 107 | + ) |
| 108 | + : const Icon(Icons.done, |
| 109 | + color: Colors.grey, size: 20.0), |
| 110 | + onPressed: () async { |
| 111 | + dietPlan.status = !dietPlan.status; |
| 112 | + await dietPlan.save(); |
| 113 | + setState(() {}); |
| 114 | + }), |
| 115 | + ), |
| 116 | + ); |
| 117 | + }); |
| 118 | + } else { |
| 119 | + return Center( |
| 120 | + child: const Text('No Data'), |
| 121 | + ); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + String dietPlansToAdd = |
| 127 | + '[{"className":"Diet_Plans","Name":"Textbook","Description":"For an active lifestyle and a straight forward macro plan, we suggest this plan.","Fat":25,"Carbs":50,"Protein":25,"Status":false},' |
| 128 | + '{"className":"Diet_Plans","Name":"Body Builder","Description":"Default Body Builders Diet","Fat":20,"Carbs":40,"Protein":40,"Status":true},' |
| 129 | + '{"className":"Diet_Plans","Name":"Zone Diet","Description":"Popular with CrossFit users. Zone Diet targets similar macros.","Fat":30,"Carbs":40,"Protein":30,"Status":true},' |
| 130 | + '{"className":"Diet_Plans","Name":"Low Fat","Description":"Low fat diet.","Fat":15,"Carbs":60,"Protein":25,"Status":false},' |
| 131 | + '{"className":"Diet_Plans","Name":"Low Carb","Description":"Low Carb diet, main focus on quality fats and protein.","Fat":35,"Carbs":25,"Protein":40,"Status":true},' |
| 132 | + '{"className":"Diet_Plans","Name":"Paleo","Description":"Paleo diet.","Fat":60,"Carbs":25,"Protein":10,"Status":false},' |
| 133 | + '{"className":"Diet_Plans","Name":"Ketogenic","Description":"High quality fats, low carbs.","Fat":65,"Carbs":5,"Protein":30,"Status":true}]'; |
| 134 | +} |
0 commit comments