Add generic type for result in PopScope (#139164)

Adds a generic type and pop result to popscope and its friend.

The use cases are to be able to capture the result when the pop is called.

migration guide: https://github.com/flutter/website/pull/9872
This commit is contained in:
chunhtai
2024-04-18 12:30:10 -07:00
committed by GitHub
parent ffa3b303ee
commit 7ee05a05ff
18 changed files with 466 additions and 55 deletions

View File

@@ -111,7 +111,7 @@ class _SaveableFormState extends State<_SaveableForm> {
const SizedBox(height: 20.0),
Form(
canPop: !_isDirty,
onPopInvoked: (bool didPop) async {
onPopInvoked: (bool didPop, Object? result) async {
if (didPop) {
return;
}

View File

@@ -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 {
onPopInvoked: (bool didPop, Object? result) async {
if (didPop) {
return;
}

View File

@@ -0,0 +1,232 @@
// 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 showing how to use PopScope to wrap 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 contains pop result in `_PageTwo`.
onPopInvoked: (bool didPop, FormData? result) async {
if (didPop) {
return;
}
final bool shouldPop = await _showBackDialog(context) ?? false;
if (context.mounted && shouldPop) {
Navigator.pop(context, result);
}
},
child: const _PageTwoBody(),
);
}
}
// 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);
}

View File

@@ -0,0 +1,67 @@
// 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.
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/pop_scope/pop_scope.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';
import '../navigator_utils.dart';
void main() {
testWidgets('Can choose to stay on page', (WidgetTester tester) async {
await tester.pumpWidget(
const example.NavigatorPopHandlerApp(),
);
expect(find.text('Page One'), findsOneWidget);
await tester.tap(find.text('Next page'));
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
await simulateSystemBack();
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
expect(find.text('Are you sure?'), findsOneWidget);
await tester.tap(find.text('Never mind'));
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
});
testWidgets('Can choose to go back with pop result', (WidgetTester tester) async {
await tester.pumpWidget(
const example.NavigatorPopHandlerApp(),
);
expect(find.text('Page One'), findsOneWidget);
expect(find.text('Page Two'), findsNothing);
await tester.tap(find.text('Next page'));
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
await tester.enterText(find.byType(TextFormField).first, 'John');
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextFormField).last, 'Apple');
await tester.pumpAndSettle();
await tester.tap(find.text('Go back'));
await tester.pumpAndSettle();
expect(find.text('Page One'), findsNothing);
expect(find.text('Page Two'), findsOneWidget);
expect(find.text('Are you sure?'), findsOneWidget);
await tester.tap(find.text('Leave'));
await tester.pumpAndSettle();
expect(find.text('Page One'), findsOneWidget);
expect(find.text('Page Two'), findsNothing);
expect(find.text('Are you sure?'), findsNothing);
expect(find.text('Hello John, whose favorite food is Apple.'), findsOneWidget);
});
}