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