near_pay_app/lib/presantation/ui/widgets/app_simpledialog.dart
2024-05-08 16:06:58 +03:00

628 lines
22 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// ignore_for_file: unnecessary_null_comparison
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:near_pay_app/appstate_container.dart';
// Examples can assume:
// enum Department { treasury, state }
// BuildContext context;
/// A material design dialog.
///
/// This dialog widget does not have any opinion about the contents of the
/// dialog. Rather than using this widget directly, consider using [AppAlertDialog]
/// or [AppSimpleDialog], which implement specific kinds of material design
/// dialogs.
///
/// See also:
///
/// * [AppAlertDialog], for dialogs that have a message and some buttons.
/// * [AppSimpleDialog], for dialogs that offer a variety of options.
/// * [showAppDialog], which actually displays the dialog and returns its result.
/// * <https://material.google.com/components/dialogs.html>
class Dialog extends StatelessWidget {
/// Creates a dialog.
///
/// Typically used in conjunction with [showAppDialog].
const Dialog({
required Key key,
required this.child,
this.insetAnimationDuration = const Duration(milliseconds: 100),
this.insetAnimationCurve = Curves.decelerate,
required this.shape,
}) : super(key: key);
/// The widget below this widget in the tree.
///
/// {@macro flutter.widgets.child}
final Widget child;
/// The duration of the animation to show when the system keyboard intrudes
/// into the space that the dialog is placed in.
///
/// Defaults to 100 milliseconds.
final Duration insetAnimationDuration;
/// The curve to use for the animation shown when the system keyboard intrudes
/// into the space that the dialog is placed in.
///
/// Defaults to [Curves.fastOutSlowIn].
final Curve insetAnimationCurve;
/// {@template flutter.material.dialog.shape}
/// The shape of this dialog's border.
///
/// Defines the dialog's [Material.shape].
///
/// The default shape is a [RoundedRectangleBorder] with a radius of 2.0.
/// {@endtemplate}
final ShapeBorder shape;
Color _getColor(BuildContext context) {
return Theme.of(context).dialogBackgroundColor;
}
double _getHeight(BuildContext context) {
return MediaQuery.of(context).size.height;
}
@override
Widget build(BuildContext context) {
DialogTheme.of(context);
return AnimatedPadding(
padding: MediaQuery.of(context).viewInsets +
const EdgeInsets.symmetric(horizontal: 20.0, vertical: 20.0),
duration: insetAnimationDuration,
curve: insetAnimationCurve,
child: MediaQuery.removeViewInsets(
removeLeft: true,
removeTop: true,
removeRight: true,
removeBottom: true,
context: context,
child: Center(
child: ConstrainedBox(
constraints: BoxConstraints(
minWidth: 280.0,
maxHeight: _getHeight(context) < 667
? _getHeight(context) * 0.75
: _getHeight(context) * 0.65),
child: Material(
elevation: 24.0,
color: _getColor(context),
type: MaterialType.card,
shape: shape,
child: child,
),
),
),
),
);
}
}
/// A material design alert dialog.
///
/// An alert dialog informs the user about situations that require
/// acknowledgement. An alert dialog has an optional title and an optional list
/// of actions. The title is displayed above the content and the actions are
/// displayed below the content.
///
/// If the content is too large to fit on the screen vertically, the dialog will
/// display the title and the actions and let the content overflow, which is
/// rarely desired. Consider using a scrolling widget for [content], such as
/// [SingleChildScrollView], to avoid overflow. (However, be aware that since
/// [AppAlertDialog] tries to size itself using the intrinsic dimensions of its
/// children, widgets such as [ListView], [GridView], and [CustomScrollView],
/// which use lazy viewports, will not work. If this is a problem, consider
/// using [Dialog] directly.)
///
/// For dialogs that offer the user a choice between several options, consider
/// using a [AppSimpleDialog].
///
/// Typically passed as the child widget to [showAppDialog], which displays the
/// dialog.
///
/// {@tool sample}
///
/// This snippet shows a method in a [State] which, when called, displays a dialog box
/// and returns a [Future] that completes when the dialog is dismissed.
///
/// ```dart
/// Future<void> _neverSatisfied() async {
/// return showAppDialog<void>(
/// context: context,
/// barrierDismissible: false, // user must tap button!
/// builder: (BuildContext context) {
/// return AppAlertDialog(
/// title: Text('Rewind and remember'),
/// content: SingleChildScrollView(
/// child: ListBody(
/// children: <Widget>[
/// Text('You will never be satisfied.'),
/// Text('You\’re like me. I’m never satisfied.'),
/// ],
/// ),
/// ),
/// actions: <Widget>[
/// FlatButton(
/// child: Text('Regret'),
/// onPressed: () {
/// Navigator.of(context).pop();
/// },
/// ),
/// ],
/// );
/// },
/// );
/// }
/// ```
/// {@end-tool}
///
/// See also:
///
/// * [AppSimpleDialog], which handles the scrolling of the contents but has no [actions].
/// * [Dialog], on which [AppAlertDialog] and [AppSimpleDialog] are based.
/// * [showAppDialog], which actually displays the dialog and returns its result.
/// * <https://material.google.com/components/dialogs.html#dialogs-alerts>
class AppAlertDialog extends StatelessWidget {
/// Creates an alert dialog.
///
/// Typically used in conjunction with [showAppDialog].
///
/// The [contentPadding] must not be null. The [titlePadding] defaults to
/// null, which implies a default that depends on the values of the other
/// properties. See the documentation of [titlePadding] for details.
const AppAlertDialog({
required Key key,
required this.title,
required this.titlePadding,
required this.content,
this.contentPadding =
const EdgeInsetsDirectional.fromSTEB(24.0, 20.0, 24.0, 24.0),
required this.actions,
required this.semanticLabel,
required this.shape,
}) : super(key: key);
/// The (optional) title of the dialog is displayed in a large font at the top
/// of the dialog.
///
/// Typically a [Text] widget.
final Widget title;
/// Padding around the title.
///
/// If there is no title, no padding will be provided. Otherwise, this padding
/// is used.
///
/// This property defaults to providing 24 pixels on the top, left, and right
/// of the title. If the [content] is not null, then no bottom padding is
/// provided (but see [contentPadding]). If it _is_ null, then an extra 20
/// pixels of bottom padding is added to separate the [title] from the
/// [actions].
final EdgeInsetsGeometry titlePadding;
/// The (optional) content of the dialog is displayed in the center of the
/// dialog in a lighter font.
///
/// Typically, this is a [ListView] containing the contents of the dialog.
/// Using a [ListView] ensures that the contents can scroll if they are too
/// big to fit on the display.
final Widget content;
/// Padding around the content.
///
/// If there is no content, no padding will be provided. Otherwise, padding of
/// 20 pixels is provided above the content to separate the content from the
/// title, and padding of 24 pixels is provided on the left, right, and bottom
/// to separate the content from the other edges of the dialog.
final EdgeInsetsGeometry contentPadding;
/// The (optional) set of actions that are displayed at the bottom of the
/// dialog.
///
/// Typically this is a list of [FlatButton] widgets.
///
/// These widgets will be wrapped in a [ButtonBar], which introduces 8 pixels
/// of padding on each side.
///
/// If the [title] is not null but the [content] _is_ null, then an extra 20
/// pixels of padding is added above the [ButtonBar] to separate the [title]
/// from the [actions].
final List<Widget> actions;
/// The semantic label of the dialog used by accessibility frameworks to
/// announce screen transitions when the dialog is opened and closed.
///
/// If this label is not provided, a semantic label will be infered from the
/// [title] if it is not null. If there is no title, the label will be taken
/// from [MaterialLocalizations.AppAlertDialogLabel].
///
/// See also:
///
/// * [SemanticsConfiguration.isRouteName], for a description of how this
/// value is used.
final String semanticLabel;
/// {@macro flutter.material.dialog.shape}
final ShapeBorder shape;
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterialLocalizations(context));
final List<Widget> children = <Widget>[];
String label = semanticLabel;
children.add(Padding(
padding: titlePadding,
child: DefaultTextStyle(
style: Theme.of(context).textTheme.displayLarge,
child: Semantics(namesRoute: true, child: title),
),
));
children.add(Flexible(
child: Padding(
padding: contentPadding,
child: DefaultTextStyle(
style: Theme.of(context).textTheme.titleMedium,
child: content,
),
),
));
children.add(ButtonBarTheme(
data: ButtonBarTheme.of(context),
child: ButtonBar(
children: actions,
)));
Widget dialogChild = IntrinsicWidth(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: children,
),
);
dialogChild =
Semantics(namesRoute: true, label: label, child: dialogChild);
return Dialog(shape: shape, key: null,
child: dialogChild);
}
}
/// An option used in a [AppSimpleDialog].
///
/// A simple dialog offers the user a choice between several options. This
/// widget is commonly used to represent each of the options. If the user
/// selects this option, the widget will call the [onPressed] callback, which
/// typically uses [Navigator.pop] to close the dialog.
///
/// The padding on a [AppSimpleDialogOption] is configured to combine with the
/// default [AppSimpleDialog.contentPadding] so that each option ends up 8 pixels
/// from the other vertically, with 20 pixels of spacing between the dialog's
/// title and the first option, and 24 pixels of spacing between the last option
/// and the bottom of the dialog.
///
/// {@tool sample}
///
/// ```dart
/// AppSimpleDialogOption(
/// onPressed: () { Navigator.pop(context, Department.treasury); },
/// child: const Text('Treasury department'),
/// )
/// ```
/// {@end-tool}
///
/// See also:
///
/// * [AppSimpleDialog], for a dialog in which to use this widget.
/// * [showAppDialog], which actually displays the dialog and returns its result.
/// * [FlatButton], which are commonly used as actions in other kinds of
/// dialogs, such as [AppAlertDialog]s.
/// * <https://material.google.com/components/dialogs.html#dialogs-simple-dialogs>
class AppSimpleDialogOption extends StatelessWidget {
/// Creates an option for a [AppSimpleDialog].
const AppSimpleDialogOption({
required Key key,
required this.onPressed,
required this.child,
}) : super(key: key);
/// The callback that is called when this option is selected.
///
/// If this is set to null, the option cannot be selected.
///
/// When used in a [AppSimpleDialog], this will typically call [Navigator.pop]
/// with a value for [showAppDialog] to complete its future with.
final VoidCallback onPressed;
/// The widget below this widget in the tree.
///
/// Typically a [Text] widget.
final Widget child;
@override
Widget build(BuildContext context) {
return InkWell(
onTap: onPressed,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 24.0),
child: child),
);
}
}
/// A simple material design dialog.
///
/// A simple dialog offers the user a choice between several options. A simple
/// dialog has an optional title that is displayed above the choices.
///
/// Choices are normally represented using [AppSimpleDialogOption] widgets. If
/// other widgets are used, see [contentPadding] for notes regarding the
/// conventions for obtaining the spacing expected by Material Design.
///
/// For dialogs that inform the user about a situation, consider using an
/// [AppAlertDialog].
///
/// Typically passed as the child widget to [showAppDialog], which displays the
/// dialog.
///
/// {@tool sample}
///
/// In this example, the user is asked to select between two options. These
/// options are represented as an enum. The [showAppDialog] method here returns
/// a [Future] that completes to a value of that enum. If the user cancels
/// the dialog (e.g. by hitting the back button on Android, or tapping on the
/// mask behind the dialog) then the future completes with the null value.
///
/// The return value in this example is used as the index for a switch statement.
/// One advantage of using an enum as the return value and then using that to
/// drive a switch statement is that the analyzer will flag any switch statement
/// that doesn't mention every value in the enum.
///
/// ```dart
/// Future<void> _askedToLead() async {
/// switch (await showAppDialog<Department>(
/// context: context,
/// builder: (BuildContext context) {
/// return AppSimpleDialog(
/// title: const Text('Select assignment'),
/// children: <Widget>[
/// AppSimpleDialogOption(
/// onPressed: () { Navigator.pop(context, Department.treasury); },
/// child: const Text('Treasury department'),
/// ),
/// AppSimpleDialogOption(
/// onPressed: () { Navigator.pop(context, Department.state); },
/// child: const Text('State department'),
/// ),
/// ],
/// );
/// }
/// )) {
/// case Department.treasury:
/// // Let's go.
/// // ...
/// break;
/// case Department.state:
/// // ...
/// break;
/// }
/// }
/// ```
/// {@end-tool}
///
/// See also:
///
/// * [AppSimpleDialogOption], which are options used in this type of dialog.
/// * [AppAlertDialog], for dialogs that have a row of buttons below the body.
/// * [Dialog], on which [AppSimpleDialog] and [AppAlertDialog] are based.
/// * [showAppDialog], which actually displays the dialog and returns its result.
/// * <https://material.google.com/components/dialogs.html#dialogs-simple-dialogs>
class AppSimpleDialog extends StatelessWidget {
/// Creates a simple dialog.
///
/// Typically used in conjunction with [showAppDialog].
///
/// The [titlePadding] and [contentPadding] arguments must not be null.
const AppSimpleDialog({
required Key key,
required this.title,
this.titlePadding =
const EdgeInsetsDirectional.fromSTEB(24.0, 24.0, 24.0, 0.0),
required this.children,
this.contentPadding =
const EdgeInsetsDirectional.fromSTEB(0.0, 12.0, 0.0, 16.0),
required this.semanticLabel,
required this.shape,
}) : assert(contentPadding != null),
super(key: key);
/// The (optional) title of the dialog is displayed in a large font at the top
/// of the dialog.
///
/// Typically a [Text] widget.
final Widget title;
/// Padding around the title.
///
/// If there is no title, no padding will be provided.
///
/// By default, this provides the recommend Material Design padding of 24
/// pixels around the left, top, and right edges of the title.
///
/// See [contentPadding] for the conventions regarding padding between the
/// [title] and the [children].
final EdgeInsetsGeometry titlePadding;
/// The (optional) content of the dialog is displayed in a
/// [SingleChildScrollView] underneath the title.
///
/// Typically a list of [AppSimpleDialogOption]s.
final List<Widget> children;
/// Padding around the content.
///
/// By default, this is 12 pixels on the top and 16 pixels on the bottom. This
/// is intended to be combined with children that have 24 pixels of padding on
/// the left and right, and 8 pixels of padding on the top and bottom, so that
/// the content ends up being indented 20 pixels from the title, 24 pixels
/// from the bottom, and 24 pixels from the sides.
///
/// The [AppSimpleDialogOption] widget uses such padding.
///
/// If there is no [title], the [contentPadding] should be adjusted so that
/// the top padding ends up being 24 pixels.
final EdgeInsetsGeometry contentPadding;
/// The semantic label of the dialog used by accessibility frameworks to
/// announce screen transitions when the dialog is opened and closed.
///
/// If this label is not provided, a semantic label will be infered from the
/// [title] if it is not null. If there is no title, the label will be taken
/// from [MaterialLocalizations.dialogLabel].
///
/// See also:
///
/// * [SemanticsConfiguration.isRouteName], for a description of how this
/// value is used.
final String semanticLabel;
/// {@macro flutter.material.dialog.shape}
final ShapeBorder shape;
@override
Widget build(BuildContext context) {
assert(debugCheckHasMaterialLocalizations(context));
final List<Widget> body = <Widget>[];
String label = semanticLabel;
body.add(Padding(
padding: titlePadding,
child: DefaultTextStyle(
style: Theme.of(context).textTheme.displayLarge,
child: Semantics(namesRoute: true, child: title),
)));
body.add(Flexible(
child: SingleChildScrollView(
padding: contentPadding,
child: ListBody(children: children),
)));
Widget dialogChild = IntrinsicWidth(
stepWidth: 56.0,
child: ConstrainedBox(
constraints: const BoxConstraints(minWidth: 280.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: body,
),
),
);
dialogChild = Semantics(
namesRoute: true,
label: label,
child: dialogChild,
);
return Dialog(shape: shape, key: null,
child: dialogChild);
}
}
Widget _buildMaterialDialogTransitions(
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
return FadeTransition(
opacity: CurvedAnimation(
parent: animation,
curve: Curves.easeOut,
),
child: child,
);
}
/// Displays a Material dialog above the current contents of the app, with
/// Material entrance and exit animations, modal barrier color, and modal
/// barrier behavior (dialog is dismissible with a tap on the barrier).
///
/// This function takes a `builder` which typically builds a [Dialog] widget.
/// Content below the dialog is dimmed with a [ModalBarrier]. The widget
/// returned by the `builder` does not share a context with the location that
/// `showAppDialog` is originally called from. Use a [StatefulBuilder] or a
/// custom [StatefulWidget] if the dialog needs to update dynamically.
///
/// The `context` argument is used to look up the [Navigator] and [Theme] for
/// the dialog. It is only used when the method is called. Its corresponding
/// widget can be safely removed from the tree before the dialog is closed.
///
/// The `child` argument is deprecated, and should be replaced with `builder`.
///
/// Returns a [Future] that resolves to the value (if any) that was passed to
/// [Navigator.pop] when the dialog was closed.
///
/// The dialog route created by this method is pushed to the root navigator.
/// If the application has multiple [Navigator] objects, it may be necessary to
/// call `Navigator.of(context, rootNavigator: true).pop(result)` to close the
/// dialog rather than just `Navigator.pop(context, result)`.
///
/// See also:
/// * [AppAlertDialog], for dialogs that have a row of buttons below a body.
/// * [AppSimpleDialog], which handles the scrolling of the contents and does
/// not show buttons below its body.
/// * [Dialog], on which [AppSimpleDialog] and [AppAlertDialog] are based.
/// * [showCupertinoDialog], which displays an iOS-style dialog.
/// * [showGeneralDialog], which allows for customization of the dialog popup.
/// * <https://material.google.com/components/dialogs.html>
Future<Object?> showAppDialog<T>({
required BuildContext context,
bool barrierDismissible = true,
@Deprecated(
'Instead of using the "child" argument, return the child from a closure '
'provided to the "builder" argument. This will ensure that the BuildContext '
'is appropriate for widgets built in the dialog.')
required Widget child,
required WidgetBuilder builder,
}) {
assert(builder == null);
assert(debugCheckHasMaterialLocalizations(context));
return showGeneralDialog(
context: context,
pageBuilder: (BuildContext buildContext, Animation<double> animation,
Animation<double> secondaryAnimation) {
final ThemeData theme = Theme.of(context);
final Widget pageChild = child;
return SafeArea(
child: Builder(builder: (BuildContext context) {
return theme != null
? Theme(data: theme, child: pageChild)
: pageChild;
}),
);
},
barrierDismissible: barrierDismissible,
barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
barrierColor: StateContainer.of(context)!.curTheme.barrier,
transitionDuration: const Duration(milliseconds: 150),
transitionBuilder: _buildMaterialDialogTransitions,
);
}