From 421af28585b1c8259b2de1da96b5d658320756b4 Mon Sep 17 00:00:00 2001 From: danhan Date: Sun, 25 Sep 2022 18:46:35 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=C2=A0FEAT:=20Sync=20favorite=20button?= =?UTF-8?q?=20state=20with=20firestore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/page/detail_page/detailPage.dart | 125 +++++++++++++-------------- 1 file changed, 60 insertions(+), 65 deletions(-) diff --git a/lib/page/detail_page/detailPage.dart b/lib/page/detail_page/detailPage.dart index c2e1dce..aed14b6 100644 --- a/lib/page/detail_page/detailPage.dart +++ b/lib/page/detail_page/detailPage.dart @@ -17,70 +17,6 @@ class detailPage extends StatefulWidget { } class _detailPageState extends State { - // List _allcoordinate = [0.0001,0.0001]; - - PreferredSizeWidget _appBarWidget() { - DocumentReference _documentReference = - FirebaseFirestore.instance.collection('cae').doc(widget.cafeName); - - // dynamic favorite = StreamBuilder( - // stream: _documentReference.snapshots(), - // builder: (context, currentUserDocSnapshot) { - // if (!currentUserDocSnapshot.hasData) { - // return Center( - // child: CircularProgressIndicator( - // backgroundColor: Colors.deepPurple, - // )); - // } - // return Text(currentUserDocSnapshot.data!["username"]); - // }, - // ); - - // print("여기야 ${favorite.data['favorite']}"); - - bool _favoriteButtonPressed = widget.likedStatus; - double _xcoordinate = 0.00000001; - double _ycoordinate = 0.00000001; - - return AppBar( - elevation: 0, - leading: IconButton( - icon: const Icon(Icons.arrow_back), - onPressed: () { - Navigator.pop(context); - }, - ), - backgroundColor: Colors.transparent, - actions: [ - IconButton( - icon: Icon( - _favoriteButtonPressed ? Icons.favorite : Icons.favorite_border, - ), - color: Colors.red, - iconSize: 25.0, - onPressed: () { - setState(() { - _favoriteButtonPressed = !_favoriteButtonPressed; - print(_favoriteButtonPressed); - _documentReference.update({'favorite': _favoriteButtonPressed}); - }); - ; - }, - ), - IconButton( - icon: const Icon(Icons.location_pin), - onPressed: () { - Navigator.of(context).push(MaterialPageRoute( - builder: (context) => detailMapPage( - xcoordinate: _xcoordinate, - ycoordinate: _ycoordinate, - ))); - }, - ), - ], - ); - } - Widget _tagWidget(int tag) { String tag_string = "태그"; @@ -300,7 +236,66 @@ class _detailPageState extends State { Widget build(BuildContext context) { return Scaffold( extendBodyBehindAppBar: true, - appBar: _appBarWidget(), + appBar: PreferredSize( + preferredSize: const Size(double.infinity, kToolbarHeight), + child: StreamBuilder( + stream: FirebaseFirestore.instance + .collection('cae') + .doc(widget.cafeName) + .snapshots(), + builder: (context, snapshot) { + if (!snapshot.hasData) { + return Text('Something went wrong'); + } + + bool _favoriteButtonPressed = snapshot.data!['favorite']; + double _xcoordinate = 0.00000001; + double _ycoordinate = 0.00000001; + + return AppBar( + elevation: 0, + leading: IconButton( + icon: const Icon(Icons.arrow_back), + onPressed: () { + Navigator.pop(context); + }, + ), + backgroundColor: Colors.transparent, + actions: [ + IconButton( + icon: Icon( + _favoriteButtonPressed + ? Icons.favorite + : Icons.favorite_border, + ), + color: Colors.red, + iconSize: 25.0, + onPressed: () { + setState(() { + _favoriteButtonPressed = !_favoriteButtonPressed; + print(_favoriteButtonPressed); + FirebaseFirestore.instance + .collection('cae') + .doc(widget.cafeName) + .update({'favorite': _favoriteButtonPressed}); + }); + ; + }, + ), + IconButton( + icon: const Icon(Icons.location_pin), + onPressed: () { + Navigator.of(context).push(MaterialPageRoute( + builder: (context) => detailMapPage( + xcoordinate: _xcoordinate, + ycoordinate: _ycoordinate, + ))); + }, + ), + ], + ); + }, + )), body: _bodyWidget()); } }