Skip to content

Commit 87ea3b1

Browse files
committed
Close Shrine cart on back button press for android
1 parent e7f39b1 commit 87ea3b1

File tree

2 files changed

+52
-51
lines changed

2 files changed

+52
-51
lines changed

gallery/gallery/lib/studies/shrine/app.dart

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/material.dart';
6+
import 'package:flutter/services.dart';
67
import 'package:gallery/data/gallery_options.dart';
78
import 'package:gallery/l10n/gallery_localizations.dart';
89
import 'package:gallery/layout/adaptive.dart';
@@ -78,6 +79,18 @@ class _ShrineAppState extends State<ShrineApp> with TickerProviderStateMixin {
7879
);
7980
}
8081

82+
// Closes the bottom sheet if it is open (this should only be relevant for Android).
83+
Future<bool> _onWillPop() async {
84+
final AnimationStatus status = _expandingController.status;
85+
if (status == AnimationStatus.completed ||
86+
status == AnimationStatus.forward) {
87+
_expandingController.reverse();
88+
return false;
89+
}
90+
91+
return true;
92+
}
93+
8194
@override
8295
Widget build(BuildContext context) {
8396
final bool isDesktop = isDisplayDesktop(context);
@@ -86,34 +99,37 @@ class _ShrineAppState extends State<ShrineApp> with TickerProviderStateMixin {
8699

87100
return ScopedModel<AppStateModel>(
88101
model: _model,
89-
child: MaterialApp(
90-
navigatorKey: widget.navigatorKey,
91-
title: 'Shrine',
92-
debugShowCheckedModeBanner: false,
93-
home: LayoutCache(
94-
layouts: _layouts,
95-
child: PageStatus(
96-
menuController: _controller,
97-
cartController: _expandingController,
98-
child: HomePage(
99-
backdrop: backdrop,
100-
scrim: Scrim(controller: _expandingController),
101-
expandingBottomSheet: ExpandingBottomSheet(
102-
hideController: _controller,
103-
expandingController: _expandingController,
102+
child: WillPopScope(
103+
onWillPop: _onWillPop,
104+
child: MaterialApp(
105+
navigatorKey: widget.navigatorKey,
106+
title: 'Shrine',
107+
debugShowCheckedModeBanner: false,
108+
home: LayoutCache(
109+
layouts: _layouts,
110+
child: PageStatus(
111+
menuController: _controller,
112+
cartController: _expandingController,
113+
child: HomePage(
114+
backdrop: backdrop,
115+
scrim: Scrim(controller: _expandingController),
116+
expandingBottomSheet: ExpandingBottomSheet(
117+
hideController: _controller,
118+
expandingController: _expandingController,
119+
),
104120
),
105121
),
106122
),
123+
initialRoute: '/login',
124+
onGenerateRoute: _getRoute,
125+
theme: shrineTheme.copyWith(
126+
platform: GalleryOptions.of(context).platform,
127+
),
128+
// L10n settings.
129+
localizationsDelegates: GalleryLocalizations.localizationsDelegates,
130+
supportedLocales: GalleryLocalizations.supportedLocales,
131+
locale: GalleryOptions.of(context).locale,
107132
),
108-
initialRoute: '/login',
109-
onGenerateRoute: _getRoute,
110-
theme: shrineTheme.copyWith(
111-
platform: GalleryOptions.of(context).platform,
112-
),
113-
// L10n settings.
114-
localizationsDelegates: GalleryLocalizations.localizationsDelegates,
115-
supportedLocales: GalleryLocalizations.supportedLocales,
116-
locale: GalleryOptions.of(context).locale,
117133
),
118134
);
119135
}

gallery/gallery/lib/studies/shrine/expanding_bottom_sheet.dart

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ double _paddedThumbnailHeight(BuildContext context) {
5151
}
5252

5353
class ExpandingBottomSheet extends StatefulWidget {
54-
const ExpandingBottomSheet(
55-
{Key key,
56-
@required this.hideController,
57-
@required this.expandingController})
58-
: assert(hideController != null),
54+
const ExpandingBottomSheet({
55+
Key key,
56+
@required this.hideController,
57+
@required this.expandingController,
58+
}) : assert(hideController != null),
5959
assert(expandingController != null),
6060
super(key: key);
6161

@@ -551,18 +551,6 @@ class _ExpandingBottomSheetState extends State<ExpandingBottomSheet>
551551
}
552552
}
553553

554-
// Closes the cart if the cart is open, otherwise exits the app (this should
555-
// only be relevant for Android).
556-
Future<bool> _onWillPop() async {
557-
if (!_isOpen) {
558-
await SystemNavigator.pop();
559-
return true;
560-
}
561-
562-
close();
563-
return true;
564-
}
565-
566554
@override
567555
Widget build(BuildContext context) {
568556
return AnimatedSize(
@@ -571,16 +559,13 @@ class _ExpandingBottomSheetState extends State<ExpandingBottomSheet>
571559
curve: Curves.easeInOut,
572560
vsync: this,
573561
alignment: AlignmentDirectional.topStart,
574-
child: WillPopScope(
575-
onWillPop: _onWillPop,
576-
child: AnimatedBuilder(
577-
animation: widget.hideController,
578-
builder: (context, child) => AnimatedBuilder(
579-
animation: widget.expandingController,
580-
builder: (context, child) => ScopedModelDescendant<AppStateModel>(
581-
builder: (context, child, model) =>
582-
_buildSlideAnimation(context, _buildCart(context)),
583-
),
562+
child: AnimatedBuilder(
563+
animation: widget.hideController,
564+
builder: (context, child) => AnimatedBuilder(
565+
animation: widget.expandingController,
566+
builder: (context, child) => ScopedModelDescendant<AppStateModel>(
567+
builder: (context, child, model) =>
568+
_buildSlideAnimation(context, _buildCart(context)),
584569
),
585570
),
586571
),

0 commit comments

Comments
 (0)