From a2672b5c78248f8c11e32cd179cbf3e1f2773361 Mon Sep 17 00:00:00 2001 From: Tiago Rodrigues Sousa Date: Wed, 16 Oct 2024 18:09:27 -0300 Subject: [PATCH] refactor: Refactored --- lib/service/preferences.dart | 28 +++++- lib/views/history_view.dart | 133 +++++++++++++++++++++++++++- lib/widgets/personal_data_form.dart | 7 ++ 3 files changed, 165 insertions(+), 3 deletions(-) diff --git a/lib/service/preferences.dart b/lib/service/preferences.dart index 3f6c822..e569666 100644 --- a/lib/service/preferences.dart +++ b/lib/service/preferences.dart @@ -15,7 +15,7 @@ abstract class Preferences { final response = await Future.wait([ sp.setString('personal_data', data), - sp.setString('history', history), + sp.setString('history', jsonEncode(history)), ]); if (response[0] && response[1]) { @@ -43,4 +43,30 @@ abstract class Preferences { rethrow; } } + + static Future getHistory() async { + try { + sp = await SharedPreferences.getInstance(); + + final response = sp.getString('history'); + + if (response != null) { + return response; + } else { + throw Exception('Não há nada a ser exibido'); + } + } catch (e) { + rethrow; + } + } + + static Future cleanHistory() async { + try { + sp = await SharedPreferences.getInstance(); + + sp.remove('history'); + } catch (e) { + rethrow; + } + } } diff --git a/lib/views/history_view.dart b/lib/views/history_view.dart index 2f354cf..2a43155 100644 --- a/lib/views/history_view.dart +++ b/lib/views/history_view.dart @@ -1,15 +1,36 @@ +import 'dart:convert'; + +import 'package:calorie_way/service/preferences.dart'; import 'package:flutter/material.dart'; -class HistoryView extends StatelessWidget { +class HistoryView extends StatefulWidget { const HistoryView({super.key}); + @override + State createState() => _HistoryViewState(); +} + +class _HistoryViewState extends State { @override Widget build(BuildContext context) { + double screenWidth = MediaQuery.of(context).size.width; + return Scaffold( appBar: AppBar( leading: BackButton( color: Theme.of(context).colorScheme.onPrimary, ), + actions: [ + IconButton( + onPressed: () { + Preferences.cleanHistory().then((_) => setState(() {})); + }, + icon: Icon( + Icons.delete, + color: Theme.of(context).colorScheme.onPrimary, + ), + ), + ], title: Text( 'Histórico', style: TextStyle( @@ -18,7 +39,115 @@ class HistoryView extends StatelessWidget { ), backgroundColor: Theme.of(context).colorScheme.inversePrimary, ), - body: const Placeholder(), + body: FutureBuilder( + future: Preferences.getHistory(), + builder: (ctx, snp) { + if (snp.connectionState == ConnectionState.done && snp.hasData) { + final history = jsonDecode(snp.data!); + return ListView( + padding: const EdgeInsets.all(24), + children: [ + Container( + margin: const EdgeInsets.only(bottom: 12), + child: Text( + 'Data da última checagem: ${history["date"]}', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontSize: screenWidth * .03, + ), + ), + ), + Container( + margin: const EdgeInsets.only(bottom: 12), + child: Text( + 'Peso ${history["weight"].toString()} kg', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontSize: screenWidth * .03, + ), + ), + ), + Container( + margin: const EdgeInsets.only(bottom: 12), + child: Text( + 'Altura ${history["height"].toString()} cm', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontSize: screenWidth * .03, + ), + ), + ), + Container( + margin: const EdgeInsets.only(bottom: 12), + child: Text( + '${history["age"].toString()} anos de idade', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontSize: screenWidth * .03, + ), + ), + ), + Container( + margin: const EdgeInsets.only(bottom: 12), + child: Text( + history["gender"], + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontSize: screenWidth * .03, + ), + ), + ), + Container( + margin: const EdgeInsets.only(bottom: 12), + child: Text( + 'Objetivo foi ${history["goals"]}', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontSize: screenWidth * .03, + ), + ), + ), + Container( + margin: const EdgeInsets.only(bottom: 12), + child: Text( + 'Calorias a perder ${history["caloriesToLoss"].toString()}', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontSize: screenWidth * .03, + ), + ), + ), + Container( + margin: const EdgeInsets.only(bottom: 12), + child: Text( + 'Calorias a ganhar ${history["caloriesToGain"].toString()}', + style: TextStyle( + color: Theme.of(context).colorScheme.primary, + fontSize: screenWidth * .03, + ), + ), + ), + ], + ); + } + + if (snp.connectionState == ConnectionState.done && snp.hasError) { + return Center( + child: Text( + snp.error.toString().replaceFirst('Exception:', ''), + textAlign: TextAlign.center, + style: TextStyle( + fontSize: screenWidth * .06, + color: Theme.of(context).colorScheme.primaryContainer, + fontWeight: FontWeight.bold, + ), + ), + ); + } + + return const CircularProgressIndicator(); + }, + ), ); } } diff --git a/lib/widgets/personal_data_form.dart b/lib/widgets/personal_data_form.dart index cb98c29..4970520 100644 --- a/lib/widgets/personal_data_form.dart +++ b/lib/widgets/personal_data_form.dart @@ -30,6 +30,9 @@ class PersonalDataForm extends StatelessWidget with PersonalDataFormController { Container( margin: const EdgeInsets.only(bottom: 12), child: TextFormField( + keyboardType: const TextInputType.numberWithOptions( + decimal: true, + ), onSaved: (value) => weightOnSave(value, formData), validator: weightValidator, decoration: InputDecoration( @@ -41,6 +44,9 @@ class PersonalDataForm extends StatelessWidget with PersonalDataFormController { Container( margin: const EdgeInsets.only(bottom: 12), child: TextFormField( + keyboardType: const TextInputType.numberWithOptions( + decimal: true, + ), validator: heightValidator, onSaved: (value) => heightOnSaved(value, formData), decoration: InputDecoration( @@ -51,6 +57,7 @@ class PersonalDataForm extends StatelessWidget with PersonalDataFormController { Container( margin: const EdgeInsets.only(bottom: 12), child: TextFormField( + keyboardType: TextInputType.number, validator: ageValidator, onSaved: (value) => ageOnSaved(value, formData), decoration: InputDecoration(