diff options
author | Iván Ávalos <avalos@disroot.org> | 2023-05-28 23:27:46 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2023-05-28 23:27:46 -0600 |
commit | 55ec472797581a02ac433e5a9e08d560dcba6a13 (patch) | |
tree | 0abb2ff303b8c5087d66611bff025e477862f93a /lib/screens | |
parent | 4874a3073450c0dfcb1c443342bcae78728a471b (diff) | |
download | linkchat-55ec472797581a02ac433e5a9e08d560dcba6a13.tar.gz linkchat-55ec472797581a02ac433e5a9e08d560dcba6a13.tar.bz2 linkchat-55ec472797581a02ac433e5a9e08d560dcba6a13.zip |
Sustitución de setState() por ValueNotifiermain
Diffstat (limited to 'lib/screens')
-rw-r--r-- | lib/screens/favorites_screen.dart | 52 | ||||
-rw-r--r-- | lib/screens/login_screen.dart | 32 | ||||
-rw-r--r-- | lib/screens/new_chat_screen.dart | 83 | ||||
-rw-r--r-- | lib/screens/register_screen.dart | 40 |
4 files changed, 101 insertions, 106 deletions
diff --git a/lib/screens/favorites_screen.dart b/lib/screens/favorites_screen.dart index 4d6f949..db87c4a 100644 --- a/lib/screens/favorites_screen.dart +++ b/lib/screens/favorites_screen.dart @@ -1,3 +1,4 @@ +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:linkchat/firebase/database.dart'; import 'package:linkchat/widgets/chat_bubble.dart'; @@ -16,19 +17,19 @@ class _FavoritesScreenState extends State<FavoritesScreen> { final Auth _auth = Auth(); final Database _db = Database(); final TextEditingController _controller = TextEditingController(); - List<Favorite> favorites = []; - List<Favorite> filteredFavorites = []; + final ValueNotifier<List<Favorite>> _favoritesNotifier = + ValueNotifier<List<Favorite>>([]); + final ValueNotifier<List<Favorite>> _filteredFavoritesNotifier = + ValueNotifier<List<Favorite>>([]); @override void initState() { super.initState(); _db.getFavoritesByUserID(_auth.currentUser!.uid).first.then((f) { - setState(() { - favorites = f; - filteredFavorites = f; - }); + _favoritesNotifier.value = f; + _filteredFavoritesNotifier.value = f; }).onError((e, st) { - print(e); + if (kDebugMode) print(e); }); } @@ -55,28 +56,29 @@ class _FavoritesScreenState extends State<FavoritesScreen> { labelText: 'Buscar', ), onChanged: (value) { - setState(() { - if (value.isNotEmpty) { - filteredFavorites = favorites - .where((fav) => - fav.messageText.contains(value) == true || - fav.linkTitle?.contains(value) == true || - fav.linkDescription?.contains(value) == true) - .toList(); - } else { - filteredFavorites = favorites; - } - }); + if (value.isNotEmpty) { + _filteredFavoritesNotifier.value = _favoritesNotifier.value + .where((fav) => + fav.messageText.contains(value) == true || + fav.linkTitle?.contains(value) == true || + fav.linkDescription?.contains(value) == true) + .toList(); + } else { + _filteredFavoritesNotifier.value = _favoritesNotifier.value; + } }, ), ), Expanded( - child: ListView.builder( - itemCount: filteredFavorites.length, - itemBuilder: (context, index) { - Favorite fav = filteredFavorites[index]; - return LinkPreview(fav.getMessage()); - }, + child: ValueListenableBuilder( + valueListenable: _filteredFavoritesNotifier, + builder: (context, value, child) => ListView.builder( + itemCount: value.length, + itemBuilder: (context, index) { + Favorite fav = value[index]; + return LinkPreview(fav.getMessage()); + }, + ), ), ), ], diff --git a/lib/screens/login_screen.dart b/lib/screens/login_screen.dart index c98a565..964c28b 100644 --- a/lib/screens/login_screen.dart +++ b/lib/screens/login_screen.dart @@ -20,7 +20,7 @@ class _LoginScreenState extends State<LoginScreen> final Auth _auth = Auth(); - bool isLoading = false; + final ValueNotifier<bool> _loadingNotifier = ValueNotifier<bool>(false); bool isInit = false; final padding = 16.0; @@ -110,18 +110,14 @@ class _LoginScreenState extends State<LoginScreen> ); void onLoginClicked(BuildContext context) { - setState(() { - isLoading = true; - }); + _loadingNotifier.value = true; _auth .signInWithEmailAndPassword( email: _emailController.text, password: _passwordController.text, ) .then((result) { - setState(() { - isLoading = false; - }); + _loadingNotifier.value = false; result.fold( (user) { if (user != null && user.emailVerified == false) { @@ -141,13 +137,9 @@ class _LoginScreenState extends State<LoginScreen> } void onGoogleLoginClicked(BuildContext context) { - setState(() { - isLoading = true; - }); + _loadingNotifier.value = true; _auth.signInWithGoogle().then((result) { - setState(() { - isLoading = false; - }); + _loadingNotifier.value = false; result.fold( (user) {}, (error) => handleError(error), @@ -156,13 +148,9 @@ class _LoginScreenState extends State<LoginScreen> } void onGithubLoginClicked(BuildContext context) { - setState(() { - isLoading = true; - }); + _loadingNotifier.value = true; _auth.signInWithGithub().then((result) { - setState(() { - isLoading = false; - }); + _loadingNotifier.value = false; result.fold( (user) {}, (error) => handleError(error), @@ -208,7 +196,11 @@ class _LoginScreenState extends State<LoginScreen> ), ), ), - isLoading ? const LoadingModal() : const SizedBox.shrink(), + ValueListenableBuilder( + valueListenable: _loadingNotifier, + builder: (context, value, child) => + value ? const LoadingModal() : const SizedBox.shrink(), + ), ], ), ); diff --git a/lib/screens/new_chat_screen.dart b/lib/screens/new_chat_screen.dart index 3c37f2c..554b9a9 100644 --- a/lib/screens/new_chat_screen.dart +++ b/lib/screens/new_chat_screen.dart @@ -17,17 +17,17 @@ class _NewChatScreenState extends State<NewChatScreen> { final Auth _auth = Auth(); final Database _db = Database(); final TextEditingController _controller = TextEditingController(); - List<FsUser> users = []; - List<FsUser> filteredUsers = []; + final ValueNotifier<List<FsUser>> _usersNotifier = + ValueNotifier<List<FsUser>>([]); + final ValueNotifier<List<FsUser>> _filteredUsersNotifier = + ValueNotifier<List<FsUser>>([]); @override void initState() { super.initState(); _db.getAllUsers().first.then((u) { - setState(() { - users = u; - filteredUsers = u; - }); + _usersNotifier.value = u; + _filteredUsersNotifier.value = u; }); } @@ -54,45 +54,46 @@ class _NewChatScreenState extends State<NewChatScreen> { labelText: 'Nombre del contacto', ), onChanged: (value) { - setState(() { - if (value.isNotEmpty) { - filteredUsers = users - .where((user) => user.displayName.contains(value)) - .toList(); - } else { - filteredUsers = users; - } - }); + if (value.isNotEmpty) { + _filteredUsersNotifier.value = _usersNotifier.value + .where((user) => user.displayName.contains(value)) + .toList(); + } else { + _filteredUsersNotifier.value = _usersNotifier.value; + } }, ), ), Expanded( - child: ListView.builder( - itemCount: filteredUsers.length, - itemBuilder: (context, index) { - FsUser user = filteredUsers[index]; - return ListTile( - leading: CachedAvatar(user.photoUrl), - title: Text(user.displayName), - trailing: IconButton( - icon: const Icon(Icons.send), - onPressed: () { - _db - .saveGroup(Group( - createdBy: _auth.currentUser!.uid, - createdAt: DateTime.now(), - members: [ - _auth.currentUser!.uid, - user.uid, - ], - )) - .whenComplete(() { - Navigator.of(context).pop(); - }); - }, - ), - ); - }, + child: ValueListenableBuilder( + valueListenable: _filteredUsersNotifier, + builder: (context, value, child) => ListView.builder( + itemCount: value.length, + itemBuilder: (context, index) { + FsUser user = value[index]; + return ListTile( + leading: CachedAvatar(user.photoUrl), + title: Text(user.displayName), + trailing: IconButton( + icon: const Icon(Icons.send), + onPressed: () { + _db + .saveGroup(Group( + createdBy: _auth.currentUser!.uid, + createdAt: DateTime.now(), + members: [ + _auth.currentUser!.uid, + user.uid, + ], + )) + .whenComplete(() { + Navigator.of(context).pop(); + }); + }, + ), + ); + }, + ), ), ), ], diff --git a/lib/screens/register_screen.dart b/lib/screens/register_screen.dart index 0ba0cf9..7ddb252 100644 --- a/lib/screens/register_screen.dart +++ b/lib/screens/register_screen.dart @@ -17,13 +17,12 @@ class RegisterScreen extends StatefulWidget { } class _RegisterScreenState extends State<RegisterScreen> { - bool isLoading = false; + final ValueNotifier<bool> _loadingNotifier = ValueNotifier<bool>(false); + final ValueNotifier<XFile?> _avatarNotifier = ValueNotifier<XFile?>(null); final padding = 16.0; final spacer = const SizedBox(height: 16.0); - XFile? _avatar; - // TextField controllers late TextEditingController _nameController; late TextEditingController _emailController; @@ -40,7 +39,7 @@ class _RegisterScreenState extends State<RegisterScreen> { } bool validateForm() { - if (_formKey.currentState!.validate() && _avatar != null) { + if (_formKey.currentState!.validate() && _avatarNotifier.value != null) { return true; } return false; @@ -72,13 +71,15 @@ class _RegisterScreenState extends State<RegisterScreen> { mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ - AvatarPicker( - avatar: _avatar, - onAvatarPicked: (avatar) { - setState(() { - _avatar = avatar; - }); - }, + ValueListenableBuilder( + valueListenable: _avatarNotifier, + builder: (context, value, child) => + AvatarPicker( + avatar: _avatarNotifier.value, + onAvatarPicked: (avatar) { + _avatarNotifier.value = avatar; + }, + ), ), spacer, TextFormField( @@ -147,7 +148,11 @@ class _RegisterScreenState extends State<RegisterScreen> { ], ), ), - isLoading ? const LoadingModal() : const SizedBox.shrink(), + ValueListenableBuilder( + valueListenable: _loadingNotifier, + builder: (context, value, child) => + value ? const LoadingModal() : const SizedBox.shrink(), + ), ], ), ) @@ -157,22 +162,17 @@ class _RegisterScreenState extends State<RegisterScreen> { } void onRegisterClicked(BuildContext context) { - setState(() { - isLoading = true; - }); - + _loadingNotifier.value = true; if (validateForm()) { Auth() .createUserWithEmailAndPassword( email: _emailController.text, password: _passwordController.text, displayName: _nameController.text, - avatar: File(_avatar!.path), + avatar: File(_avatarNotifier.value!.path), ) .then((result) { - setState(() { - isLoading = false; - }); + _loadingNotifier.value = false; result.fold((user) { Navigator.of(context).pushReplacementNamed('/login'); }, (error) { |