diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/screens/new_post_screen.dart | 65 | ||||
-rw-r--r-- | lib/widgets/post_item.dart | 4 |
2 files changed, 54 insertions, 15 deletions
diff --git a/lib/screens/new_post_screen.dart b/lib/screens/new_post_screen.dart index ee1ed50..a1b2d2d 100644 --- a/lib/screens/new_post_screen.dart +++ b/lib/screens/new_post_screen.dart @@ -1,5 +1,8 @@ import 'package:flutter/material.dart'; import 'package:pmsna1/database/helper.dart'; +import 'package:pmsna1/models/post.dart'; +import 'package:pmsna1/providers/flags_provider.dart'; +import 'package:provider/provider.dart'; class NewPostScreen extends StatefulWidget { const NewPostScreen({super.key}); @@ -13,29 +16,63 @@ class _NewPostScreenState extends State<NewPostScreen> { final _formKey = GlobalKey<FormState>(); final DatabaseHelper _database = DatabaseHelper(); + Post? post; + @override Widget build(BuildContext context) { + post = ModalRoute.of(context)?.settings.arguments as Post?; + if (post != null) { + _controller.text = post!.description; + } + + FlagsProvider flags = Provider.of<FlagsProvider>(context); + return Scaffold( appBar: AppBar( title: const Text('Nueva publicación'), actions: [ IconButton( - icon: const Icon(Icons.send), - tooltip: 'Publicar', + icon: + post == null ? const Icon(Icons.send) : const Icon(Icons.edit), + tooltip: post == null ? 'Publicar' : 'Editar', onPressed: () { - _database.insert('posts', { - 'description': _controller.text, - 'date': DateTime.now().toString(), - }).then((value) { - SnackBar bar = SnackBar( - content: Text( - value > 0 ? 'Registro insertado' : 'Ocurrió un error', - ), - ); + if (post == null) { + _database.insert('posts', { + 'description': _controller.text, + 'date': DateTime.now().toString(), + }).then((value) { + SnackBar bar = SnackBar( + content: Text( + value > 0 ? 'Registro insertado' : 'Ocurrió un error', + ), + ); + + ScaffoldMessenger.of(context).showSnackBar(bar); + Navigator.of(context).pop(); + flags.triggerRefresh(); + }); + } else { + _database + .update( + 'posts', + { + 'description': _controller.text, + 'date': post!.date, + }, + 'id', + post!.id) + .then((value) { + SnackBar bar = SnackBar( + content: Text( + value > 0 ? 'Registro modificado' : 'Ocurrió un error', + ), + ); - ScaffoldMessenger.of(context).showSnackBar(bar); - Navigator.of(context).pop(); - }); + ScaffoldMessenger.of(context).showSnackBar(bar); + Navigator.of(context).pop(); + flags.triggerRefresh(); + }); + } }, ) ], diff --git a/lib/widgets/post_item.dart b/lib/widgets/post_item.dart index 952fd3f..4373059 100644 --- a/lib/widgets/post_item.dart +++ b/lib/widgets/post_item.dart @@ -69,7 +69,9 @@ class PostItem extends StatelessWidget { const Expanded(child: SizedBox.shrink()), IconButton( icon: const Icon(Icons.edit), - onPressed: () {}, + onPressed: () { + Navigator.of(context).pushNamed('/new', arguments: post); + }, ), IconButton( icon: const Icon(Icons.delete), |