Skip to content

Commit

Permalink
refactor: Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagors09 committed Oct 16, 2024
1 parent b9e902e commit a2672b5
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 3 deletions.
28 changes: 27 additions & 1 deletion lib/service/preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -43,4 +43,30 @@ abstract class Preferences {
rethrow;
}
}

static Future<String> 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<Null> cleanHistory() async {
try {
sp = await SharedPreferences.getInstance();

sp.remove('history');
} catch (e) {
rethrow;
}
}
}
133 changes: 131 additions & 2 deletions lib/views/history_view.dart
Original file line number Diff line number Diff line change
@@ -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<HistoryView> createState() => _HistoryViewState();
}

class _HistoryViewState extends State<HistoryView> {
@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(
Expand All @@ -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();
},
),
);
}
}
7 changes: 7 additions & 0 deletions lib/widgets/personal_data_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit a2672b5

Please sign in to comment.