Skip to content

Commit

Permalink
Reapply new PopScope API (#147607)
Browse files Browse the repository at this point in the history
same as previous but with soft transition on modalroute

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
[Data Driven Fixes]:
https://github.com/flutter/flutter/wiki/Data-driven-Fixes
  • Loading branch information
chunhtai authored May 7, 2024
1 parent 8dde449 commit 007faa9
Show file tree
Hide file tree
Showing 18 changed files with 577 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CupertinoNavigationDemo extends StatelessWidget {

@override
Widget build(BuildContext context) {
return PopScope(
return PopScope<Object?>(
// Prevent swipe popping of this page. Use explicit exit buttons only.
canPop: false,
child: DefaultTextStyle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
bool _hasName = false;
late String _eventName;

Future<void> _handlePopInvoked(bool didPop) async {
Future<void> _handlePopInvoked(bool didPop, Object? result) async {
if (didPop) {
return;
}
Expand Down Expand Up @@ -175,7 +175,7 @@ class FullScreenDialogDemoState extends State<FullScreenDialogDemo> {
),
body: Form(
canPop: !_saveNeeded && !_hasLocation && !_hasName,
onPopInvoked: _handlePopInvoked,
onPopInvokedWithResult: _handlePopInvoked,
child: Scrollbar(
child: ListView(
primary: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
return null;
}

Future<void> _handlePopInvoked(bool didPop) async {
Future<void> _handlePopInvoked(bool didPop, Object? result) async {
if (didPop) {
return;
}
Expand Down Expand Up @@ -192,7 +192,7 @@ class TextFormFieldDemoState extends State<TextFormFieldDemo> {
key: _formKey,
autovalidateMode: _autovalidateMode,
canPop: _formKey.currentState == null || !_formWasEdited || _formKey.currentState!.validate(),
onPopInvoked: _handlePopInvoked,
onPopInvokedWithResult: _handlePopInvoked,
child: Scrollbar(
child: SingleChildScrollView(
primary: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> with TickerP

// Closes the cart if the cart is open, otherwise exits the app (this should
// only be relevant for Android).
void _handlePopInvoked(bool didPop) {
void _handlePopInvoked(bool didPop, Object? result) {
if (didPop) {
return;
}
Expand All @@ -370,9 +370,9 @@ class ExpandingBottomSheetState extends State<ExpandingBottomSheet> with TickerP
duration: const Duration(milliseconds: 225),
curve: Curves.easeInOut,
alignment: FractionalOffset.topLeft,
child: PopScope(
child: PopScope<Object?>(
canPop: !_isOpen,
onPopInvoked: _handlePopInvoked,
onPopInvokedWithResult: _handlePopInvoked,
child: AnimatedBuilder(
animation: widget.hideController,
builder: _buildSlideAnimation,
Expand Down
4 changes: 2 additions & 2 deletions dev/integration_tests/flutter_gallery/lib/gallery/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ class _GalleryHomeState extends State<GalleryHome> with SingleTickerProviderStat
backgroundColor: isDark ? _kFlutterBlue : theme.primaryColor,
body: SafeArea(
bottom: false,
child: PopScope(
child: PopScope<Object?>(
canPop: _category == null,
onPopInvoked: (bool didPop) {
onPopInvokedWithResult: (bool didPop, Object? result) {
if (didPop) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/api/lib/widgets/form/form.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class _SaveableFormState extends State<_SaveableForm> {
const SizedBox(height: 20.0),
Form(
canPop: !_isDirty,
onPopInvoked: (bool didPop) async {
onPopInvokedWithResult: (bool didPop, Object? result) async {
if (didPop) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/api/lib/widgets/pop_scope/pop_scope.0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ class _PageTwoState extends State<_PageTwo> {
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Page Two'),
PopScope(
PopScope<Object?>(
canPop: false,
onPopInvoked: (bool didPop) async {
onPopInvokedWithResult: (bool didPop, Object? result) async {
if (didPop) {
return;
}
Expand Down
233 changes: 233 additions & 0 deletions examples/api/lib/widgets/pop_scope/pop_scope.1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This sample demonstrates how to use a PopScope to wrap a widget that
// may pop the page with a result.

import 'package:flutter/material.dart';

void main() => runApp(const NavigatorPopHandlerApp());

class NavigatorPopHandlerApp extends StatelessWidget {
const NavigatorPopHandlerApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/home',
onGenerateRoute: (RouteSettings settings) {
return switch (settings.name) {
'/two' => MaterialPageRoute<FormData>(
builder: (BuildContext context) => const _PageTwo(),
),
_ => MaterialPageRoute<void>(
builder: (BuildContext context) => const _HomePage(),
),
};
},
);
}
}

class _HomePage extends StatefulWidget {
const _HomePage();

@override
State<_HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<_HomePage> {
FormData? _formData;

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Page One'),
if (_formData != null)
Text('Hello ${_formData!.name}, whose favorite food is ${_formData!.favoriteFood}.'),
TextButton(
onPressed: () async {
final FormData formData =
await Navigator.of(context).pushNamed<FormData?>('/two')
?? const FormData();
if (formData != _formData) {
setState(() {
_formData = formData;
});
}
},
child: const Text('Next page'),
),
],
),
),
);
}
}

class _PopScopeWrapper extends StatelessWidget {
const _PopScopeWrapper({required this.child});

final Widget child;

Future<bool?> _showBackDialog(BuildContext context) {
return showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Are you sure?'),
content: const Text(
'Are you sure you want to leave this page?',
),
actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Never mind'),
onPressed: () {
Navigator.pop(context, false);
},
),
TextButton(
style: TextButton.styleFrom(
textStyle: Theme.of(context).textTheme.labelLarge,
),
child: const Text('Leave'),
onPressed: () {
Navigator.pop(context, true);
},
),
],
);
},
);
}

@override
Widget build(BuildContext context) {
return PopScope<FormData>(
canPop: false,
// The result argument contains the pop result that is defined in `_PageTwo`.
onPopInvokedWithResult: (bool didPop, FormData? result) async {
if (didPop) {
return;
}
final bool shouldPop = await _showBackDialog(context) ?? false;
if (context.mounted && shouldPop) {
Navigator.pop(context, result);
}
},
child: child,
);
}
}

// This is a PopScope wrapper over _PageTwoBody
class _PageTwo extends StatelessWidget {
const _PageTwo();

@override
Widget build(BuildContext context) {
return const _PopScopeWrapper(
child: _PageTwoBody(),
);
}

}

class _PageTwoBody extends StatefulWidget {
const _PageTwoBody();

@override
State<_PageTwoBody> createState() => _PageTwoBodyState();
}

class _PageTwoBodyState extends State<_PageTwoBody> {
FormData _formData = const FormData();

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('Page Two'),
Form(
child: Column(
children: <Widget>[
TextFormField(
decoration: const InputDecoration(
hintText: 'Enter your name.',
),
onChanged: (String value) {
_formData = _formData.copyWith(
name: value,
);
},
),
TextFormField(
decoration: const InputDecoration(
hintText: 'Enter your favorite food.',
),
onChanged: (String value) {
_formData = _formData.copyWith(
favoriteFood: value,
);
},
),
],
),
),
TextButton(
onPressed: () async {
Navigator.maybePop(context, _formData);
},
child: const Text('Go back'),
),
],
),
),
);
}
}

@immutable
class FormData {
const FormData({
this.name = '',
this.favoriteFood = '',
});

final String name;
final String favoriteFood;

FormData copyWith({String? name, String? favoriteFood}) {
return FormData(
name: name ?? this.name,
favoriteFood: favoriteFood ?? this.favoriteFood,
);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is FormData
&& other.name == name
&& other.favoriteFood == favoriteFood;
}

@override
int get hashCode => Object.hash(name, favoriteFood);
}
Loading

0 comments on commit 007faa9

Please sign in to comment.