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()) {
|
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(
|
||||||
|
@ -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});
|
||||||
|
}
|
||||||
|
@ -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];
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -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,7 +115,8 @@ class _BodyState extends State<Body> {
|
|||||||
: const SizedBox.shrink())
|
: const SizedBox.shrink())
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
isButtonToTopShown
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
|
builder: (context, state) => state.isButtonToTopShown
|
||||||
? Container(
|
? Container(
|
||||||
alignment: Alignment.topRight,
|
alignment: Alignment.topRight,
|
||||||
padding: EdgeInsets.only(right: 16, top: 64),
|
padding: EdgeInsets.only(right: 16, top: 64),
|
||||||
@ -126,7 +124,8 @@ class _BodyState extends State<Body> {
|
|||||||
onPressed: _goToTop,
|
onPressed: _goToTop,
|
||||||
child: Icon(Icons.arrow_upward_rounded),
|
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>();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user