From b2db3bafef52f942c22e92934a0ca154c2bb3f4c Mon Sep 17 00:00:00 2001 From: xster Date: Thu, 3 May 2018 11:15:51 -0700 Subject: [PATCH] Make Android back button presses in a demo category not unexpectedly quit the app (#17224) --- .../flutter_gallery/lib/gallery/home.dart | 77 ++++++++++--------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/examples/flutter_gallery/lib/gallery/home.dart b/examples/flutter_gallery/lib/gallery/home.dart index ef1e8ff1a67..c487a5c76bb 100644 --- a/examples/flutter_gallery/lib/gallery/home.dart +++ b/examples/flutter_gallery/lib/gallery/home.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; import 'dart:developer'; import 'dart:math' as math; @@ -298,42 +299,46 @@ class _GalleryHomeState extends State with SingleTickerProviderStat backgroundColor: isDark ? _kFlutterBlue : theme.primaryColor, body: new SafeArea( bottom: false, - child: new Backdrop( - backTitle: const Text('Options'), - backLayer: widget.optionsPage, - frontAction: new AnimatedSwitcher( - duration: _kFrontLayerSwitchDuration, - child: _category == null - ? const _FlutterLogo() - : new IconButton( - icon: const BackButtonIcon(), - tooltip: 'Back', - onPressed: () { - setState(() { - _category = null; - }); - }, - ), - ), - frontTitle: new AnimatedSwitcher( - duration: _kFrontLayerSwitchDuration, - child: _category == null - ? const Text('Flutter gallery') - : new Text(_category.name), - ), - frontHeading: new Container(height: 24.0), - frontLayer: new AnimatedSwitcher( - duration: _kFrontLayerSwitchDuration, - child: _category != null - ? new _DemosPage(_category) - : new _CategoriesPage( - categories: kAllGalleryDemoCategories, - onCategoryTap: (GalleryDemoCategory category) { - setState(() { - _category = category; - }); - }, - ), + child: new WillPopScope( + onWillPop: () { + // Pop the category page if Android back button is pressed. + if (_category != null) { + setState(() => _category = null); + return new Future.value(false); + } + return new Future.value(true); + }, + child: new Backdrop( + backTitle: const Text('Options'), + backLayer: widget.optionsPage, + frontAction: new AnimatedSwitcher( + duration: _kFrontLayerSwitchDuration, + child: _category == null + ? const _FlutterLogo() + : new IconButton( + icon: const BackButtonIcon(), + tooltip: 'Back', + onPressed: () => setState(() => _category = null), + ), + ), + frontTitle: new AnimatedSwitcher( + duration: _kFrontLayerSwitchDuration, + child: _category == null + ? const Text('Flutter gallery') + : new Text(_category.name), + ), + frontHeading: new Container(height: 24.0), + frontLayer: new AnimatedSwitcher( + duration: _kFrontLayerSwitchDuration, + child: _category != null + ? new _DemosPage(_category) + : new _CategoriesPage( + categories: kAllGalleryDemoCategories, + onCategoryTap: (GalleryDemoCategory category) { + setState(() => _category = category); + }, + ), + ), ), ), ),