From 2653dbd8841e2eec117e6b5e40e9d1180e27dc20 Mon Sep 17 00:00:00 2001 From: Simone Sestito Date: Mon, 29 Oct 2018 12:09:27 +0100 Subject: [PATCH] Go to HomeFragment on back pressed --- lib/route/HomeRoute.dart | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/route/HomeRoute.dart b/lib/route/HomeRoute.dart index 77d81fe..951df66 100644 --- a/lib/route/HomeRoute.dart +++ b/lib/route/HomeRoute.dart @@ -45,7 +45,12 @@ class HomeRouteState extends State { children: [ Padding( padding: EdgeInsets.only(bottom: BOTTOM_BAR_HEIGHT), - child: SafeArea(child: currentFragment), + child: SafeArea( + child: WillPopScope( + child: currentFragment, + onWillPop: _willPopCallback, + ), + ), ), Align( alignment: Alignment.bottomCenter, @@ -53,11 +58,14 @@ class HomeRouteState extends State { buttons: [ Image.asset("assets/img/ic_home_news.png", color: Colors.white), GestureDetector( - child: RocketHero(), - onVerticalDragEnd: (_) => - Navigator.of(context).pushNamed("/faq")), - Image.asset("assets/img/home_collection.png", - color: Colors.white), + child: RocketHero(), + onVerticalDragEnd: (_) => + Navigator.of(context).pushNamed("/faq"), + ), + Image.asset( + "assets/img/home_collection.png", + color: Colors.white, + ), ], clickListener: (index) { switch (index) { @@ -88,4 +96,17 @@ class HomeRouteState extends State { ), ); } + + Future _willPopCallback() async { + if (currentFragment is HomeFragment) { + // Call .pop() as usual + return true; + } else { + // Go back to home fragment + setState(() { + currentFragment = HomeFragment(); + }); + return false; + } + } }