diff options
author | Iván Ávalos <avalos@disroot.org> | 2023-03-10 16:25:26 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2023-03-10 16:25:26 -0600 |
commit | 71dfcf3b6527c67441f372ab08e82fd6bff5908f (patch) | |
tree | 97c0129941230fc6811244df2a386fc656a40d88 | |
parent | b85b5740847a4a4a615f8f0cc9f2b6d5d8509a82 (diff) | |
download | pmsna1-71dfcf3b6527c67441f372ab08e82fd6bff5908f.tar.gz pmsna1-71dfcf3b6527c67441f372ab08e82fd6bff5908f.tar.bz2 pmsna1-71dfcf3b6527c67441f372ab08e82fd6bff5908f.zip |
Implemented feature to add new posts
-rw-r--r-- | lib/database/helper.dart | 8 | ||||
-rw-r--r-- | lib/models/post.dart | 2 | ||||
-rw-r--r-- | lib/routes.dart | 2 | ||||
-rw-r--r-- | lib/screens/dashboard_screen.dart | 11 | ||||
-rw-r--r-- | lib/screens/new_post_screen.dart | 70 | ||||
-rw-r--r-- | lib/widgets/post_item.dart | 62 | ||||
-rw-r--r-- | lib/widgets/post_list.dart | 40 |
7 files changed, 190 insertions, 5 deletions
diff --git a/lib/database/helper.dart b/lib/database/helper.dart index a1f672d..e684ee0 100644 --- a/lib/database/helper.dart +++ b/lib/database/helper.dart @@ -20,18 +20,18 @@ class DatabaseHelper { _database = await openDatabase( dbDir, version: dbVersion, - onConfigure: _onConfigure, + onCreate: _onCreate, ); return _database!; } Future<Database> get database async => _database ?? await _initDatabase(); - static void _onConfigure(Database db) { - db.execute("""CREATE TABLE posts ( + static void _onCreate(Database db, int version) async { + await db.execute("""CREATE TABLE posts ( id INTEGER PRIMARY KEY, description VARCHAR(500), - date DATE, + date DATE );"""); } diff --git a/lib/models/post.dart b/lib/models/post.dart index 28231f1..e1de8cc 100644 --- a/lib/models/post.dart +++ b/lib/models/post.dart @@ -13,7 +13,7 @@ class Post { return Post( id: map["id"], description: map["description"], - date: map["description"], + date: map["date"], ); } } diff --git a/lib/routes.dart b/lib/routes.dart index e74f28d..7510075 100644 --- a/lib/routes.dart +++ b/lib/routes.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:pmsna1/screens/dashboard_screen.dart'; +import 'package:pmsna1/screens/new_post_screen.dart'; import 'package:pmsna1/screens/onboarding_screen.dart'; import 'screens/login_screen.dart'; @@ -11,5 +12,6 @@ Map<String, WidgetBuilder> getApplicationRoutes() { '/register': (BuildContext context) => const RegisterScreen(), '/onboard': (BuildContext context) => const OnboardingScreen(), '/dash': (BuildContext context) => const DashboardScreen(), + '/new': (BuildContext context) => const NewPostScreen(), }; } diff --git a/lib/screens/dashboard_screen.dart b/lib/screens/dashboard_screen.dart index 69e6d00..3f4ae21 100644 --- a/lib/screens/dashboard_screen.dart +++ b/lib/screens/dashboard_screen.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:pmsna1/providers/theme_provider.dart'; import 'package:pmsna1/settings/themes.dart'; +import 'package:pmsna1/widgets/post_list.dart'; import 'package:provider/provider.dart'; class DashboardScreen extends StatelessWidget { @@ -14,6 +15,9 @@ class DashboardScreen extends StatelessWidget { SliverAppBar.large( title: const Text('Inicio'), ), + const SliverFillRemaining( + child: PostList(), + ), ], ), drawer: Drawer( @@ -58,6 +62,13 @@ class DashboardScreen extends StatelessWidget { ], ), ), + floatingActionButton: FloatingActionButton.extended( + label: const Text('Nuevo'), + icon: const Icon(Icons.create), + onPressed: () { + Navigator.of(context).pushNamed('/new'); + }, + ), ); } } diff --git a/lib/screens/new_post_screen.dart b/lib/screens/new_post_screen.dart new file mode 100644 index 0000000..ee1ed50 --- /dev/null +++ b/lib/screens/new_post_screen.dart @@ -0,0 +1,70 @@ +import 'package:flutter/material.dart'; +import 'package:pmsna1/database/helper.dart'; + +class NewPostScreen extends StatefulWidget { + const NewPostScreen({super.key}); + + @override + State<NewPostScreen> createState() => _NewPostScreenState(); +} + +class _NewPostScreenState extends State<NewPostScreen> { + final TextEditingController _controller = TextEditingController(); + final _formKey = GlobalKey<FormState>(); + final DatabaseHelper _database = DatabaseHelper(); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Nueva publicación'), + actions: [ + IconButton( + icon: const Icon(Icons.send), + tooltip: 'Publicar', + 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', + ), + ); + + ScaffoldMessenger.of(context).showSnackBar(bar); + Navigator.of(context).pop(); + }); + }, + ) + ], + ), + body: Padding( + padding: const EdgeInsets.all(12.0), + child: Form( + key: _formKey, + child: Column( + children: [ + TextFormField( + controller: _controller, + maxLines: 4, + textAlignVertical: TextAlignVertical.top, + decoration: const InputDecoration( + border: OutlineInputBorder(), + labelText: 'Contenido', + ), + validator: (value) { + if (value != null && value.isNotEmpty) { + return null; + } + return 'Este campo no debe estar vacío'; + }, + ), + ], + ), + ), + ), + ); + } +} diff --git a/lib/widgets/post_item.dart b/lib/widgets/post_item.dart new file mode 100644 index 0000000..55f0a60 --- /dev/null +++ b/lib/widgets/post_item.dart @@ -0,0 +1,62 @@ +import 'package:flutter/material.dart'; +import 'package:pmsna1/models/post.dart'; + +class PostItem extends StatelessWidget { + final Post post; + + final padding = 16.0; + final spacer = const SizedBox( + height: 16.0, + width: 16.0, + ); + + const PostItem({super.key, required this.post}); + + @override + Widget build(BuildContext context) { + final avatar = CircleAvatar( + child: Image.asset("assets/logo_itc.png"), + ); + + const txtUser = Text( + 'Iván Ávalos', + style: TextStyle(fontWeight: FontWeight.bold), + ); + final txtDate = Text(post.date); + final txtDesc = Text(post.description); + final imgPost = Image.asset('assets/logo_itc.png'); + const likes = Icon(Icons.rate_review); + + return Card( + margin: EdgeInsets.fromLTRB(padding, 0, padding, padding), + child: Padding( + padding: EdgeInsets.all(padding), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Row(children: [ + avatar, + spacer, + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [txtUser, txtDate]) + ]), + ], + ), + spacer, + Card( + child: Padding( + padding: EdgeInsets.all(padding), + child: txtDesc, + ), + ), + ], + ), + ), + ); + } +} diff --git a/lib/widgets/post_list.dart b/lib/widgets/post_list.dart new file mode 100644 index 0000000..1f0f870 --- /dev/null +++ b/lib/widgets/post_list.dart @@ -0,0 +1,40 @@ +import 'package:flutter/material.dart'; +import 'package:pmsna1/database/helper.dart'; +import 'package:pmsna1/models/post.dart'; +import 'package:pmsna1/widgets/post_item.dart'; + +class PostList extends StatefulWidget { + const PostList({super.key}); + + @override + State<PostList> createState() => _PostListState(); +} + +class _PostListState extends State<PostList> { + late DatabaseHelper helper; + + @override + void initState() { + super.initState(); + helper = DatabaseHelper(); + } + + @override + Widget build(BuildContext context) { + return FutureBuilder( + future: helper.getAllPost(), + builder: (BuildContext context, AsyncSnapshot<List<Post>> snapshot) => + snapshot.hasData + ? ListView.builder( + itemCount: snapshot.data?.length, + itemBuilder: (context, index) { + Post post = snapshot.data![index]; + return PostItem(post: post); + }, + ) + : snapshot.hasError + ? const Center(child: Text('Ocurrió un error')) + : const Center(child: CircularProgressIndicator()), + ); + } +} |