fix: improve homepage scrolling behavior and clean up logic
This commit is contained in:
parent
0038efc13c
commit
3ad68594e2
|
@ -6,6 +6,17 @@ import 'package:furman_now/src/screens/home/content.dart';
|
||||||
import 'package:furman_now/src/screens/home/state.dart';
|
import 'package:furman_now/src/screens/home/state.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
class HomeScreenProvider extends StatelessWidget {
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return ChangeNotifierProvider(
|
||||||
|
create: (context) => HomePageState(),
|
||||||
|
child: const HomeScreen(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
class HomeScreen extends StatefulWidget {
|
class HomeScreen extends StatefulWidget {
|
||||||
const HomeScreen({Key? key}) : super(key: key);
|
const HomeScreen({Key? key}) : super(key: key);
|
||||||
|
|
||||||
|
@ -28,10 +39,11 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||||
|
|
||||||
updateScrollPosition() {
|
updateScrollPosition() {
|
||||||
// bottom overscroll
|
// bottom overscroll
|
||||||
if (_controller.position.pixels > _controller.position.maxScrollExtent) {
|
var bottomOverscroll =
|
||||||
|
_controller.position.pixels - _controller.position.maxScrollExtent;
|
||||||
|
if (bottomOverscroll > 0) {
|
||||||
setState(() {
|
setState(() {
|
||||||
overscrollBottomAmount =
|
overscrollBottomAmount = bottomOverscroll;
|
||||||
_controller.position.pixels - _controller.position.maxScrollExtent;
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (overscrollBottomAmount != 0) {
|
if (overscrollBottomAmount != 0) {
|
||||||
|
@ -42,10 +54,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// top overscroll
|
// top overscroll
|
||||||
if (_controller.position.pixels < 0) {
|
var topOverscroll = -_controller.position.pixels;
|
||||||
|
if (topOverscroll > 0) {
|
||||||
setState(() {
|
setState(() {
|
||||||
overscrollTopAmount =
|
overscrollTopAmount = topOverscroll;
|
||||||
_controller.position.pixels.abs();
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (overscrollTopAmount != 0) {
|
if (overscrollTopAmount != 0) {
|
||||||
|
@ -54,85 +66,77 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleTopOverscroll(topOverscroll);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleScrollEvent(BuildContext context) {
|
handleTopOverscroll(double overscrollTopAmount) {
|
||||||
const scrollMessageSensitivity = 20;
|
const scrollMessageSensitivity = 20;
|
||||||
const pageSwitchSensitivity = 60;
|
const pageSwitchSensitivity = 60;
|
||||||
var offsetAmount = overscrollTopAmount.abs();
|
var collapsed = context.read<HomePageState>().collapse;
|
||||||
|
|
||||||
if (overscrollTopAmount != 0) {
|
if (!collapsed) {
|
||||||
if (offsetAmount > pageSwitchSensitivity) {
|
if (overscrollTopAmount > pageSwitchSensitivity) {
|
||||||
|
// switch to student ID page
|
||||||
context.router.navigate(const StudentIdRoute());
|
context.router.navigate(const StudentIdRoute());
|
||||||
context.read<HomePageState>().collapse = true;
|
context.read<HomePageState>().collapse = true;
|
||||||
} else if (offsetAmount > scrollMessageSensitivity) {
|
} else if (overscrollTopAmount > scrollMessageSensitivity) {
|
||||||
|
// show student ID hint
|
||||||
context.read<HomePageState>().showScrollMessage = true;
|
context.read<HomePageState>().showScrollMessage = true;
|
||||||
} else {
|
} else {
|
||||||
|
// hide student ID hint
|
||||||
context.read<HomePageState>().showScrollMessage = false;
|
context.read<HomePageState>().showScrollMessage = false;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
context.read<HomePageState>().showScrollMessage = false;
|
|
||||||
|
|
||||||
if (_controller.position.pixels > pageSwitchSensitivity) {
|
if (collapsed && -overscrollTopAmount > pageSwitchSensitivity) {
|
||||||
context.router.navigate(const HomeRoute());
|
// switch back to home page
|
||||||
context.read<HomePageState>().collapse = false;
|
context.router.navigate(const HomeRoute());
|
||||||
}
|
context.read<HomePageState>().collapse = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return ChangeNotifierProvider(
|
return Scaffold(
|
||||||
create: (context) => HomePageState(),
|
body: AnnotatedRegion<SystemUiOverlayStyle>(
|
||||||
builder: (context, _) => Scaffold(
|
value: SystemUiOverlayStyle.light,
|
||||||
body: AnnotatedRegion<SystemUiOverlayStyle>(
|
child: Container(
|
||||||
value: SystemUiOverlayStyle.light,
|
color: const Color(0xffb7acc9),
|
||||||
|
child: SafeArea(
|
||||||
child: Container(
|
child: Container(
|
||||||
color: const Color(0xffb7acc9),
|
color: const Color(0xffb7acc9),
|
||||||
child: SafeArea(
|
child: Stack(
|
||||||
child: Container(
|
fit: StackFit.loose,
|
||||||
color: const Color(0xffb7acc9),
|
children: [
|
||||||
child: Stack(
|
const AutoTabsRouter(
|
||||||
fit: StackFit.loose,
|
routes: [
|
||||||
children: [
|
HomeRoute(),
|
||||||
// overscroll indicator color
|
StudentIdRoute(),
|
||||||
Align(
|
|
||||||
alignment: Alignment.bottomCenter,
|
|
||||||
child: Container(
|
|
||||||
height: overscrollBottomAmount + 30,
|
|
||||||
color: Colors.grey.shade50,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const AutoTabsRouter(
|
|
||||||
routes: [
|
|
||||||
HomeRoute(),
|
|
||||||
StudentIdRoute(),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
NotificationListener<ScrollNotification>(
|
|
||||||
onNotification: (notification) {
|
|
||||||
if (notification is ScrollUpdateNotification) {
|
|
||||||
handleScrollEvent(context);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
child: Consumer<HomePageState>(
|
|
||||||
builder: (context, state, _) =>
|
|
||||||
ClipRect(child: HomeContent(
|
|
||||||
controller: _controller,
|
|
||||||
collapse: state.collapse,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
// overscroll indicator color
|
||||||
|
Align(
|
||||||
|
alignment: Alignment.bottomCenter,
|
||||||
|
child: Container(
|
||||||
|
height: overscrollBottomAmount + 30,
|
||||||
|
color: Colors.grey.shade50,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Consumer<HomePageState>(
|
||||||
|
builder: (context, state, _) =>
|
||||||
|
ClipRect(child: HomeContent(
|
||||||
|
controller: _controller,
|
||||||
|
collapse: state.collapse,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue