fix button "to top" to not use "useState()"
This commit is contained in:
parent
cf6fce5ec5
commit
fa5c949153
@ -8,6 +8,17 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||
|
||||
HomeBloc(this.repo) : super(const HomeState()) {
|
||||
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(
|
||||
|
@ -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});
|
||||
}
|
||||
|
@ -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<Object?> get props => [data, isLoading, isPaginationLoading, error];
|
||||
List<Object?> get props =>
|
||||
[data, isLoading, isPaginationLoading, isButtonToTopShown, error];
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -24,9 +24,6 @@ class _HomePageState extends State<HomePage> {
|
||||
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<Body> {
|
||||
: 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<HomeBloc, HomeState>(
|
||||
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<Body> {
|
||||
}
|
||||
|
||||
void _viewListScrollListener() {
|
||||
if (!isButtonToTopShown) {
|
||||
if (!context.read<HomeBloc>().state.isButtonToTopShown) {
|
||||
if (scrollController.offset >= 400) {
|
||||
setState(() {
|
||||
isButtonToTopShown = true;
|
||||
});
|
||||
context.read<HomeBloc>().add(HomeShowButtonToTopEvent(isShown: true));
|
||||
}
|
||||
} else {
|
||||
if (scrollController.offset < 400) {
|
||||
setState(() {
|
||||
isButtonToTopShown = false;
|
||||
});
|
||||
context.read<HomeBloc>().add(HomeShowButtonToTopEvent(isShown: false));
|
||||
}
|
||||
}
|
||||
|
||||
if (scrollController.offset >= scrollController.position.maxScrollExtent) {
|
||||
final bloc = context.read<HomeBloc>();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user