From fa5c94915371398fcea0a545ede600aa945eaa53 Mon Sep 17 00:00:00 2001 From: shirotame Date: Sun, 6 Oct 2024 01:52:11 +0400 Subject: [PATCH] fix button "to top" to not use "useState()" --- lib/views/home_page/bloc/bloc.dart | 11 +++++++++ lib/views/home_page/bloc/events.dart | 6 +++++ lib/views/home_page/bloc/state.dart | 5 +++- lib/views/home_page/bloc/state.g.dart | 13 ++++++++++ lib/views/home_page/home_page.dart | 34 ++++++++++++--------------- 5 files changed, 49 insertions(+), 20 deletions(-) diff --git a/lib/views/home_page/bloc/bloc.dart b/lib/views/home_page/bloc/bloc.dart index 5440b21..c5dab10 100644 --- a/lib/views/home_page/bloc/bloc.dart +++ b/lib/views/home_page/bloc/bloc.dart @@ -8,6 +8,17 @@ class HomeBloc extends Bloc { HomeBloc(this.repo) : super(const HomeState()) { on(_onLoadData); + on(_onShowButton); + } + + void _onShowButton(HomeShowButtonToTopEvent event, Emitter emit) { + if (event.isShown != null) { + if (event.isShown == true) { + emit(state.copyWith(isButtonToTopShown: true)); + } else { + emit(state.copyWith(isButtonToTopShown: false)); + } + } } Future _onLoadData( diff --git a/lib/views/home_page/bloc/events.dart b/lib/views/home_page/bloc/events.dart index 4dad5f2..4baa736 100644 --- a/lib/views/home_page/bloc/events.dart +++ b/lib/views/home_page/bloc/events.dart @@ -9,3 +9,9 @@ class HomeLoadDataEvent extends HomeEvent { const HomeLoadDataEvent({this.search, this.nextPage, this.hasError}); } + +class HomeShowButtonToTopEvent extends HomeEvent { + final bool? isShown; + + const HomeShowButtonToTopEvent({this.isShown}); +} diff --git a/lib/views/home_page/bloc/state.dart b/lib/views/home_page/bloc/state.dart index f09099e..43d801d 100644 --- a/lib/views/home_page/bloc/state.dart +++ b/lib/views/home_page/bloc/state.dart @@ -9,14 +9,17 @@ class HomeState extends Equatable { final HomeData? data; final bool isLoading; final bool isPaginationLoading; + final bool isButtonToTopShown; final String? error; const HomeState( {this.data, this.isLoading = false, this.isPaginationLoading = false, + this.isButtonToTopShown = false, this.error}); @override - List get props => [data, isLoading, isPaginationLoading, error]; + List get props => + [data, isLoading, isPaginationLoading, isButtonToTopShown, error]; } diff --git a/lib/views/home_page/bloc/state.g.dart b/lib/views/home_page/bloc/state.g.dart index 114ac25..245f829 100644 --- a/lib/views/home_page/bloc/state.g.dart +++ b/lib/views/home_page/bloc/state.g.dart @@ -13,6 +13,8 @@ abstract class _$HomeStateCWProxy { HomeState isPaginationLoading(bool isPaginationLoading); + HomeState isButtonToTopShown(bool isButtonToTopShown); + HomeState error(String? error); /// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `HomeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support. @@ -25,6 +27,7 @@ abstract class _$HomeStateCWProxy { HomeData? data, bool? isLoading, bool? isPaginationLoading, + bool? isButtonToTopShown, String? error, }); } @@ -45,6 +48,10 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy { HomeState isPaginationLoading(bool isPaginationLoading) => this(isPaginationLoading: isPaginationLoading); + @override + HomeState isButtonToTopShown(bool isButtonToTopShown) => + this(isButtonToTopShown: isButtonToTopShown); + @override HomeState error(String? error) => this(error: error); @@ -60,6 +67,7 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy { Object? data = const $CopyWithPlaceholder(), Object? isLoading = const $CopyWithPlaceholder(), Object? isPaginationLoading = const $CopyWithPlaceholder(), + Object? isButtonToTopShown = const $CopyWithPlaceholder(), Object? error = const $CopyWithPlaceholder(), }) { return HomeState( @@ -77,6 +85,11 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy { ? _value.isPaginationLoading // ignore: cast_nullable_to_non_nullable : isPaginationLoading as bool, + isButtonToTopShown: isButtonToTopShown == const $CopyWithPlaceholder() || + isButtonToTopShown == null + ? _value.isButtonToTopShown + // ignore: cast_nullable_to_non_nullable + : isButtonToTopShown as bool, error: error == const $CopyWithPlaceholder() ? _value.error // ignore: cast_nullable_to_non_nullable diff --git a/lib/views/home_page/home_page.dart b/lib/views/home_page/home_page.dart index 85b3bc3..cb71721 100644 --- a/lib/views/home_page/home_page.dart +++ b/lib/views/home_page/home_page.dart @@ -24,9 +24,6 @@ class _HomePageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - flexibleSpace: GestureDetector( - onTap: () => print("to top"), - ), backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Center( child: Text( @@ -118,15 +115,17 @@ class _BodyState extends State { : const SizedBox.shrink()) ], ), - isButtonToTopShown - ? Container( - alignment: Alignment.topRight, - padding: EdgeInsets.only(right: 16, top: 64), - child: FloatingActionButton( - onPressed: _goToTop, - child: Icon(Icons.arrow_upward_rounded), - )) - : SizedBox.shrink() + BlocBuilder( + builder: (context, state) => state.isButtonToTopShown + ? Container( + alignment: Alignment.topRight, + padding: EdgeInsets.only(right: 16, top: 64), + child: FloatingActionButton( + onPressed: _goToTop, + child: Icon(Icons.arrow_upward_rounded), + )) + : SizedBox.shrink(), + ) ]); } @@ -158,19 +157,16 @@ class _BodyState extends State { } void _viewListScrollListener() { - if (!isButtonToTopShown) { + if (!context.read().state.isButtonToTopShown) { if (scrollController.offset >= 400) { - setState(() { - isButtonToTopShown = true; - }); + context.read().add(HomeShowButtonToTopEvent(isShown: true)); } } else { if (scrollController.offset < 400) { - setState(() { - isButtonToTopShown = false; - }); + context.read().add(HomeShowButtonToTopEvent(isShown: false)); } } + if (scrollController.offset >= scrollController.position.maxScrollExtent) { final bloc = context.read();