|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +import 'mongol_button_bar.dart'; |
| 4 | + |
| 5 | +/// This class was adapted from Flutter [Dialog] |
| 6 | +class MongolDialog extends StatelessWidget { |
| 7 | + const MongolDialog({ |
| 8 | + Key key, |
| 9 | + this.backgroundColor, |
| 10 | + this.elevation, |
| 11 | + this.insetAnimationDuration = const Duration(milliseconds: 100), |
| 12 | + this.insetAnimationCurve = Curves.decelerate, |
| 13 | + this.shape, |
| 14 | + this.child, |
| 15 | + }) : super(key: key); |
| 16 | + |
| 17 | + final Color backgroundColor; |
| 18 | + final double elevation; |
| 19 | + final Duration insetAnimationDuration; |
| 20 | + final Curve insetAnimationCurve; |
| 21 | + final ShapeBorder shape; |
| 22 | + final Widget child; |
| 23 | + |
| 24 | + static const RoundedRectangleBorder _defaultDialogShape = |
| 25 | + RoundedRectangleBorder( |
| 26 | + borderRadius: BorderRadius.all(Radius.circular(2.0))); |
| 27 | + static const double _defaultElevation = 24.0; |
| 28 | + |
| 29 | + @override |
| 30 | + Widget build(BuildContext context) { |
| 31 | + final DialogTheme dialogTheme = DialogTheme.of(context); |
| 32 | + return AnimatedPadding( |
| 33 | + padding: MediaQuery.of(context).viewInsets + |
| 34 | + const EdgeInsets.symmetric(horizontal: 24.0, vertical: 40.0), |
| 35 | + duration: insetAnimationDuration, |
| 36 | + curve: insetAnimationCurve, |
| 37 | + child: MediaQuery.removeViewInsets( |
| 38 | + removeLeft: true, |
| 39 | + removeTop: true, |
| 40 | + removeRight: true, |
| 41 | + removeBottom: true, |
| 42 | + context: context, |
| 43 | + child: Center( |
| 44 | + child: ConstrainedBox( |
| 45 | + constraints: const BoxConstraints(minHeight: 280.0), |
| 46 | + child: Material( |
| 47 | + color: backgroundColor ?? |
| 48 | + dialogTheme.backgroundColor ?? |
| 49 | + Theme.of(context).dialogBackgroundColor, |
| 50 | + elevation: |
| 51 | + elevation ?? dialogTheme.elevation ?? _defaultElevation, |
| 52 | + shape: shape ?? dialogTheme.shape ?? _defaultDialogShape, |
| 53 | + type: MaterialType.card, |
| 54 | + child: child, |
| 55 | + ), |
| 56 | + ), |
| 57 | + ), |
| 58 | + ), |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// This class was adapted from the Flutter [AlertDialog] class |
| 64 | +class MongolAlertDialog extends StatelessWidget { |
| 65 | + const MongolAlertDialog({ |
| 66 | + Key key, |
| 67 | + this.title, |
| 68 | + this.titlePadding, |
| 69 | + this.titleTextStyle, |
| 70 | + this.content, |
| 71 | + this.contentPadding = const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0), |
| 72 | + this.contentTextStyle, |
| 73 | + this.actions, |
| 74 | + this.actionsPadding = EdgeInsets.zero, |
| 75 | + this.actionsOverflowDirection, |
| 76 | + this.buttonPadding, |
| 77 | + this.backgroundColor, |
| 78 | + this.elevation, |
| 79 | + this.shape, |
| 80 | + }) : assert(contentPadding != null), |
| 81 | + super(key: key); |
| 82 | + |
| 83 | + final Widget title; |
| 84 | + final EdgeInsetsGeometry titlePadding; |
| 85 | + final TextStyle titleTextStyle; |
| 86 | + final Widget content; |
| 87 | + final EdgeInsetsGeometry contentPadding; |
| 88 | + final TextStyle contentTextStyle; |
| 89 | + final List<Widget> actions; |
| 90 | + final EdgeInsetsGeometry actionsPadding; |
| 91 | + final VerticalDirection actionsOverflowDirection; |
| 92 | + final EdgeInsetsGeometry buttonPadding; |
| 93 | + final Color backgroundColor; |
| 94 | + final double elevation; |
| 95 | + final ShapeBorder shape; |
| 96 | + |
| 97 | + @override |
| 98 | + Widget build(BuildContext context) { |
| 99 | + final ThemeData theme = Theme.of(context); |
| 100 | + final DialogTheme dialogTheme = DialogTheme.of(context); |
| 101 | + |
| 102 | + Widget titleWidget; |
| 103 | + Widget contentWidget; |
| 104 | + Widget actionsWidget; |
| 105 | + if (title != null) |
| 106 | + titleWidget = Padding( |
| 107 | + padding: titlePadding ?? |
| 108 | + EdgeInsets.fromLTRB(24.0, 24.0, 24.0, content == null ? 20.0 : 0.0), |
| 109 | + child: DefaultTextStyle( |
| 110 | + style: titleTextStyle ?? |
| 111 | + dialogTheme.titleTextStyle ?? |
| 112 | + theme.textTheme.headline6, |
| 113 | + child: Semantics( |
| 114 | + child: title, |
| 115 | + namesRoute: true, |
| 116 | + container: true, |
| 117 | + ), |
| 118 | + ), |
| 119 | + ); |
| 120 | + |
| 121 | + if (content != null) |
| 122 | + contentWidget = Padding( |
| 123 | + padding: contentPadding, |
| 124 | + child: DefaultTextStyle( |
| 125 | + style: contentTextStyle ?? |
| 126 | + dialogTheme.contentTextStyle ?? |
| 127 | + theme.textTheme.subtitle1, |
| 128 | + child: content, |
| 129 | + ), |
| 130 | + ); |
| 131 | + |
| 132 | + if (actions != null) |
| 133 | + actionsWidget = Padding( |
| 134 | + padding: actionsPadding, |
| 135 | + child: MongolButtonBar( |
| 136 | + buttonPadding: buttonPadding, |
| 137 | + children: actions, |
| 138 | + ), |
| 139 | + ); |
| 140 | + |
| 141 | + List<Widget> rowChildren; |
| 142 | + |
| 143 | + rowChildren = <Widget>[ |
| 144 | + if (title != null || content != null) |
| 145 | + Flexible( |
| 146 | + child: Row( |
| 147 | + mainAxisSize: MainAxisSize.min, |
| 148 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 149 | + children: <Widget>[ |
| 150 | + if (title != null) titleWidget, |
| 151 | + if (content != null) contentWidget, |
| 152 | + ], |
| 153 | + ), |
| 154 | + ), |
| 155 | + if (actions != null) actionsWidget, |
| 156 | + ]; |
| 157 | + |
| 158 | + Widget dialogChild = IntrinsicHeight( |
| 159 | + child: Row( |
| 160 | + mainAxisSize: MainAxisSize.min, |
| 161 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 162 | + children: rowChildren, |
| 163 | + ), |
| 164 | + ); |
| 165 | + |
| 166 | + return MongolDialog( |
| 167 | + backgroundColor: backgroundColor, |
| 168 | + elevation: elevation, |
| 169 | + shape: shape, |
| 170 | + child: dialogChild, |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
0 commit comments