Skip to content

Commit

Permalink
Update Flutter dependencies
Browse files Browse the repository at this point in the history
- Replace deprecated WillPopScope with PopScope
  • Loading branch information
joanpablo committed Mar 29, 2024
1 parent e8e8aaa commit f839d7d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.reactive_forms_example"
minSdkVersion 16
minSdkVersion flutter.minSdkVersion
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
19 changes: 8 additions & 11 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 @@ -19,10 +20,10 @@ class ReactiveForm extends StatelessWidget {
final FormGroup formGroup;

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

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

/// Creates and instance of [ReactiveForm].
///
Expand Down Expand Up @@ -62,15 +63,11 @@ class ReactiveForm extends StatelessWidget {
return FormControlInheritedStreamer(
control: formGroup,
stream: formGroup.statusChanged,
child: canPop != null || onPopInvoked != null
? PopScope(
canPop: canPop != null ? canPop!(formGroup) : true,
onPopInvoked: onPopInvoked != null
? (didPop) => onPopInvoked!(formGroup, didPop)
: null,
child: child,
)
: child,
child: ReactiveFormPopScope(
canPop: canPop,
onPopInvoked: onPopInvoked,
child: child,
),
);
}
}
19 changes: 18 additions & 1 deletion lib/src/widgets/reactive_form_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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.
/// Determine whether a route can be 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.
Expand Down Expand Up @@ -50,6 +50,23 @@ class ReactiveFormBuilder extends StatefulWidget {
/// }
/// }
/// ```
/// ### Example: Allows the route to be popped only if the form is valid.
/// ```dart
/// class MyWidget extends StatelessWidget {
/// @override
/// Widget build(BuildContext context) {
/// return ReactiveFormBuilder(
/// form: (context) => FormGroup({'name': FormControl<String>()}),
/// canPop: (formGroup) => formGroup.valid
/// builder: (context, form, child) {
/// return ReactiveTextField(
/// formControlName: 'name',
/// );
/// },
/// );
/// }
/// }
/// ```
const ReactiveFormBuilder({
super.key,
required this.form,
Expand Down
52 changes: 52 additions & 0 deletions lib/src/widgets/reactive_form_pop_scope.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2020 Joan Pablo Jimenez Milian. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
import 'package:reactive_forms/reactive_forms.dart';

/// This is the signature to determine whether a route can popped.
/// See [PopScope] for more details.
typedef ReactiveFormCanPopCallback = bool Function(FormGroup formGroup);

/// This is the signature of the callback invoked when a route is popped.
/// See [PopScope] for more details.
typedef ReactiveFormPopInvokedCallback = void Function(
FormGroup formGroup, bool didPop);

class ReactiveFormPopScope extends StatelessWidget {
/// The widget below this widget in the tree.
final Widget child;

/// Determine whether a route can popped. See [PopScope] for more details.
final ReactiveFormCanPopCallback? canPop;

/// A callback invoked when a route is popped. See [PopScope] for more details.
final ReactiveFormPopInvokedCallback? onPopInvoked;

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

@override
Widget build(BuildContext context) {
if (canPop == null && onPopInvoked == null) {
return child;
}

return ReactiveFormConsumer(
builder: (context, formGroup, _) {
return PopScope(
canPop: canPop?.call(formGroup) ?? true,
onPopInvoked: onPopInvoked != null
? (didPop) => onPopInvoked!(formGroup, didPop)
: null,
child: child,
);
},
);
}
}

0 comments on commit f839d7d

Please sign in to comment.