From d0d54a9d6e7ac947ae86962b6bbc7eeb9ada5011 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 22 May 2023 23:16:49 -0600 Subject: Se implementa caché para ávatars MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/screens/chat_screen.dart | 12 +++++++----- lib/screens/dashboard_screen.dart | 7 ++----- lib/screens/new_chat_screen.dart | 5 ++--- lib/widgets/cached_avatar.dart | 22 ++++++++++++++++++++++ 4 files changed, 33 insertions(+), 13 deletions(-) create mode 100644 lib/widgets/cached_avatar.dart (limited to 'lib') diff --git a/lib/screens/chat_screen.dart b/lib/screens/chat_screen.dart index 61f3f9e..64f8621 100644 --- a/lib/screens/chat_screen.dart +++ b/lib/screens/chat_screen.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:linkchat/firebase/auth.dart'; import 'package:linkchat/firebase/database.dart'; import 'package:linkchat/models/group.dart'; +import 'package:linkchat/widgets/cached_avatar.dart'; import '../models/favorite.dart'; import '../models/message.dart'; @@ -42,11 +43,12 @@ class _ChatScreenState extends State { leadingWidth: 30, title: Row( children: [ - ClipRRect( - borderRadius: BorderRadius.circular(30), - child: CircleAvatar( - backgroundImage: NetworkImage(user!.photoUrl), - )), + // ClipRRect( + // borderRadius: BorderRadius.circular(30), + // child: CircleAvatar( + // backgroundImage: NetworkImage(user!.photoUrl), + // )), + CachedAvatar(user?.photoUrl), Padding( padding: const EdgeInsets.only(left: 10), child: Text(user!.displayName), diff --git a/lib/screens/dashboard_screen.dart b/lib/screens/dashboard_screen.dart index 97a9de6..4905b84 100644 --- a/lib/screens/dashboard_screen.dart +++ b/lib/screens/dashboard_screen.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:linkchat/widgets/cached_avatar.dart'; import 'package:provider/provider.dart'; import '../firebase/auth.dart'; @@ -49,11 +50,7 @@ class _DashboardScreenState extends State { child: ListView( children: [ UserAccountsDrawerHeader( - currentAccountPicture: CircleAvatar( - backgroundImage: _auth.currentUser?.photoURL != null - ? NetworkImage(_auth.currentUser!.photoURL!) - : null, - ), + currentAccountPicture: CachedAvatar(_auth.currentUser?.photoURL), accountName: Text(_auth.currentUser?.displayName ?? "Lincite"), accountEmail: _auth.currentUser?.email != null ? Text(_auth.currentUser!.email!) diff --git a/lib/screens/new_chat_screen.dart b/lib/screens/new_chat_screen.dart index bf7486f..3c37f2c 100644 --- a/lib/screens/new_chat_screen.dart +++ b/lib/screens/new_chat_screen.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:linkchat/firebase/database.dart'; import 'package:linkchat/models/group.dart'; +import 'package:linkchat/widgets/cached_avatar.dart'; import '../firebase/auth.dart'; import '../models/user.dart'; @@ -71,9 +72,7 @@ class _NewChatScreenState extends State { itemBuilder: (context, index) { FsUser user = filteredUsers[index]; return ListTile( - leading: CircleAvatar( - backgroundImage: NetworkImage(user.photoUrl), - ), + leading: CachedAvatar(user.photoUrl), title: Text(user.displayName), trailing: IconButton( icon: const Icon(Icons.send), diff --git a/lib/widgets/cached_avatar.dart b/lib/widgets/cached_avatar.dart new file mode 100644 index 0000000..e6fcf89 --- /dev/null +++ b/lib/widgets/cached_avatar.dart @@ -0,0 +1,22 @@ +import 'package:cached_network_image/cached_network_image.dart'; +import 'package:flutter/material.dart'; + +class CachedAvatar extends StatelessWidget { + final String? avatarUrl; + const CachedAvatar(this.avatarUrl, {super.key}); + + @override + Widget build(BuildContext context) { + return CircleAvatar( + backgroundImage: + avatarUrl != null ? CachedNetworkImageProvider(avatarUrl!) : null, + backgroundColor: Theme.of(context).colorScheme.primary, + child: avatarUrl == null + ? Icon( + Icons.person, + color: Theme.of(context).colorScheme.onPrimary, + ) + : null, + ); + } +} -- cgit v1.2.3