From 598f2c114b772e2c2b011c0c3dac1f2d1a8f01e5 Mon Sep 17 00:00:00 2001 From: Juan Felipe Duarte <70235683+DuarteJFelipe@users.noreply.github.com> Date: Thu, 6 Apr 2023 13:27:22 -0500 Subject: [PATCH] imagen --- lib/src/components/photo_view.dart | 106 ++++++++++++++++------ lib/src/screens/profile.dart | 138 +++++++++++------------------ lib/src/screens/welcome.dart | 28 +++--- 3 files changed, 151 insertions(+), 121 deletions(-) diff --git a/lib/src/components/photo_view.dart b/lib/src/components/photo_view.dart index 5b830fc..0e45fec 100644 --- a/lib/src/components/photo_view.dart +++ b/lib/src/components/photo_view.dart @@ -1,37 +1,93 @@ +import 'dart:io'; + +import 'package:firebase_storage/firebase_storage.dart'; import 'package:flutter/material.dart'; -class PhotoViewWidget extends StatelessWidget { - Bytes imageData; - PhotoViewWidget({required this.imageData}); +const double photoSize = 100; + +class ReferencePhoto extends StatelessWidget { + Reference? ref; + double size; + ReferencePhoto({super.key, required this.ref, this.size = photoSize}); + + Future downloadImage() async { + try { + if (ref != null) { + final imageData = await ref!.getData(); + if (imageData != null) { + return ClipOval( + child: Image.memory( + imageData, + width: size, + height: size, + fit: BoxFit.cover, + ), + ); + } + } + // ignore: empty_catches + } catch (e) {} + + return const DefaultPhoto(); + } + + @override + Widget build(BuildContext context) { + return FutureBuilder( + future: downloadImage(), + builder: (BuildContext context, AsyncSnapshot snapshot) { + if (snapshot.connectionState == ConnectionState.waiting) { + // mientras la llamada asíncrona está en proceso, muestra un mensaje de carga + return const SizedBox( + width: photoSize, + height: photoSize, + child: Center( + child: CircularProgressIndicator(), + )); + } else if (snapshot.connectionState == ConnectionState.done && + snapshot.hasData) { + return snapshot.data!; + } else { + return const DefaultPhoto(); + } + }); + } +} + +class DefaultPhoto extends StatelessWidget { + const DefaultPhoto({super.key}); @override Widget build(BuildContext context) { return Container( - margin: EdgeInsets.symmetric(vertical: 50), - child: ClipOval( - child: Image.memory( - imageData, - width: 60, - height: 60, - fit: BoxFit.cover, - ), + width: photoSize, + height: photoSize, + decoration: BoxDecoration( + color: const Color(0xFF2BA4EC), + borderRadius: BorderRadius.circular(50), + ), + child: const Icon( + Icons.person, + color: Color.fromARGB(255, 255, 255, 255), + size: 55, ), ); } } +class LocalPhoto extends StatelessWidget { + File file; + LocalPhoto({super.key, required this.file}); -// Container( -// margin: EdgeInsets.symmetric(vertical: 50), -// width: 100, -// height: 100, -// decoration: BoxDecoration( -// color: Color(0xFF2BA4EC), -// borderRadius: BorderRadius.circular(50), -// ), -// child: Icon( -// Icons.person, -// color: Colors.white, -// size: 90, -// ), -// ); \ No newline at end of file + @override + Widget build(BuildContext context) { + return ClipOval( + child: Image.file( + file, + width: photoSize, + height: photoSize, + fit: BoxFit.cover, + ), + ); + } +} diff --git a/lib/src/screens/profile.dart b/lib/src/screens/profile.dart index b1b0b8e..38a778f 100644 --- a/lib/src/screens/profile.dart +++ b/lib/src/screens/profile.dart @@ -9,6 +9,8 @@ import 'package:prosappco/src/controllers/add_name_email_city.dart'; import 'package:prosappco/src/services/select_image_profile.dart'; import 'package:prosappco/src/services/upload_image.dart'; +import '../components/photo_view.dart'; + class ProfileScreen extends StatefulWidget { ProfileScreen({Key? key}) : super(key: key); @@ -271,6 +273,15 @@ class _ProfileScreenState extends State { } } + Widget prueba() { + try { + return ReferencePhoto(ref: null); + } catch (e) { + print('errrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrror $e'); + return Center(); + } + } + Future _showChoiceDialog(BuildContext context) async { return showDialog( context: context, @@ -343,60 +354,36 @@ class _ProfileScreenState extends State { children: [ Column( children: [ - Container( - child: Builder(builder: (context) { - return ListTile( - onTap: () { - Scaffold.of(context).openEndDrawer(); - }, - title: Text('${user?.displayName}', - style: TextStyle(fontWeight: FontWeight.bold)), - subtitle: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - '${user?.phoneNumber}', - style: TextStyle(fontSize: 12), - ), - Text( - '$city', - style: TextStyle(fontSize: 12), - ), - ], - ), - leading: FutureBuilder( - future: - downloadImage(storage.ref().child(_photo)), - builder: (BuildContext context, - AsyncSnapshot snapshot) { - if (snapshot.connectionState == - ConnectionState.done && - snapshot.hasData) { - return snapshot.data!; - } else { - return Container( - width: 60, - height: 60, - decoration: BoxDecoration( - color: Color(0xFF2BA4EC), - borderRadius: BorderRadius.circular(50), - ), - child: Icon( - Icons.person, - color: Colors.white, - size: 55, - ), - ); - } - }, - ), - trailing: Icon(Icons.keyboard_arrow_right, - color: Colors.black), - contentPadding: EdgeInsets.symmetric( - vertical: 20, horizontal: 16), - ); - }), - ), + Builder(builder: (context) { + return ListTile( + onTap: () { + Scaffold.of(context).openEndDrawer(); + }, + title: Text('${user?.displayName}', + style: TextStyle(fontWeight: FontWeight.bold)), + subtitle: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + '${user?.phoneNumber}', + style: TextStyle(fontSize: 12), + ), + Text( + '$city', + style: TextStyle(fontSize: 12), + ), + ], + ), + leading: ReferencePhoto( + ref: storage.ref().child(_photo), + size: 50, + ), + trailing: Icon(Icons.keyboard_arrow_right, + color: Colors.black), + contentPadding: EdgeInsets.symmetric( + vertical: 20, horizontal: 16), + ); + }), ], // padding: const EdgeInsets.only(top: 20, bottom: 15), ), Divider(height: 0, color: Colors.grey), @@ -555,38 +542,21 @@ class _ProfileScreenState extends State { body: Center( child: Column( children: [ - imagen_to_upload != null - ? GestureDetector( - onTap: () { - _showChoiceDialog(context); - }, - child: Container( - margin: EdgeInsets.symmetric(vertical: 50), - child: ClipOval( - child: Image.file( - imagen_to_upload!, - width: 100, - height: 100, - fit: BoxFit.cover, - ), - ), - ), - ) - : FutureBuilder( - future: downloadImage(storage.ref().child(_photo)), - builder: (BuildContext context, - AsyncSnapshot snapshot) { - if (snapshot.connectionState == ConnectionState.done && - snapshot.hasData) { - return snapshot.data!; - } else { - return const CircularProgressIndicator(); - } - }, - ), + GestureDetector( + onTap: () { + _showChoiceDialog(context); + }, + child: Container( + margin: const EdgeInsets.symmetric(vertical: 50), + child: (imagen_to_upload != null) + ? LocalPhoto( + file: imagen_to_upload!, + ) + : ReferencePhoto(ref: storage.ref().child(_photo))), + ), Container( width: 300, - padding: EdgeInsets.only(top: 0), + padding: const EdgeInsets.only(top: 0), child: Form( key: _formKey, child: Column( diff --git a/lib/src/screens/welcome.dart b/lib/src/screens/welcome.dart index ff1f65d..914ff9d 100644 --- a/lib/src/screens/welcome.dart +++ b/lib/src/screens/welcome.dart @@ -1,4 +1,5 @@ import 'package:firebase_auth/firebase_auth.dart'; +import 'package:firebase_storage/firebase_storage.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:prosappco/src/authentication/authentication_repository.dart'; @@ -6,6 +7,8 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:prosappco/src/controllers/add_name_email_city.dart'; import 'package:prosappco/src/screens/profile.dart'; +import '../components/photo_view.dart'; + class WelcomeScreen extends StatefulWidget { const WelcomeScreen({super.key}); @@ -14,14 +17,17 @@ class WelcomeScreen extends StatefulWidget { } class _WelcomeScreenState extends State { + final FirebaseStorage storage = FirebaseStorage.instance; final uid = AuthenticationRepository.instance.getCurrentUserUid(); final _formKey = GlobalKey(); final controller = Get.put(NameEmailCityController()); var _ciudad = ''; + var _photo = '...'; @override void initState() { super.initState(); + final Reference ref = storage.ref().child(_photo); if (_ciudad == '') { AuthenticationRepository.instance @@ -30,6 +36,13 @@ class _WelcomeScreenState extends State { _ciudad = s; })); } + if (_photo == '...') { + AuthenticationRepository.instance + .getPhoto(uid.toString()) + .then((String s) => setState(() { + _photo = s; + })); + } } @override @@ -69,18 +82,9 @@ class _WelcomeScreenState extends State { ), ], ), - leading: Container( - width: 60, - height: 60, - decoration: BoxDecoration( - color: Color(0xFF2BA4EC), - borderRadius: BorderRadius.circular(50), - ), - child: Icon( - Icons.person, - color: Colors.white, - size: 55, - ), + leading: ReferencePhoto( + ref: storage.ref().child(_photo), + size: 50, ), trailing: Icon(Icons.keyboard_arrow_right, color: Colors.black),