Skip to content

Commit b891c7c

Browse files
authored
[Gallery] Close Shrine cart on back button press for android (#288)
* Close Shrine cart on back button press for android
1 parent 81a8e98 commit b891c7c

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

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

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ class _ShrineAppState extends State<ShrineApp> with TickerProviderStateMixin {
7878
);
7979
}
8080

81+
// Closes the bottom sheet if it is open.
82+
Future<bool> _onWillPop() async {
83+
final status = _expandingController.status;
84+
if (status == AnimationStatus.completed ||
85+
status == AnimationStatus.forward) {
86+
_expandingController.reverse();
87+
return false;
88+
}
89+
90+
return true;
91+
}
92+
8193
@override
8294
Widget build(BuildContext context) {
8395
final bool isDesktop = isDisplayDesktop(context);
@@ -86,34 +98,37 @@ class _ShrineAppState extends State<ShrineApp> with TickerProviderStateMixin {
8698

8799
return ScopedModel<AppStateModel>(
88100
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,
101+
child: WillPopScope(
102+
onWillPop: _onWillPop,
103+
child: MaterialApp(
104+
navigatorKey: widget.navigatorKey,
105+
title: 'Shrine',
106+
debugShowCheckedModeBanner: false,
107+
home: LayoutCache(
108+
layouts: _layouts,
109+
child: PageStatus(
110+
menuController: _controller,
111+
cartController: _expandingController,
112+
child: HomePage(
113+
backdrop: backdrop,
114+
scrim: Scrim(controller: _expandingController),
115+
expandingBottomSheet: ExpandingBottomSheet(
116+
hideController: _controller,
117+
expandingController: _expandingController,
118+
),
104119
),
105120
),
106121
),
122+
initialRoute: '/login',
123+
onGenerateRoute: _getRoute,
124+
theme: shrineTheme.copyWith(
125+
platform: GalleryOptions.of(context).platform,
126+
),
127+
// L10n settings.
128+
localizationsDelegates: GalleryLocalizations.localizationsDelegates,
129+
supportedLocales: GalleryLocalizations.supportedLocales,
130+
locale: GalleryOptions.of(context).locale,
107131
),
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,
117132
),
118133
);
119134
}

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

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:async';
65
import 'dart:math';
76

87
import 'package:flutter/material.dart';
9-
import 'package:flutter/services.dart';
108
import 'package:meta/meta.dart';
119
import 'package:scoped_model/scoped_model.dart';
1210

@@ -51,11 +49,11 @@ double _paddedThumbnailHeight(BuildContext context) {
5149
}
5250

5351
class ExpandingBottomSheet extends StatefulWidget {
54-
const ExpandingBottomSheet(
55-
{Key key,
56-
@required this.hideController,
57-
@required this.expandingController})
58-
: assert(hideController != null),
52+
const ExpandingBottomSheet({
53+
Key key,
54+
@required this.hideController,
55+
@required this.expandingController,
56+
}) : assert(hideController != null),
5957
assert(expandingController != null),
6058
super(key: key);
6159

@@ -551,18 +549,6 @@ class _ExpandingBottomSheetState extends State<ExpandingBottomSheet>
551549
}
552550
}
553551

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-
566552
@override
567553
Widget build(BuildContext context) {
568554
return AnimatedSize(
@@ -571,16 +557,13 @@ class _ExpandingBottomSheetState extends State<ExpandingBottomSheet>
571557
curve: Curves.easeInOut,
572558
vsync: this,
573559
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-
),
560+
child: AnimatedBuilder(
561+
animation: widget.hideController,
562+
builder: (context, child) => AnimatedBuilder(
563+
animation: widget.expandingController,
564+
builder: (context, child) => ScopedModelDescendant<AppStateModel>(
565+
builder: (context, child, model) =>
566+
_buildSlideAnimation(context, _buildCart(context)),
584567
),
585568
),
586569
),

0 commit comments

Comments
 (0)