From f6f822e9f8e7bb14e2e27f4fb2b4d497b3bf3d7c Mon Sep 17 00:00:00 2001 From: dev-Roshan-lab Date: Mon, 19 Aug 2024 10:55:17 +0530 Subject: [PATCH] feat: add loading indicator for posting --- lib/pages/feed/feed.dart | 6 +- lib/pages/post_page.dart | 3 +- lib/pages/profile.dart | 3 +- lib/widgets/appbar.dart | 322 +++++++++++++++-------------------- lib/widgets/post_dialog.dart | 48 +++--- 5 files changed, 174 insertions(+), 208 deletions(-) diff --git a/lib/pages/feed/feed.dart b/lib/pages/feed/feed.dart index 46ef3f3..056a1f2 100644 --- a/lib/pages/feed/feed.dart +++ b/lib/pages/feed/feed.dart @@ -440,15 +440,15 @@ class _FeedState extends State with TickerProviderStateMixin { AssetImage assetImage = el[txt]!; return GestureDetector( onTap: () async { + setState(() { + selectedIndex = index; + }); if (index == 0) { await getAllPosts(); } else { await queryCollegeInPosts( el.keys.toList()[index]); } - setState(() { - selectedIndex = index; - }); }, child: Container( width: MediaQuery.of(context).size.width * diff --git a/lib/pages/post_page.dart b/lib/pages/post_page.dart index 9d91769..27ca576 100644 --- a/lib/pages/post_page.dart +++ b/lib/pages/post_page.dart @@ -13,6 +13,7 @@ import 'package:readmore/readmore.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:srm_curious_bug/pages/profile.dart'; import 'package:srm_curious_bug/utils/constants.dart'; +import 'package:srm_curious_bug/widgets/appbar.dart'; import 'package:srm_curious_bug/widgets/gantt_chart.dart'; import 'package:srm_curious_bug/widgets/post_dialog.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -192,7 +193,7 @@ class _PostPageState extends State with TickerProviderStateMixin { TabController tabController = TabController(length: 2, vsync: this); return Scaffold( - // appBar: appBar(context), + appBar: appBar(context), backgroundColor: const Color(0xffF7F9FA), body: SafeArea( child: Column( diff --git a/lib/pages/profile.dart b/lib/pages/profile.dart index cd8ee5d..1e3d963 100644 --- a/lib/pages/profile.dart +++ b/lib/pages/profile.dart @@ -5,6 +5,7 @@ import 'package:google_fonts/google_fonts.dart'; import 'package:readmore/readmore.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:srm_curious_bug/pages/post_page.dart'; +import 'package:srm_curious_bug/widgets/appbar.dart'; import 'package:srm_curious_bug/widgets/editProfile.dart'; import 'package:srm_curious_bug/widgets/post_dialog.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -146,7 +147,7 @@ class _ProfileState extends State { @override Widget build(BuildContext context) { return Scaffold( - // appBar: appBar(context), + appBar: appBar(context), backgroundColor: const Color.fromARGB(255, 223, 218, 218), body: SingleChildScrollView( child: Column( diff --git a/lib/widgets/appbar.dart b/lib/widgets/appbar.dart index be6d805..ab244fa 100644 --- a/lib/widgets/appbar.dart +++ b/lib/widgets/appbar.dart @@ -1,181 +1,143 @@ -// import 'dart:convert'; +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:flutter/material.dart'; +import 'package:srm_curious_bug/pages/profile.dart'; -// import 'package:firebase_auth/firebase_auth.dart'; -// import 'package:flutter/material.dart'; -// import 'package:google_fonts/google_fonts.dart'; -// import 'package:http/http.dart' as http; -// import 'package:srm_curious_bug/pages/profile.dart'; -// import 'package:srm_curious_bug/widgets/post_dialog.dart'; - -// PreferredSizeWidget appBar(BuildContext context) { -// TextEditingController searchController = TextEditingController(); -// String url = "http://10.9.31.214:80"; -// final List filterNames = [ -// 'Hashtags', -// 'Posts', -// ]; -// return AppBar( -// elevation: 0, -// leading: InkWell( -// onTap: () { -// Navigator.pushReplacementNamed(context, '/feed'); -// }, -// child: Padding( -// padding: const EdgeInsets.only(left: 10.0), -// child: Container( -// decoration: const BoxDecoration( -// image: -// DecorationImage(image: AssetImage("assets/images/logo.png"))), -// ), -// ), -// ), -// centerTitle: true, -// title: SizedBox( -// height: 40, -// width: MediaQuery.of(context).size.width * 0.45, -// child: TextFormField( -// controller: searchController, -// decoration: InputDecoration( -// prefixIcon: const Icon(Icons.search), -// labelText: "Search", -// suffixIcon: SizedBox( -// width: 140, -// child: Row( -// mainAxisAlignment: MainAxisAlignment.center, -// crossAxisAlignment: CrossAxisAlignment.center, -// children: [ -// Padding( -// padding: const EdgeInsets.all(2.0), -// child: Row( -// mainAxisAlignment: MainAxisAlignment.center, -// crossAxisAlignment: CrossAxisAlignment.center, -// children: [ -// // Icon(Icons.sort, -// // color: Theme.of(context).colorScheme.primary), -// DropdownButtonHideUnderline( -// child: DropdownButton( -// icon: Padding( -// padding: const EdgeInsets.only(left: 1), -// child: Icon(Icons.sort, -// color: -// Theme.of(context).colorScheme.secondary), -// ), -// iconSize: 20, -// elevation: 10, -// style: GoogleFonts.inter( -// textStyle: -// const TextStyle(color: Colors.black)), -// dropdownColor: Colors.grey[200], -// hint: Text('filter', -// style: GoogleFonts.inter( -// textStyle: TextStyle( -// fontWeight: FontWeight.w500, -// color: Theme.of(context) -// .colorScheme -// .secondary), -// )), -// value: null, // Initially selected value or null -// onChanged: (String? newValue) { -// // Handle dropdown value change -// }, -// items: filterNames.map((String value) { -// return DropdownMenuItem( -// value: value, -// child: Text( -// value, -// style: GoogleFonts.inter( -// textStyle: const TextStyle(fontSize: 16)), -// ), -// ); -// }).toList(), -// ), -// ), -// ]), -// ), -// IconButton( -// onPressed: () async { -// http.Response res = await http.post( -// Uri.parse("$url/post"), -// body: jsonEncode({ -// "user_id": "123", -// "type": "search", -// "content": searchController.text, -// "id": "" -// })); -// //TODO: implement posts list update -// }, -// icon: Icon( -// Icons.search, -// color: Theme.of(context).colorScheme.primary, -// )) -// ], -// ), -// ), -// contentPadding: -// const EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0), -// border: OutlineInputBorder( -// borderRadius: BorderRadius.circular(5.0), -// borderSide: const BorderSide(color: Colors.transparent, width: 0), -// ), -// enabledBorder: OutlineInputBorder( -// borderRadius: BorderRadius.circular(5.0), -// borderSide: const BorderSide(color: Colors.transparent, width: 0), -// ), -// fillColor: Colors.grey.shade200, -// filled: true, -// ), -// ), -// ), -// actions: [ -// TextButton( -// onPressed: () { -// postDialog(context); -// }, -// style: ButtonStyle( -// backgroundColor: WidgetStateProperty.all(Colors.black), -// shape: WidgetStateProperty.all(RoundedRectangleBorder( -// borderRadius: BorderRadius.circular(10))), -// fixedSize: WidgetStateProperty.all(const Size(100, 30))), -// child: Text( -// "Create +", -// style: GoogleFonts.inter( -// color: Colors.white, fontWeight: FontWeight.bold), -// ), -// ), -// const SizedBox(width: 20), -// InkWell( -// onTap: () {}, -// child: Image.asset("assets/icons/bell.png", height: 30, width: 30), -// ), -// const SizedBox(width: 20), -// InkWell( -// onTap: () {}, -// child: Image.asset("assets/icons/message.png", height: 30, width: 30), -// ), -// const SizedBox(width: 20), -// InkWell( -// onTap: () { -// Navigator.push(context, MaterialPageRoute(builder: (context) => const Profile(email: "123"))); -// // Navigator.pushNamed(context, '/profile'); -// }, -// child: Row( -// children: [ -// CircleAvatar( -// backgroundColor: Colors.white, -// radius: 18, -// backgroundImage: -// NetworkImage(FirebaseAuth.instance.currentUser!.photoURL!)), -// const SizedBox( -// height: 30, -// width: 30, -// child: Icon( -// Icons.arrow_drop_down_outlined, -// color: Colors.grey, -// ), -// ) -// ], -// ), -// ), -// ], -// ); -// } +PreferredSizeWidget appBar(BuildContext context) { + // TextEditingController searchController = TextEditingController(); + return AppBar( + elevation: 0, + leading: InkWell( + onTap: () { + Navigator.pushReplacementNamed(context, '/feed'); + }, + child: Padding( + padding: const EdgeInsets.only(left: 10.0), + child: Container( + decoration: const BoxDecoration( + image: + DecorationImage(image: AssetImage("assets/images/logo.png"))), + ), + ), + ), + centerTitle: true, + // title: SizedBox( + // height: 40, + // width: MediaQuery.of(context).size.width * 0.45, + // child: TextFormField( + // controller: searchController, + // decoration: InputDecoration( + // prefixIcon: const Icon(Icons.search), + // labelText: "Search", + // suffixIcon: SizedBox( + // width: 140, + // child: Row( + // mainAxisAlignment: MainAxisAlignment.center, + // crossAxisAlignment: CrossAxisAlignment.center, + // children: [ + // Padding( + // padding: const EdgeInsets.all(2.0), + // child: Row( + // mainAxisAlignment: MainAxisAlignment.center, + // crossAxisAlignment: CrossAxisAlignment.center, + // children: [ + // // Icon(Icons.sort, + // // color: Theme.of(context).colorScheme.primary), + // DropdownButtonHideUnderline( + // child: DropdownButton( + // icon: Padding( + // padding: const EdgeInsets.only(left: 1), + // child: Icon(Icons.sort, + // color: + // Theme.of(context).colorScheme.secondary), + // ), + // iconSize: 20, + // elevation: 10, + // style: GoogleFonts.inter( + // textStyle: + // const TextStyle(color: Colors.black)), + // dropdownColor: Colors.grey[200], + // hint: Text('filter', + // style: GoogleFonts.inter( + // textStyle: TextStyle( + // fontWeight: FontWeight.w500, + // color: Theme.of(context) + // .colorScheme + // .secondary), + // )), + // value: null, // Initially selected value or null + // onChanged: (String? newValue) { + // // Handle dropdown value change + // }, + // items: filterNames.map((String value) { + // return DropdownMenuItem( + // value: value, + // child: Text( + // value, + // style: GoogleFonts.inter( + // textStyle: const TextStyle(fontSize: 16)), + // ), + // ); + // }).toList(), + // ), + // ), + // ]), + // ), + // IconButton( + // onPressed: () async { + // http.Response res = await http.post( + // Uri.parse("$url/post"), + // body: jsonEncode({ + // "user_id": "123", + // "type": "search", + // "content": searchController.text, + // "id": "" + // })); + // }, + // icon: Icon( + // Icons.search, + // color: Theme.of(context).colorScheme.primary, + // )) + // ], + // ), + // ), + // contentPadding: + // const EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0), + // border: OutlineInputBorder( + // borderRadius: BorderRadius.circular(5.0), + // borderSide: const BorderSide(color: Colors.transparent, width: 0), + // ), + // enabledBorder: OutlineInputBorder( + // borderRadius: BorderRadius.circular(5.0), + // borderSide: const BorderSide(color: Colors.transparent, width: 0), + // ), + // fillColor: Colors.grey.shade200, + // filled: true, + // ), + // ), + // ), + actions: [ + InkWell( + onTap: () { + Navigator.push(context, MaterialPageRoute(builder: (context) => const Profile())); + }, + child: Row( + children: [ + CircleAvatar( + backgroundColor: Colors.white, + radius: 18, + backgroundImage: + NetworkImage(FirebaseAuth.instance.currentUser!.photoURL!)), + const SizedBox( + height: 30, + width: 30, + child: Icon( + Icons.arrow_drop_down_outlined, + color: Colors.grey, + ), + ) + ], + ), + ), + ], + ); +} diff --git a/lib/widgets/post_dialog.dart b/lib/widgets/post_dialog.dart index 8165624..ba96b46 100644 --- a/lib/widgets/post_dialog.dart +++ b/lib/widgets/post_dialog.dart @@ -50,6 +50,7 @@ postDialog(BuildContext context, } bool showError = false; bool mediaUploaded = false; + bool posting = false; return showDialog( context: context, @@ -521,6 +522,7 @@ postDialog(BuildContext context, padding: const EdgeInsets.all(2.0), child: TextButton( onPressed: () async { + dialogState(() => posting = true); String id = const Uuid().v4(); SharedPreferences prefs = await SharedPreferences.getInstance(); @@ -553,15 +555,13 @@ postDialog(BuildContext context, if (isAnUpdate) { if (images.isEmpty) { - FirebaseFirestore.instance + await FirebaseFirestore.instance .collection("posts") .doc(id) .update(storeData); // await getAllPosts(); - dialogState(() { - // posts.insert(0,storeData); - Navigator.pop(context); - }); + dialogState(() => posting = false); + Navigator.pop(context); // upload to qdrant // http.Response res = await http @@ -581,10 +581,9 @@ postDialog(BuildContext context, // throw "Error on sendin request to QDrant"; // }); // print(res.body); - Navigator.pop(context); } else { if (mediaUploaded) { - FirebaseFirestore.instance + await FirebaseFirestore.instance .collection("posts") .doc(id) .update(storeData); @@ -606,19 +605,16 @@ postDialog(BuildContext context, // throw "Error on sendin request to QDrant"; // }); } + dialogState(() => posting = false); Navigator.pop(context); } } else { if (images.isEmpty) { - FirebaseFirestore.instance + await FirebaseFirestore.instance .collection("posts") .doc(id) .set(storeData); // await getAllPosts(); - dialogState(() { - // posts.insert(0,storeData); - Navigator.pop(context); - }); // upload to qdrant http.Response res = await http @@ -637,11 +633,12 @@ postDialog(BuildContext context, print(e); throw "Error on sendin request to QDrant"; }); - print(res.body); + debugPrint(res.body); + dialogState(() => posting = false); Navigator.pop(context); } else { if (mediaUploaded) { - FirebaseFirestore.instance + await FirebaseFirestore.instance .collection("posts") .doc(id) .set(storeData); @@ -661,9 +658,9 @@ postDialog(BuildContext context, .onError((e, s) { print(e); throw "Error on sendin request to QDrant"; - }).whenComplete(() { - Navigator.pop(context); }); + dialogState(() => posting = false); + Navigator.pop(context); } // dialogState(() => showError = true); } @@ -681,13 +678,18 @@ postDialog(BuildContext context, borderRadius: BorderRadius.circular(10))), fixedSize: WidgetStateProperty.all( - const Size(100, 30))), - child: Text( - "Submit", - style: GoogleFonts.inter( - color: Colors.white, - fontWeight: FontWeight.bold), - ), + const Size(100, 40))), + child: posting + ? const SizedBox( + height: 35, + width: 35, + child: CircularProgressIndicator()) + : Text( + "Submit", + style: GoogleFonts.inter( + color: Colors.white, + fontWeight: FontWeight.bold), + ), ), ), ),