Skip to content

Commit

Permalink
Merge pull request #431 from remonke/remove-willpopscope
Browse files Browse the repository at this point in the history
Remove WillPopScope
  • Loading branch information
joanpablo committed Mar 29, 2024
2 parents 94c928d + 0568710 commit 9722066
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 35 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/reactive_forms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
with:
flutter-version: "3.10.0"
flutter-version: "3.16.0"
channel: "stable"
- run: flutter pub get
- run: flutter test --no-pub --coverage
Expand All @@ -36,7 +36,7 @@ jobs:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
with:
flutter-version: "3.10.0"
flutter-version: "3.16.0"
channel: "stable"
- run: flutter pub get
- name: Analyze lib
Expand All @@ -51,7 +51,7 @@ jobs:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
with:
flutter-version: "3.10.0"
flutter-version: "3.16.0"
channel: "stable"
- run: flutter pub get
- name: Format lib
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 17.0.0

## Breaking changes

- Remove `onWillPop` from `ReactiveForm` and `ReactiveFormBuilder` widgets.

## Features

- Add `canPop` and `onPopInvoked` to `ReactiveForm` and `ReactiveFormBuilder` widgets.

# 16.1.1

## Fixes
Expand Down
31 changes: 15 additions & 16 deletions lib/src/widgets/reactive_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:flutter/material.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:reactive_forms/src/widgets/form_control_inherited_notifier.dart';
import 'package:reactive_forms/src/widgets/reactive_form_pop_scope.dart';

/// This class is responsible for create a [FormControlInheritedStreamer] for
/// exposing a [FormGroup] to all descendants widgets.
Expand All @@ -18,17 +19,11 @@ class ReactiveForm extends StatelessWidget {
/// The form group control that is bound to this widget.
final FormGroup formGroup;

/// Enables the form to veto attempts by the user to dismiss the [ModalRoute]
/// that contains the form.
///
/// If the callback returns a Future that resolves to false, the form's route
/// will not be popped.
///
/// See also:
///
/// * [WillPopScope], another widget that provides a way to intercept the
/// back button.
final WillPopCallback? onWillPop;
/// Determine whether a route can popped. See [PopScope] for more details.
final bool Function(FormGroup formGroup)? canPop;

/// A callback invoked when a route is popped. See [PopScope] for more details.
final void Function(FormGroup formGroup, bool didPop)? onPopInvoked;

/// Creates and instance of [ReactiveForm].
///
Expand All @@ -37,7 +32,8 @@ class ReactiveForm extends StatelessWidget {
Key? key,
required this.formGroup,
required this.child,
this.onWillPop,
this.canPop,
this.onPopInvoked,
}) : super(key: key);

/// Returns the nearest model up its widget tree.
Expand Down Expand Up @@ -67,10 +63,13 @@ class ReactiveForm extends StatelessWidget {
return FormControlInheritedStreamer(
control: formGroup,
stream: formGroup.statusChanged,
child: WillPopScope(
onWillPop: onWillPop,
child: child,
),
child: canPop != null || onPopInvoked != null
? ReactiveFormPopScope(
canPop: canPop,
onPopInvoked: onPopInvoked,
child: child,
)
: child,
);
}
}
26 changes: 11 additions & 15 deletions lib/src/widgets/reactive_form_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,15 @@ class ReactiveFormBuilder extends StatefulWidget {
/// Called to create the FormGroup that will be bind to this widget.
final ReactiveFormBuilderCreator form;

/// Determine whether a route can popped. See [PopScope] for more details.
final bool Function(FormGroup formGroup)? canPop;

/// A callback invoked when a route is popped. See [PopScope] for more details.
final void Function(FormGroup formGroup, bool didPop)? onPopInvoked;

/// The widget below this widget in the tree.
final Widget? child;

/// Enables the form to veto attempts by the user to dismiss the [ModalRoute]
/// that contains the form.
///
/// If the callback returns a Future that resolves to false, the form's route
/// will not be popped.
///
/// See also:
///
/// * [WillPopScope], another widget that provides a way to intercept the
/// back button.
final WillPopCallback? onWillPop;

/// Creates and instance of [ReactiveFormBuilder].
///
/// The [form] and [builder] arguments must not be null.
Expand All @@ -58,10 +52,11 @@ class ReactiveFormBuilder extends StatefulWidget {
/// ```
const ReactiveFormBuilder({
Key? key,
this.child,
this.onWillPop,
required this.form,
required this.builder,
this.canPop,
this.onPopInvoked,
this.child,
}) : super(key: key);

@override
Expand All @@ -81,7 +76,8 @@ class ReactiveFormBuilderState extends State<ReactiveFormBuilder> {
Widget build(BuildContext context) {
return ReactiveForm(
formGroup: _form,
onWillPop: widget.onWillPop,
canPop: widget.canPop,
onPopInvoked: widget.onPopInvoked,
child: widget.builder(context, _form, widget.child),
);
}
Expand Down
30 changes: 30 additions & 0 deletions lib/src/widgets/reactive_form_pop_scope.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:flutter/material.dart';
import 'package:reactive_forms/reactive_forms.dart';

class ReactiveFormPopScope extends StatelessWidget {
final bool Function(FormGroup formGroup)? canPop;
final void Function(FormGroup formGroup, bool didPop)? onPopInvoked;
final Widget child;

const ReactiveFormPopScope({
super.key,
this.canPop,
this.onPopInvoked,
required this.child,
});

@override
Widget build(BuildContext context) {
return ReactiveFormConsumer(
builder: (context, formGroup, _) {
return PopScope(
canPop: canPop != null ? canPop!(formGroup) : true,
onPopInvoked: onPopInvoked != null
? (didPop) => onPopInvoked!(formGroup, didPop)
: null,
child: child,
);
},
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: reactive_forms
description: This is a model-driven approach to handling form inputs and validations, heavily inspired in Angular Reactive Forms.
version: 16.1.1
version: 17.0.0
homepage: "https://github.com/joanpablo/reactive_forms"

environment:
Expand Down

0 comments on commit 9722066

Please sign in to comment.