responsive_dialog.dart
5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) 2018, codegrue. All rights reserved. Use of this source code
// is governed by the MIT license that can be found in the LICENSE file.
import '../flutter_material_pickers.dart';
import 'package:flutter/material.dart';
import '../interfaces/common_dialog_properties.dart';
// copied from flutter calendar picker
const Duration _dialogSizeAnimationDuration = Duration(milliseconds: 200);
/// This is a support widget that returns an Dialog with checkboxes as a Widget.
/// It is designed to be used in the showDialog method of other fields.
class ResponsiveDialog extends StatefulWidget
implements ICommonDialogProperties {
ResponsiveDialog({
this.context,
String title,
Widget child,
this.headerColor,
this.headerTextColor,
this.backgroundColor,
this.buttonTextColor,
this.forcePortrait = false,
double maxLongSide,
double maxShortSide,
this.hideButtons = false,
this.okPressed,
this.cancelPressed,
this.confirmText,
this.cancelText,
}) : title = title ?? "Title Here",
child = child ?? Text("Content Here"),
maxLongSide = maxLongSide ?? 600,
maxShortSide = maxShortSide ?? 400;
// Variables
final BuildContext context;
@override
final String title;
final Widget child;
final bool forcePortrait;
@override
final Color headerColor;
@override
final Color headerTextColor;
@override
final Color backgroundColor;
@override
final Color buttonTextColor;
@override
final double maxLongSide;
@override
final double maxShortSide;
final bool hideButtons;
@override
final String confirmText;
@override
final String cancelText;
// Events
final VoidCallback cancelPressed;
final VoidCallback okPressed;
@override
_ResponsiveDialogState createState() => _ResponsiveDialogState();
}
class _ResponsiveDialogState extends State<ResponsiveDialog> {
Color _headerColor;
Color _headerTextColor;
Color _backgroundColor;
Color _buttonTextColor;
Widget header(BuildContext context, Orientation orientation) {
return Container(
color: _headerColor,
height: (orientation == Orientation.portrait)
? kPickerHeaderPortraitHeight
: null,
width: (orientation == Orientation.landscape)
? kPickerHeaderLandscapeWidth
: null,
child: Center(
child: Text(
widget.title,
style: TextStyle(
color: _headerTextColor,
fontSize: 23,
fontFamily: 'Noto',
fontWeight: FontWeight.bold),
),
),
padding: EdgeInsets.all(20.0),
);
}
Widget actionBar(BuildContext context) {
if (widget.hideButtons) return Container();
var localizations = MaterialLocalizations.of(context);
return Container(
height: kDialogActionBarHeight,
child: Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(width: 1.0, color: _headerColor),
),
),
child: ButtonBar(
children: <Widget>[
FlatButton(
textColor: _buttonTextColor,
child: Text('취소'),
onPressed: () => (widget.cancelPressed == null)
? Navigator.of(context).pop()
: widget.cancelPressed(),
),
FlatButton(
textColor: _buttonTextColor,
child: Text('확인'),
onPressed: () => (widget.okPressed == null)
? Navigator.of(context).pop()
: widget.okPressed(),
),
],
),
),
);
}
@override
Widget build(BuildContext context) {
assert(context != null);
var theme = Theme.of(context);
_headerColor = widget.headerColor ?? theme.primaryColor;
_headerTextColor =
widget.headerTextColor ?? theme.primaryTextTheme.headline6.color;
_buttonTextColor = widget.buttonTextColor ?? theme.textTheme.button.color;
_backgroundColor = widget.backgroundColor ?? theme.dialogBackgroundColor;
final Orientation orientation = MediaQuery.of(context).orientation;
// constrain the dialog from expanding to full screen
final Size dialogSize = (orientation == Orientation.portrait)
? Size(widget.maxShortSide, widget.maxLongSide)
: Size(widget.maxLongSide, widget.maxShortSide);
return Dialog(
backgroundColor: _backgroundColor,
child: AnimatedContainer(
width: dialogSize.width,
height: dialogSize.height * 0.8,
duration: _dialogSizeAnimationDuration,
child: OrientationBuilder(
builder: (BuildContext context, Orientation orientation) {
assert(orientation != null);
assert(context != null);
if (widget.forcePortrait) orientation = Orientation.portrait;
switch (orientation) {
case Orientation.portrait:
return Column(
children: <Widget>[
header(context, orientation),
Expanded(
child: Container(
child: widget.child,
),
),
actionBar(context),
],
);
case Orientation.landscape:
return Row(
children: <Widget>[
header(context, orientation),
Expanded(
child: Column(
children: <Widget>[
Expanded(
child: widget.child,
),
actionBar(context),
],
),
),
],
);
}
return null;
},
),
),
);
}
}