Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: replace deprecated WillPopScope with PopScope #120

Merged
merged 6 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.0.0

- **BREAKING**: replace deprecated `WillPopScope` with `PopScope`
- refactor: update to Flutter >=3.16.0, dart >=3.2.0

# 0.0.10

- feat: add optional `clipBehavior` ([#113](https://github.com/felangel/flow_builder/pull/113))
Expand Down
26 changes: 15 additions & 11 deletions lib/flow_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class _FlowBuilderState<T> extends State<FlowBuilder<T>> {
if (mounted) {
final popHandled = await _navigator?.maybePop() ?? false;
if (popHandled) return true;
if (mounted && !_canPop) return Navigator.of(context).maybePop();
if (mounted && !_canPop) _navigator?.pop();
return false;
}
return false;
Expand Down Expand Up @@ -176,12 +176,9 @@ class _FlowBuilderState<T> extends State<FlowBuilder<T>> {
Widget build(BuildContext context) {
return _InheritedFlowController(
controller: _controller,
child: _ConditionalWillPopScope(
child: _ConditionalPopScope(
condition: _canPop,
onWillPop: () async {
await _navigator?.maybePop();
return false;
},
onPopInvoked: (bool _) => _navigator?.maybePop(),
child: Navigator(
key: _navigatorKey,
pages: _pages,
Expand All @@ -197,6 +194,7 @@ class _FlowBuilderState<T> extends State<FlowBuilder<T>> {
_pages.removeLast();
}
setState(() {});
route.onPopInvoked(true);
return route.didPop(result);
},
),
Expand Down Expand Up @@ -329,20 +327,26 @@ class FakeFlowController<T> extends FlowController<T> {
}
}

class _ConditionalWillPopScope extends StatelessWidget {
const _ConditionalWillPopScope({
class _ConditionalPopScope extends StatelessWidget {
const _ConditionalPopScope({
required this.condition,
required this.onWillPop,
required this.onPopInvoked,
required this.child,
});

final bool condition;
final Widget child;
final Future<bool> Function() onWillPop;
final PopInvokedCallback onPopInvoked;

@override
Widget build(BuildContext context) {
return condition ? WillPopScope(onWillPop: onWillPop, child: child) : child;
return condition
? PopScope(
canPop: false,
onPopInvoked: onPopInvoked,
child: child,
)
: child;
}
}

Expand Down
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ name: flow_builder
description: Flutter Flows made easy! A Flutter package which simplifies flows with a flexible, declarative API.
repository: https://github.com/felangel/flow_builder
homepage: https://github.com/felangel/flow_builder
topics: [navigation, routing]
topics: [ navigation, routing ]

version: 0.0.10

environment:
sdk: ">=2.17.0 <3.0.0"
sdk: ^3.2.0
flutter: ^3.16.0

dependencies:
flutter:
Expand Down
52 changes: 24 additions & 28 deletions test/flow_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ void main() {
expect(find.byKey(button2Key), findsOneWidget);
});

testWidgets('onWillPop pops top page when there are multiple',
testWidgets('Navigator.pop pops top page when there are multiple',
(tester) async {
const button1Key = Key('__button1__');
const button2Key = Key('__button2__');
Expand All @@ -1084,10 +1084,14 @@ void main() {
),
MaterialPage<void>(
child: Scaffold(
body: TextButton(
key: button2Key,
child: const Text('Button'),
onPressed: () {},
body: Builder(
builder: (context) {
return TextButton(
key: button2Key,
child: const Text('Button'),
onPressed: () => Navigator.of(context).pop(),
);
},
),
),
),
Expand All @@ -1100,19 +1104,14 @@ void main() {
expect(find.byKey(button1Key), findsNothing);
expect(find.byKey(button2Key), findsOneWidget);

final willPopScope = tester.widget<WillPopScope>(
find.byType(WillPopScope),
);
final result = await willPopScope.onWillPop!();
expect(result, isFalse);

await tester.tap(find.byKey(button2Key));
await tester.pumpAndSettle();

expect(find.byKey(button1Key), findsOneWidget);
expect(find.byKey(button2Key), findsNothing);
});

testWidgets('onWillPop does not exist for only one page', (tester) async {
testWidgets('PopScope does not exist for only one page', (tester) async {
const button1Key = Key('__button1__');
await tester.pumpWidget(
MaterialApp(
Expand All @@ -1136,7 +1135,7 @@ void main() {
);

expect(find.byKey(button1Key), findsOneWidget);
expect(find.byType(WillPopScope), findsNothing);
expect(find.byType(PopScope), findsNothing);
});

testWidgets('controller change triggers a rebuild with correct state',
Expand Down Expand Up @@ -1389,21 +1388,19 @@ void main() {
expect(navigators.last.observers, equals(observers));
});

testWidgets('SystemNavigator.pop respects when WillPopScope returns false',
testWidgets('SystemNavigator.pop respects when PopScope(canPop: false)',
(tester) async {
const targetKey = Key('__target__');
var onWillPopCallCount = 0;
var onPopCallCount = 0;
final flow = FlowBuilder<int>(
state: 0,
onGeneratePages: (state, pages) {
return <Page<dynamic>>[
MaterialPage<void>(
child: Builder(
builder: (context) => WillPopScope(
onWillPop: () async {
onWillPopCallCount++;
return false;
},
builder: (context) => PopScope(
canPop: false,
onPopInvoked: (_) => onPopCallCount++,
child: TextButton(
key: targetKey,
onPressed: () {
Expand Down Expand Up @@ -1441,24 +1438,23 @@ void main() {
await tester.tap(find.byKey(targetKey));
await tester.pumpAndSettle();

expect(onWillPopCallCount, equals(1));
expect(onPopCallCount, equals(1));
expect(find.byKey(targetKey), findsOneWidget);
});

testWidgets('SystemNavigator.pop respects when WillPopScope returns true',
testWidgets('SystemNavigator.pop respects when PopScope(canPop: true)',
(tester) async {
const targetKey = Key('__target__');
var onWillPopCallCount = 0;
var onPopCallCount = 0;
final flow = FlowBuilder<int>(
state: 0,
onGeneratePages: (state, pages) {
return <Page<dynamic>>[
MaterialPage<void>(
child: Builder(
builder: (context) => WillPopScope(
onWillPop: () async {
onWillPopCallCount++;
return true;
builder: (context) => PopScope(
onPopInvoked: (_) {
onPopCallCount++;
},
child: TextButton(
key: targetKey,
Expand Down Expand Up @@ -1497,7 +1493,7 @@ void main() {
await tester.tap(find.byKey(targetKey));
await tester.pumpAndSettle();

expect(onWillPopCallCount, equals(1));
expect(onPopCallCount, equals(1));
expect(find.byKey(targetKey), findsNothing);
});

Expand Down