fix button "to top" to not use "useState()"

This commit is contained in:
shirotame 2024-10-06 01:52:11 +04:00
parent cf6fce5ec5
commit fa5c949153
5 changed files with 49 additions and 20 deletions

View File

@ -8,6 +8,17 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
HomeBloc(this.repo) : super(const HomeState()) { HomeBloc(this.repo) : super(const HomeState()) {
on<HomeLoadDataEvent>(_onLoadData); on<HomeLoadDataEvent>(_onLoadData);
on<HomeShowButtonToTopEvent>(_onShowButton);
}
void _onShowButton(HomeShowButtonToTopEvent event, Emitter<HomeState> emit) {
if (event.isShown != null) {
if (event.isShown == true) {
emit(state.copyWith(isButtonToTopShown: true));
} else {
emit(state.copyWith(isButtonToTopShown: false));
}
}
} }
Future<void> _onLoadData( Future<void> _onLoadData(

View File

@ -9,3 +9,9 @@ class HomeLoadDataEvent extends HomeEvent {
const HomeLoadDataEvent({this.search, this.nextPage, this.hasError}); const HomeLoadDataEvent({this.search, this.nextPage, this.hasError});
} }
class HomeShowButtonToTopEvent extends HomeEvent {
final bool? isShown;
const HomeShowButtonToTopEvent({this.isShown});
}

View File

@ -9,14 +9,17 @@ class HomeState extends Equatable {
final HomeData? data; final HomeData? data;
final bool isLoading; final bool isLoading;
final bool isPaginationLoading; final bool isPaginationLoading;
final bool isButtonToTopShown;
final String? error; final String? error;
const HomeState( const HomeState(
{this.data, {this.data,
this.isLoading = false, this.isLoading = false,
this.isPaginationLoading = false, this.isPaginationLoading = false,
this.isButtonToTopShown = false,
this.error}); this.error});
@override @override
List<Object?> get props => [data, isLoading, isPaginationLoading, error]; List<Object?> get props =>
[data, isLoading, isPaginationLoading, isButtonToTopShown, error];
} }

View File

@ -13,6 +13,8 @@ abstract class _$HomeStateCWProxy {
HomeState isPaginationLoading(bool isPaginationLoading); HomeState isPaginationLoading(bool isPaginationLoading);
HomeState isButtonToTopShown(bool isButtonToTopShown);
HomeState error(String? error); 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. /// 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, HomeData? data,
bool? isLoading, bool? isLoading,
bool? isPaginationLoading, bool? isPaginationLoading,
bool? isButtonToTopShown,
String? error, String? error,
}); });
} }
@ -45,6 +48,10 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
HomeState isPaginationLoading(bool isPaginationLoading) => HomeState isPaginationLoading(bool isPaginationLoading) =>
this(isPaginationLoading: isPaginationLoading); this(isPaginationLoading: isPaginationLoading);
@override
HomeState isButtonToTopShown(bool isButtonToTopShown) =>
this(isButtonToTopShown: isButtonToTopShown);
@override @override
HomeState error(String? error) => this(error: error); HomeState error(String? error) => this(error: error);
@ -60,6 +67,7 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
Object? data = const $CopyWithPlaceholder(), Object? data = const $CopyWithPlaceholder(),
Object? isLoading = const $CopyWithPlaceholder(), Object? isLoading = const $CopyWithPlaceholder(),
Object? isPaginationLoading = const $CopyWithPlaceholder(), Object? isPaginationLoading = const $CopyWithPlaceholder(),
Object? isButtonToTopShown = const $CopyWithPlaceholder(),
Object? error = const $CopyWithPlaceholder(), Object? error = const $CopyWithPlaceholder(),
}) { }) {
return HomeState( return HomeState(
@ -77,6 +85,11 @@ class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
? _value.isPaginationLoading ? _value.isPaginationLoading
// ignore: cast_nullable_to_non_nullable // ignore: cast_nullable_to_non_nullable
: isPaginationLoading as bool, : isPaginationLoading as bool,
isButtonToTopShown: isButtonToTopShown == const $CopyWithPlaceholder() ||
isButtonToTopShown == null
? _value.isButtonToTopShown
// ignore: cast_nullable_to_non_nullable
: isButtonToTopShown as bool,
error: error == const $CopyWithPlaceholder() error: error == const $CopyWithPlaceholder()
? _value.error ? _value.error
// ignore: cast_nullable_to_non_nullable // ignore: cast_nullable_to_non_nullable

View File

@ -24,9 +24,6 @@ class _HomePageState extends State<HomePage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
flexibleSpace: GestureDetector(
onTap: () => print("to top"),
),
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Center( title: Center(
child: Text( child: Text(
@ -118,15 +115,17 @@ class _BodyState extends State<Body> {
: const SizedBox.shrink()) : const SizedBox.shrink())
], ],
), ),
isButtonToTopShown BlocBuilder<HomeBloc, HomeState>(
? Container( builder: (context, state) => state.isButtonToTopShown
alignment: Alignment.topRight, ? Container(
padding: EdgeInsets.only(right: 16, top: 64), alignment: Alignment.topRight,
child: FloatingActionButton( padding: EdgeInsets.only(right: 16, top: 64),
onPressed: _goToTop, child: FloatingActionButton(
child: Icon(Icons.arrow_upward_rounded), onPressed: _goToTop,
)) child: Icon(Icons.arrow_upward_rounded),
: SizedBox.shrink() ))
: SizedBox.shrink(),
)
]); ]);
} }
@ -158,19 +157,16 @@ class _BodyState extends State<Body> {
} }
void _viewListScrollListener() { void _viewListScrollListener() {
if (!isButtonToTopShown) { if (!context.read<HomeBloc>().state.isButtonToTopShown) {
if (scrollController.offset >= 400) { if (scrollController.offset >= 400) {
setState(() { context.read<HomeBloc>().add(HomeShowButtonToTopEvent(isShown: true));
isButtonToTopShown = true;
});
} }
} else { } else {
if (scrollController.offset < 400) { if (scrollController.offset < 400) {
setState(() { context.read<HomeBloc>().add(HomeShowButtonToTopEvent(isShown: false));
isButtonToTopShown = false;
});
} }
} }
if (scrollController.offset >= scrollController.position.maxScrollExtent) { if (scrollController.offset >= scrollController.position.maxScrollExtent) {
final bloc = context.read<HomeBloc>(); final bloc = context.read<HomeBloc>();