|
1 | 1 | import 'package:flutter/material.dart'; |
2 | | -import 'package:smart_color/smart_color.dart'; |
| 2 | +import 'package:flutter_weui/flutter_weui.dart'; |
| 3 | +import 'package:flutter_weui/src/base/button_theme.dart'; |
3 | 4 | import 'package:flutter/cupertino.dart'; |
4 | 5 |
|
5 | 6 | ///按钮的三种类型 |
| 7 | + |
6 | 8 | enum ButtonType { |
7 | | - btnPrimary, |
8 | | - btnWarn, |
9 | | - btnDefault, |
| 9 | + primay, |
| 10 | + warn, |
| 11 | + default_, |
10 | 12 | } |
11 | 13 |
|
12 | 14 | class Button extends StatefulWidget { |
13 | | - Button({Key key, this.type = ButtonType.btnDefault, this.disabled, this.onPressed, this.loading, this.text = "button"}) : super(key: key); |
14 | | - |
15 | | - /// 按钮类型 |
16 | | - final ButtonType type; //按钮类型 |
17 | | - final bool disabled; //是否可用 |
18 | | - final VoidCallback onPressed; //按下时进行触发 |
19 | | - final bool loading; //是否加载中 |
20 | | - final String text; //按钮上的文字 |
| 15 | + Button({ |
| 16 | + Key key, |
| 17 | + this.disabled = false, |
| 18 | + this.loading = false, |
| 19 | + this.text, |
| 20 | + this.type = ButtonType.default_, |
| 21 | + this.onPress, |
| 22 | + }) : super(key: key); |
| 23 | + final bool disabled; |
| 24 | + final String text; |
| 25 | + final ButtonType type; |
| 26 | + final bool loading; |
| 27 | + final VoidCallback onPress; |
21 | 28 | @override |
22 | | - State<StatefulWidget> createState() => _Button(); |
| 29 | + _ButtonState createState() => _ButtonState(); |
23 | 30 | } |
24 | 31 |
|
25 | | -class _ButtonColor { |
| 32 | +class _ButtonStateextendsState<Button> { |
26 | 33 | Color fontColor; |
27 | | - Color activeFontColor; |
28 | | - Color disabledFontColor; |
29 | | - |
30 | | - Color bg; |
31 | | - Color activeBg; |
32 | | - Color disabledBg; |
33 | | -} |
34 | | - |
35 | | -///默认的按钮颜色 |
36 | | -class _ButtonDefault extends _ButtonColor { |
37 | | - Color fontColor = Color(0xFF000000); |
38 | | - Color activeFontColor = Color.fromARGB((0.6 * 255).toInt(), 0, 0, 0); |
39 | | - Color disabledFontColor = Color.fromARGB((0.3 * 255).toInt(), 0, 0, 0); |
40 | | - |
41 | | - Color bg = Color(0xFFF8F8F8); |
42 | | - Color activeBg = Color(0xFFDEDEDE); |
43 | | - Color disabledBg = Color(0xFFF7F7F7); |
44 | | -} |
45 | | - |
46 | | -///警告按钮颜色 |
47 | | -class _ButtonWarn extends _ButtonColor { |
48 | | - Color fontColor = Color(0xFFFFFFFF); //按钮文本颜色 |
49 | | - Color activeFontColor = Color.fromARGB((0.6 * 255).toInt(), 255, 255, 255); //按下时文本颜色 |
50 | | - Color disabledFontColor = Color.fromARGB((0.6 * 255).toInt(), 255, 255, 255); //不可用时文本颜色 |
| 34 | + // 初始时的背景颜色 |
| 35 | + Color bgInitColor; |
| 36 | + // 按下时的背景颜色 |
| 37 | + Color bgActiveColor; |
| 38 | + // 释放之后文本颜色 |
| 39 | + Color fontVisitedColor; |
51 | 40 |
|
52 | | - Color bg = Color(0xFFE64340); |
53 | | - Color activeBg = Color(0xFFCE3C39); |
54 | | - Color disabledBg = Color(0xFFEC8B89); |
55 | | -} |
56 | | - |
57 | | -///按钮主颜色 |
58 | | -class _ButtonPrimary extends _ButtonColor { |
59 | | - Color fontColor = Color(0xFFFFFFFF); //按钮文本颜色 |
60 | | - Color activeFontColor = Color.fromARGB((0.6 * 255).toInt(), 255, 255, 255); //按下时文本颜色 |
61 | | - Color disabledFontColor = Color.fromARGB((0.6 * 255).toInt(), 255, 255, 255); //不可用时文本颜色 |
62 | | - |
63 | | - Color bg = Color(0xFF1AAD19); |
64 | | - Color activeBg = Color(0xFF179B16); |
65 | | - Color disabledBg = SmartColor.parse("#9ED99D"); |
66 | | -} |
| 41 | + Color bgColor; |
67 | 42 |
|
68 | | -class _Button extends State<Button> { |
69 | | - double wuiBtnHeight = 46; //按钮高度 |
70 | | - double wuiBtnFontSize = 18; //按钮文本尺寸 |
71 | | - double wuiBtnBorderRadius = 5; //按钮borderRadius |
72 | | - Color wuiBtnBorderColor = SmartColor.parse("rgba(0,0,0,.2)"); //按钮border颜色 |
73 | | - double wuiBtnDefaultGap = 15; |
| 43 | + WeUIThemeData get theme => WeUITheme.of(context); |
74 | 44 |
|
75 | | - double wuiBtnMiniFontSize = 13; |
76 | | - double wuiBtnMiniHeight = 2.3; |
77 | | - |
78 | | - Color wuiBtnPlainPrimaryColor = SmartColor.parse("rgba(26,173,25,1)"); |
79 | | - Color wuiBtnPlainPrimaryBorderColor = SmartColor.parse("rgba(26,173,25,1)"); |
80 | | - Color wuiBtnPlainPrimaryActiveColor = SmartColor.parse("rgba(26,173,25,.6)"); |
81 | | - Color wuiBtnPlainPrimaryActiveBorderColor = SmartColor.parse("rgba(26,173,25,.6)"); |
82 | | - |
83 | | - Color wuiBtnPlainDefaultColor = SmartColor.parse("rgba(53,53,53,1)"); |
84 | | - Color wuiBtnPlainDefaultBorderColor = SmartColor.parse("rgba(53,53,53,1)"); |
85 | | - Color wuiBtnPlainDefaultActiveColor = SmartColor.parse("rgba(53,53,53,.6)"); |
86 | | - Color wuiBtnPlainDefaultActiveBorderColor = SmartColor.parse("rgba(53,53,53,.6)"); |
87 | | - |
88 | | - Color fontColor; |
89 | | - Color btnColor; |
90 | | - |
91 | | - _ButtonColor buttonColor; |
| 45 | + WeUIButtonThemeData get buttonTheme => WeUIButtonTheme.of(context) ?? theme.buttonTheme; |
92 | 46 |
|
93 | 47 | @override |
94 | 48 | void initState() { |
95 | 49 | super.initState(); |
| 50 | + } |
96 | 51 |
|
97 | | - ///按钮颜色初始化 |
98 | | - buttonColor = _getButtonColor(widget.type); |
99 | | - if (widget.disabled == true) { |
100 | | - fontColor = buttonColor.disabledFontColor; |
101 | | - btnColor = buttonColor.disabledBg; |
102 | | - } else { |
103 | | - fontColor = buttonColor.fontColor; |
104 | | - btnColor = buttonColor.bg; |
| 52 | + // 初始化颜色 |
| 53 | + initColor() { |
| 54 | + if (widget.disabled) { |
| 55 | + fontColor = buttonTheme.btnDisabledFontColor; |
| 56 | + bgColor = buttonTheme.btnDisabledBg; |
| 57 | + bgInitColor = bgColor; |
| 58 | + return; |
| 59 | + } |
| 60 | + switch (widget.type) { |
| 61 | + case ButtonType.primay: |
| 62 | + fontColor = buttonTheme.btnFontColor; |
| 63 | + bgColor = buttonTheme.btnPrimaryBg; |
| 64 | + bgActiveColor = buttonTheme.btnPrimaryActiveBg; |
| 65 | + fontVisitedColor = buttonTheme.btnPrimaryFontColor; |
| 66 | + if (widget.loading) { |
| 67 | + bgColor = bgActiveColor; |
| 68 | + fontColor = Colors.white; |
| 69 | + } |
| 70 | + bgInitColor = bgColor; |
| 71 | + break; |
| 72 | + case ButtonType.warn: |
| 73 | + fontColor = buttonTheme.btnWarnFontColor; |
| 74 | + bgColor = buttonTheme.btnWarnBg; |
| 75 | + bgActiveColor = buttonTheme.btnWarnActiveBg; |
| 76 | + fontVisitedColor = buttonTheme.btnWarnFontColor; |
| 77 | + if (widget.loading) bgColor = bgActiveColor; |
| 78 | + bgInitColor = bgColor; |
| 79 | + break; |
| 80 | + default: |
| 81 | + fontColor = buttonTheme.btnDefaultFontColor; |
| 82 | + bgColor = buttonTheme.btnDefaultBg; |
| 83 | + bgActiveColor = buttonTheme.btnDefaultActiveBg; |
| 84 | + fontVisitedColor = buttonTheme.btnDefaultFontColor; |
| 85 | + if (widget.loading) bgColor = bgActiveColor; |
| 86 | + bgInitColor = bgColor; |
| 87 | + break; |
105 | 88 | } |
| 89 | + markNeedBuild(); |
106 | 90 | } |
107 | 91 |
|
108 | | - ///获取按钮颜色 |
109 | | - _ButtonColor _getButtonColor(ButtonType type) { |
110 | | - if (type == ButtonType.btnPrimary) return _ButtonPrimary(); |
111 | | - if (type == ButtonType.btnWarn) return _ButtonWarn(); |
112 | | - return _ButtonDefault(); |
| 92 | + onTapDown(_) { |
| 93 | + if (widget.disabled || widget.loading) return; |
| 94 | + bgColor = bgActiveColor; |
| 95 | + markNeedBuild(); |
113 | 96 | } |
114 | 97 |
|
115 | | - _onTapDown(e) { |
116 | | - if (widget.disabled ==true|| widget.loading==true) return; |
117 | | - setState(() { |
118 | | - fontColor = buttonColor.activeFontColor; |
119 | | - btnColor = buttonColor.activeBg; |
120 | | - }); |
| 98 | + onTapUp(_) { |
| 99 | + if (widget.disabled || widget.loading) return; |
| 100 | + if (widget.onPress !=null) widget.onPress(); |
| 101 | + bgColor = bgInitColor; |
| 102 | + fontColor = fontVisitedColor; |
| 103 | + markNeedBuild(); |
121 | 104 | } |
122 | 105 |
|
123 | | - _onTapUp(e) { |
124 | | - setState(() { |
125 | | - fontColor = buttonColor.fontColor; |
126 | | - btnColor = buttonColor.bg; |
127 | | - }); |
128 | | - if (widget.onPressed != null) widget.onPressed(); |
| 106 | + onTapCancel() { |
| 107 | + if (widget.disabled || widget.loading) return; |
| 108 | + bgColor = bgInitColor; |
| 109 | + markNeedBuild(); |
129 | 110 | } |
130 | 111 |
|
| 112 | + markNeedBuild() => setState(() {}); |
| 113 | + |
131 | 114 | @override |
132 | 115 | Widget build(BuildContext context) { |
| 116 | + final double btnWidth = buttonTheme.btnWidth; |
| 117 | + final double fontSize = buttonTheme.btnFontSize; |
| 118 | + final double radius = buttonTheme.btnBorderRadius; |
| 119 | + final double btnHeight = buttonTheme.btnHeight; |
| 120 | + final double lineHeight = (btnHeight - 16) / fontSize; |
133 | 121 | return GestureDetector( |
134 | | - onTapDown: _onTapDown, |
135 | | - onTapUp: _onTapUp, |
| 122 | + onTapDown: onTapDown, |
| 123 | + onTapUp: onTapUp, |
| 124 | + onTapCancel: onTapCancel, |
| 125 | + behavior: HitTestBehavior.opaque, |
136 | 126 | child: Container( |
137 | | - height: wuiBtnHeight, |
| 127 | + width: btnWidth, |
| 128 | + decoration: BoxDecoration(borderRadius: BorderRadius.circular(radius), color: bgColor), |
138 | 129 | child: Row( |
139 | 130 | mainAxisAlignment: MainAxisAlignment.center, |
140 | | - crossAxisAlignment: CrossAxisAlignment.center, |
141 | 131 | children: <Widget>[ |
142 | | - widget.loading == true |
| 132 | + widget.loading == true&&!widget.disabled |
143 | 133 | ? Padding( |
144 | | - padding: constEdgeInsets.only(right: 10), |
145 | | - child: CupertinoActivityIndicator(), |
| 134 | + padding: EdgeInsets.only(right: 0.34* fontSize), |
| 135 | + child: CupertinoActivityIndicator(radius:9), |
146 | 136 | ) |
147 | | - : Text(""), |
148 | | - Text( |
149 | | - widget.text, |
150 | | - style: TextStyle( |
151 | | - color: fontColor, |
152 | | - fontSize: wuiBtnFontSize, |
| 137 | + : SizedBox(), |
| 138 | + Padding( |
| 139 | + padding: EdgeInsets.symmetric(vertical: lineHeight / 2.0), |
| 140 | + child: Text( |
| 141 | + widget.text ?? '按钮', |
| 142 | + style: TextStyle( |
| 143 | + fontSize: fontSize, |
| 144 | + color: fontColor, |
| 145 | + ), |
153 | 146 | ), |
154 | | - ) |
| 147 | + ), |
155 | 148 | ], |
156 | 149 | ), |
157 | | - decoration: BoxDecoration( |
158 | | - borderRadius: BorderRadius.all(Radius.circular( |
159 | | - wuiBtnBorderRadius, |
160 | | - )), |
161 | | - border: Border.all(color: wuiBtnBorderColor, width: 1 / MediaQuery.of(context).devicePixelRatio), |
162 | | - color: btnColor, |
163 | | - ), |
| 150 | + padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 24), |
164 | 151 | ), |
165 | 152 | ); |
166 | 153 | } |
| 154 | + |
| 155 | + @override |
| 156 | + void didUpdateWidget(Button oldWidget) { |
| 157 | + super.didUpdateWidget(oldWidget); |
| 158 | + if (widget.loading != oldWidget.loading || widget.disabled != oldWidget.disabled || widget.type != oldWidget.type) { |
| 159 | + initColor(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @override |
| 164 | + void didChangeDependencies() { |
| 165 | + super.didChangeDependencies(); |
| 166 | + initColor(); |
| 167 | + } |
167 | 168 | } |
0 commit comments