base_dialog.dart
3.29 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
import 'package:flutter/material.dart';
import 'package:Parlando/res/resources.dart';
import 'package:Parlando/routers/fluro_navigator.dart';
import 'package:Parlando/util/device_utils.dart';
import 'my_button.dart';
/// 自定义dialog的模板
class BaseDialog extends StatelessWidget {
const BaseDialog(
{Key? key,
this.title,
this.onPressed,
this.hiddenTitle = false,
required this.child
}) : super(key : key);
final String? title;
final VoidCallback? onPressed;
final Widget child;
final bool hiddenTitle;
@override
Widget build(BuildContext context) {
final Widget dialogTitle = Visibility(
visible: !hiddenTitle,
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Text(
hiddenTitle ? '' : title ?? '',
style: TextStyles.textBold18,
),
),
);
final Widget bottomButton = Row(
children: <Widget>[
_DialogButton(
text: '取消',
textColor: Colours.text_gray,
onPressed: () => NavigatorUtils.goBack(context),
),
const SizedBox(
height: 48.0,
width: 0.6,
child: VerticalDivider(),
),
_DialogButton(
text: '确定',
textColor: Theme.of(context).primaryColor,
onPressed: onPressed,
),
],
);
final Widget content = Material(
borderRadius: BorderRadius.circular(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Gaps.vGap24,
dialogTitle,
Flexible(child: child),
Gaps.vGap8,
Gaps.line,
bottomButton,
],
),
);
final Widget body = MediaQuery.removeViewInsets(
removeLeft: true,
removeTop: true,
removeRight: true,
removeBottom: true,
context: context,
child: Center(
child: SizedBox(
width: 270.0,
child: content,
),
),
);
/// Android 11添加了键盘弹出动画,这与我添加的过渡动画冲突(原先iOS、Android 没有相关过渡动画,相关问题跟踪:https://github.com/flutter/flutter/issues/19279)。
/// 因为在Android 11上,viewInsets的值在键盘弹出过程中是变化的(以前只有开始结束的值)。
/// 所以解决方法就是在Android 11及以上系统中使用Padding代替AnimatedPadding。
if (Device.getAndroidSdkInt() >= 30) {
return Padding(
padding: MediaQuery.of(context).viewInsets,
child: body,
);
} else {
return AnimatedPadding(
padding: MediaQuery.of(context).viewInsets,
duration: const Duration(milliseconds: 120),
curve: Curves.easeInCubic, // easeOutQuad
child: body,
);
}
}
}
class _DialogButton extends StatelessWidget {
const _DialogButton({
Key? key,
required this.text,
this.textColor,
this.onPressed,
}): super(key: key);
final String text;
final Color? textColor;
final VoidCallback? onPressed;
@override
Widget build(BuildContext context) {
return Expanded(
child: MyButton(
text: text,
textColor: textColor,
onPressed: onPressed,
backgroundColor: Colors.transparent,
),
);
}
}