From c7eb14ccf7d017baa142d2436d8df49ef2f47dcc Mon Sep 17 00:00:00 2001 From: Elian Ortega Date: Mon, 5 Apr 2021 22:54:42 -0600 Subject: [PATCH] Disable button on loading --- lib/src/features/jokes/logic/jokes_state.dart | 4 +++ lib/src/features/jokes/views/joke_page.dart | 26 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/src/features/jokes/logic/jokes_state.dart b/lib/src/features/jokes/logic/jokes_state.dart index a312834..936604e 100644 --- a/lib/src/features/jokes/logic/jokes_state.dart +++ b/lib/src/features/jokes/logic/jokes_state.dart @@ -3,6 +3,10 @@ import 'package:jokes/jokes.dart'; part 'jokes_state.freezed.dart'; +extension JokesGetters on JokesState { + bool get isLoading => this is _Loading; +} + @freezed abstract class JokesState with _$JokesState { /// Data is present state diff --git a/lib/src/features/jokes/views/joke_page.dart b/lib/src/features/jokes/views/joke_page.dart index 80e3020..f7b897e 100644 --- a/lib/src/features/jokes/views/joke_page.dart +++ b/lib/src/features/jokes/views/joke_page.dart @@ -27,13 +27,7 @@ class JokePage extends StatelessWidget { children: [ const SizedBox(height: 5), const JokeConsumer(), - CupertinoButton.filled( - key: getJokeButtonKey, - child: Text(kGiveMeAJoke.i18n), - onPressed: () { - context.read(jokesNotifierProvider.notifier).getJoke(); - }, - ), + const _GetJokeButton(), AppVersion(), const SizedBox(height: 5), ], @@ -78,3 +72,21 @@ class _Message extends StatelessWidget { ); } } + +class _GetJokeButton extends ConsumerWidget { + const _GetJokeButton(); + @override + Widget build(BuildContext context, ScopedReader watch) { + final state = watch(jokesNotifierProvider); + + return CupertinoButton.filled( + key: getJokeButtonKey, + child: Text(kGiveMeAJoke.i18n), + onPressed: !state.isLoading + ? () { + context.read(jokesNotifierProvider.notifier).getJoke(); + } + : null, + ); + } +}