my_button.dart
2.63 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
import 'package:flutter/material.dart';
import 'package:one_poem/res/resources.dart';
import 'package:one_poem/util/theme_utils.dart';
/// 默认字号18,白字蓝底,高度48
class MyButton extends StatelessWidget {
const MyButton({
Key? key,
this.text = '',
this.fontSize = Dimens.font_sp18,
this.textColor,
this.disabledTextColor,
this.backgroundColor,
this.disabledBackgroundColor,
this.minHeight = 48.0,
this.minWidth = double.infinity,
this.padding = const EdgeInsets.symmetric(horizontal: 16.0),
this.radius = 2.0,
this.side = BorderSide.none,
required this.onPressed,
}): super(key: key);
final String text;
final double fontSize;
final Color? textColor;
final Color? disabledTextColor;
final Color? backgroundColor;
final Color? disabledBackgroundColor;
final double? minHeight;
final double? minWidth;
final VoidCallback? onPressed;
final EdgeInsetsGeometry padding;
final double radius;
final BorderSide side;
@override
Widget build(BuildContext context) {
final bool isDark = context.isDark;
return TextButton(
onPressed: onPressed,
style: ButtonStyle(
// 文字颜色
foregroundColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.disabled)) {
return disabledTextColor ?? (isDark ? Colours.dark_text_disabled : Colours.text_disabled);
}
return textColor ?? (isDark ? Colours.dark_button_text : Colors.white);
},
),
// 背景颜色
backgroundColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.disabled)) {
return disabledBackgroundColor ?? (isDark ? Colours.dark_button_disabled : Colours.button_disabled);
}
return backgroundColor ?? (isDark ? Colours.dark_app_main : Colours.app_main);
}),
// 水波纹
overlayColor: MaterialStateProperty.resolveWith((states) {
return (textColor ?? (isDark ? Colours.dark_button_text : Colors.white)).withOpacity(0.12);
}),
// 按钮最小大小
minimumSize: (minWidth == null || minHeight == null) ? null : MaterialStateProperty.all<Size>(Size(minWidth!, minHeight!)),
padding: MaterialStateProperty.all<EdgeInsetsGeometry>(padding),
shape: MaterialStateProperty.all<OutlinedBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(radius),
),
),
side: MaterialStateProperty.all<BorderSide>(side),
),
child: Text(text, style: TextStyle(fontSize: fontSize),)
);
}
}