From 416572bdee055b0ea435c32cc860f1f4bc927c8e Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Fri, 10 Mar 2023 14:19:15 +0530 Subject: [PATCH] Button state provider and UI update --- lib/components/editor_pane/url_card.dart | 100 ++++++++++++++++------- lib/providers/providers.dart | 1 + 2 files changed, 72 insertions(+), 29 deletions(-) diff --git a/lib/components/editor_pane/url_card.dart b/lib/components/editor_pane/url_card.dart index 2c40db8f..6425e073 100644 --- a/lib/components/editor_pane/url_card.dart +++ b/lib/components/editor_pane/url_card.dart @@ -5,16 +5,15 @@ import '../../utils/utils.dart'; import '../styles.dart'; import '../../consts.dart'; -class EditorPaneRequestURLCard extends ConsumerStatefulWidget { +class EditorPaneRequestURLCard extends StatefulWidget { const EditorPaneRequestURLCard({super.key}); @override - ConsumerState createState() => + State createState() => _EditorPaneRequestURLCardState(); } -class _EditorPaneRequestURLCardState - extends ConsumerState { +class _EditorPaneRequestURLCardState extends State { @override void initState() { super.initState(); @@ -22,7 +21,6 @@ class _EditorPaneRequestURLCardState @override Widget build(BuildContext context) { - final activeId = ref.watch(activeIdStateProvider); return Card( elevation: 0, shape: RoundedRectangleBorder( @@ -37,38 +35,20 @@ class _EditorPaneRequestURLCardState horizontal: 20, ), child: Row( - children: [ - const DropdownButtonHTTPMethod(), - const SizedBox( + children: const [ + DropdownButtonHTTPMethod(), + SizedBox( width: 20, ), - const Expanded( + Expanded( child: URLTextField(), ), - const SizedBox( + SizedBox( width: 20, ), SizedBox( height: 36, - child: FilledButton( - onPressed: () async { - ref - .read(collectionStateNotifierProvider.notifier) - .sendRequest(activeId!); - }, - child: Row( - children: const [ - Text( - "Send", - style: textStyleButton, - ), - SizedBox( - width: 10, - ), - Icon(size: 16, Icons.send), - ], - ), - ), + child: SendRequestButton(), ), ], ), @@ -172,3 +152,65 @@ class _URLTextFieldState extends ConsumerState { ); } } + +class SendRequestButton extends ConsumerStatefulWidget { + const SendRequestButton({ + super.key, + }); + + @override + ConsumerState createState() => _SendRequestButtonState(); +} + +class _SendRequestButtonState extends ConsumerState { + @override + void initState() { + super.initState(); + } + + @override + Widget build(BuildContext context) { + final activeId = ref.watch(activeIdStateProvider); + final sentRequestId = ref.watch(sentRequestIdStateProvider); + bool disable = sentRequestId != null; + return FilledButton( + onPressed: disable + ? null + : () async { + ref + .read(sentRequestIdStateProvider.notifier) + .update((state) => activeId); + await Future.delayed( + const Duration(seconds: 5), + () { + ref + .read(collectionStateNotifierProvider.notifier) + .sendRequest(activeId!); + }, + ); + ref + .read(sentRequestIdStateProvider.notifier) + .update((state) => null); + }, + child: Row( + children: [ + Text( + disable + ? (activeId == sentRequestId ? "Sending.." : "Busy") + : "Send", + style: textStyleButton, + ), + if (!disable) + const SizedBox( + width: 10, + ), + if (!disable) + const Icon( + size: 16, + Icons.send, + ), + ], + ), + ); + } +} diff --git a/lib/providers/providers.dart b/lib/providers/providers.dart index 06e4532f..7393e455 100644 --- a/lib/providers/providers.dart +++ b/lib/providers/providers.dart @@ -7,6 +7,7 @@ import '../consts.dart'; const _uuid = Uuid(); final activeIdStateProvider = StateProvider((ref) => null); +final sentRequestIdStateProvider = StateProvider((ref) => null); final StateNotifierProvider> collectionStateNotifierProvider =