Skip to content

Commit

Permalink
Merge pull request #3 from naaando/33-corrige-estado-de-salvamento-da…
Browse files Browse the repository at this point in the history
…-medicação-e-pet

33 - Corrige estado de salvamento da medicação e pet
  • Loading branch information
naaando authored Sep 25, 2023
2 parents b953e39 + 2067fa0 commit 8bbccea
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 113 deletions.
24 changes: 14 additions & 10 deletions pets/lib/components/datetime_form_field.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:intl/intl.dart';

class DateTimeFormField extends StatelessWidget {
final TextEditingController controller;
class DateTimeFormField extends HookWidget {
final InputDecoration decoration;
final Function(DateTime?) onDateChanged;
final String? initialValue;
final DateTime firstDate;
final DateTime lastDate;

final String? Function(dynamic value)? validator;

const DateTimeFormField({
Key? key,
required this.controller,
required this.initialValue,
required this.decoration,
required this.onDateChanged,
required this.firstDate,
required this.lastDate,
this.validator,
}) : super(key: key);

@override
Widget build(BuildContext context) {
DateTime? dateTime = DateTime.tryParse(controller.text)?.toLocal();
DateTime? dateTime = initialValue != null
? DateTime.tryParse(initialValue!)?.toLocal()
: null;

if (dateTime is DateTime) {
controller.text = DateFormat().format(dateTime);
}
final value = dateTime != null ? DateFormat().format(dateTime) : null;

return TextFormField(
controller: controller,
controller: TextEditingController(text: value),
decoration: decoration,
readOnly: true,
validator: validator,
onTap: () {
showDatePicker(
context: context,
Expand All @@ -37,7 +42,7 @@ class DateTimeFormField extends StatelessWidget {
lastDate: lastDate,
).then((date) {
if (date == null) {
return;
return onDateChanged(null);
}

showTimePicker(
Expand All @@ -56,7 +61,6 @@ class DateTimeFormField extends StatelessWidget {
seconds: 0,
));

controller.text = dateTime.toIso8601String();
onDateChanged(dateTime.toUtc());
});
});
Expand Down
6 changes: 4 additions & 2 deletions pets/lib/components/pet_avatar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
import 'package:pets/models/pet.dart';

class PetAvatar extends StatelessWidget {
final FastCachedImageProvider image;
final FastCachedImageProvider? image;
final String nome;
final double? size;
final TextStyle? textStyle;
Expand All @@ -21,7 +21,9 @@ class PetAvatar extends StatelessWidget {
super.key,
this.size = 20,
this.textStyle,
}) : image = FastCachedImageProvider(pet.imagemUri.toString()),
}) : image = pet.imagemUri != null
? FastCachedImageProvider(pet.imagemUri.toString())
: null,
nome = pet.nome.substring(0, 2).toUpperCase();

@override
Expand Down
37 changes: 22 additions & 15 deletions pets/lib/components/pet_square_avatar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
import 'package:pets/models/pet.dart';

class PetSquareAvatar extends StatelessWidget {
final FastCachedImageProvider image;
final FastCachedImageProvider? image;
final String nome;
final double? size;
final TextStyle? textStyle;
Expand All @@ -21,31 +21,38 @@ class PetSquareAvatar extends StatelessWidget {
super.key,
this.size = 20,
this.textStyle,
}) : image = FastCachedImageProvider(pet.imagemUri.toString()),
}) : image = pet.imagemUri != null
? FastCachedImageProvider(pet.imagemUri.toString())
: null,
nome = pet.nome.substring(0, 2).toUpperCase();

@override
Widget build(BuildContext context) {
final backgroundColor = Theme.of(context).colorScheme.onPrimaryContainer;
final iconColor = Theme.of(context).colorScheme.onPrimary;
final placeholder = Container(
width: size! * 2,
height: size! * 2,
decoration: BoxDecoration(
color: backgroundColor,
),
child: Icon(
Icons.pets,
color: iconColor,
size: size!,
),
);

if (image == null) {
return placeholder;
}

return Image(
image: image,
image: image!,
fit: BoxFit.cover,
width: size! * 2,
height: size! * 2,
errorBuilder: (context, error, stackTrace) => Container(
width: size! * 2,
height: size! * 2,
decoration: BoxDecoration(
color: backgroundColor,
),
child: Icon(
Icons.pets,
color: iconColor,
size: size!,
),
),
errorBuilder: (context, error, stackTrace) => placeholder,
);
}
}
54 changes: 32 additions & 22 deletions pets/lib/pages/medicacao_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,39 @@ class MedicacaoPage extends HookConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
var medicacaoRouterArg =
final medicacaoRouterArg =
(ModalRoute.of(context)!.settings.arguments as Medicacao?);

var medicacao =
useState<Medicacao>(medicacaoRouterArg ?? Medicacao(tipo: tipoPadrao));
final medicacaoOriginal = medicacaoRouterArg?.copyWith(
completado: medicacaoRouterArg.deveCompletar(),
) ??
Medicacao(tipo: tipoPadrao);

medicacao.value =
medicacao.value.copyWith(completado: medicacao.value.deveCompletar());
final medicacao = useState<Medicacao>(medicacaoOriginal);

var proximaData = useState<String?>(null);
final proximaData = useState<String?>(null);

var title = medicacao.value.id != null
final title = medicacao.value.id != null
? 'Editando ${medicacao.value.tipoExtenso}'
: 'Nova ${medicacao.value.tipoExtenso}';

var formKey = useRef(GlobalKey<FormState>());
final formKey = useRef(GlobalKey<FormState>());

return WillPopScope(
onWillPop: () =>
pedirParaSalvarDialog(context, medicacaoRouterArg, medicacao),
onWillPop: () => pedirParaSalvarDialog(
context,
medicacaoOriginal,
medicacao,
),
child: Scaffold(
appBar: AppBar(
title: Text(title),
actions: barActions(context, ref, formKey.value, medicacao.value),
actions: barActions(
context,
ref,
formKey.value,
medicacao.value,
),
),
body: body(
context,
Expand Down Expand Up @@ -179,15 +188,11 @@ class MedicacaoPage extends HookConsumerWidget {
Map<String, Pet> pets =
ref.read(petsProvider).asData?.value ?? <String, Pet>{};

final dataController = useTextEditingController(
text: medicacao.value.quando,
);

return Form(
key: formKey,
child: ListView(
children: [
conteudoPrincipal(medicacao, pets, dataController),
conteudoPrincipal(medicacao, pets),
outrasInformacoes(medicacao),
repetir(medicacao, proximaData),
const SizedBox(height: 60),
Expand All @@ -199,13 +204,12 @@ class MedicacaoPage extends HookConsumerWidget {
ExpansionTile conteudoPrincipal(
ValueNotifier<Medicacao> medicacao,
Map<String, Pet> pets,
TextEditingController dataController,
) {
return ExpansionTile(
title: const Text('Principal'),
leading: const Icon(Icons.event),
initiallyExpanded: true,
childrenPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 18),
childrenPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 12),
children: [
DropdownButtonFormField<String>(
value: medicacao.value.tipo,
Expand Down Expand Up @@ -265,7 +269,7 @@ class MedicacaoPage extends HookConsumerWidget {
),
const SizedBox(height: 20),
DateTimeFormField(
controller: dataController,
initialValue: medicacao.value.quando,
firstDate: DateTime.now().subtract(const Duration(days: 365)),
lastDate: DateTime.now().add(const Duration(days: 365)),
decoration: const InputDecoration(
Expand All @@ -278,6 +282,12 @@ class MedicacaoPage extends HookConsumerWidget {
medicacao.value =
medicacao.value.copyWith(quando: dateTime?.toIso8601String());
},
validator: (value) {
if (value == null || value.isEmpty) {
return 'Data é obrigatória';
}
return null;
},
),
const SizedBox(height: 12),
],
Expand Down Expand Up @@ -305,9 +315,9 @@ class MedicacaoPage extends HookConsumerWidget {
return [
const SizedBox(height: 20),
DateTimeFormField(
initialValue: proximaData.value,
firstDate: DateTime.now(),
lastDate: DateTime.now().add(const Duration(days: 365)),
controller: proximaDoseController,
decoration: const InputDecoration(
hintText: 'Próxima dose',
labelText: 'Próxima dose',
Expand Down Expand Up @@ -381,7 +391,7 @@ class MedicacaoPage extends HookConsumerWidget {
return ExpansionTile(
title: const Text('Outras informações'),
leading: const Icon(Icons.dataset),
childrenPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 18),
childrenPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 12),
children: [
TextFormField(
initialValue: medicacao.value.atributos.fabricante,
Expand Down Expand Up @@ -425,7 +435,7 @@ class MedicacaoPage extends HookConsumerWidget {
return ExpansionTile(
title: const Text('Repetir'),
leading: const Icon(Icons.alarm),
childrenPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 18),
childrenPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 12),
children: [
Row(
children: [
Expand Down
Loading

0 comments on commit 8bbccea

Please sign in to comment.