forked from firka/firka
home_subpage: handle back events
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
import 'package:firka/ui/phone/screens/home/home_screen.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../helpers/firka_state.dart';
|
||||
import '../../../../helpers/update_notifier.dart';
|
||||
import '../../../model/style.dart';
|
||||
|
||||
class PageWithSubPages extends StatefulWidget {
|
||||
final int pageIndex;
|
||||
final List<Widget Function(void Function(int))> subPages;
|
||||
final ValueNotifier<bool> subPageActive;
|
||||
final UpdateNotifier back;
|
||||
|
||||
const PageWithSubPages(this.subPages, {Key? key, required this.pageIndex})
|
||||
const PageWithSubPages(this.subPages, this.subPageActive, this.back,
|
||||
{Key? key, required this.pageIndex})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
@@ -17,11 +22,43 @@ class PageWithSubPages extends StatefulWidget {
|
||||
class _PageWithSubPagesState extends FirkaState<PageWithSubPages> {
|
||||
int _currentSubPage = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
widget.back.addListener(_backListener);
|
||||
}
|
||||
|
||||
void _backListener() {
|
||||
if (!mounted) return;
|
||||
|
||||
setState(() {
|
||||
subPageActive.value = false;
|
||||
_currentSubPage = 0;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(PageWithSubPages oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
|
||||
widget.back.removeListener(_backListener);
|
||||
widget.back.addListener(_backListener);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
|
||||
widget.back.removeListener(_backListener);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: appStyle.colors.background,
|
||||
body: widget.subPages[_currentSubPage]((page) {
|
||||
subPageActive.value = _currentSubPage == 0;
|
||||
setState(() {
|
||||
_currentSubPage = page;
|
||||
});
|
||||
|
||||
@@ -55,6 +55,8 @@ enum ActiveToastType { fetching, error, reauth, none }
|
||||
bool _fetching = true;
|
||||
bool _prefetched = false;
|
||||
bool canPop = true;
|
||||
ValueNotifier<bool> subPageActive = ValueNotifier(false);
|
||||
UpdateNotifier subPageBack = UpdateNotifier();
|
||||
|
||||
class _HomeScreenState extends FirkaState<HomeScreen> {
|
||||
_HomeScreenState();
|
||||
@@ -380,7 +382,7 @@ class _HomeScreenState extends FirkaState<HomeScreen> {
|
||||
return DefaultAssetBundle(
|
||||
bundle: FirkaBundle(),
|
||||
child: PopScope(
|
||||
canPop: canPop,
|
||||
canPop: canPop || subPageActive.value,
|
||||
child: Scaffold(
|
||||
backgroundColor: appStyle.colors.background,
|
||||
body: SafeArea(
|
||||
@@ -549,21 +551,25 @@ class _HomeScreenState extends FirkaState<HomeScreen> {
|
||||
),
|
||||
),
|
||||
),
|
||||
onPopInvokedWithResult: (_, __) => {
|
||||
if (previousPages.isNotEmpty && page != previousPages.last)
|
||||
{
|
||||
setState(() {
|
||||
page = previousPages.removeLast();
|
||||
onPopInvokedWithResult: (_, __) {
|
||||
if (subPageActive.value) {
|
||||
subPageBack.update();
|
||||
return;
|
||||
}
|
||||
|
||||
forcedNav++;
|
||||
_pageController.animateToPage(
|
||||
page.index,
|
||||
duration: Duration(milliseconds: 175),
|
||||
curve: Curves.easeInOut,
|
||||
);
|
||||
canPop = previousPages.isEmpty;
|
||||
})
|
||||
}
|
||||
if (previousPages.isNotEmpty && page != previousPages.last) {
|
||||
setState(() {
|
||||
page = previousPages.removeLast();
|
||||
|
||||
forcedNav++;
|
||||
_pageController.animateToPage(
|
||||
page.index,
|
||||
duration: Duration(milliseconds: 175),
|
||||
curve: Curves.easeInOut,
|
||||
);
|
||||
canPop = previousPages.isEmpty;
|
||||
});
|
||||
}
|
||||
},
|
||||
));
|
||||
}
|
||||
@@ -601,14 +607,14 @@ class HomeSubPage extends StatelessWidget {
|
||||
data, _updateNotifier, _updateFinishNotifier, cb),
|
||||
(cb) => HomeGradesSubjectScreen(
|
||||
data, _updateNotifier, _updateFinishNotifier)
|
||||
], pageIndex: 0);
|
||||
], subPageActive, subPageBack, pageIndex: 0);
|
||||
case HomePage.timetable:
|
||||
return PageWithSubPages([
|
||||
(cb) => HomeTimetableScreen(
|
||||
data, _updateNotifier, _updateFinishNotifier, cb),
|
||||
(cb) => HomeTimetableMonthlyScreen(
|
||||
data, _updateNotifier, _updateFinishNotifier, cb)
|
||||
], pageIndex: 0);
|
||||
], subPageActive, subPageBack, pageIndex: 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user