Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dddd92b

Browse files
Merge branch 'beta'
2 parents c08d773 + c9641c0 commit dddd92b

File tree

3 files changed

+209
-170
lines changed

3 files changed

+209
-170
lines changed

‎example/lib/main.dart‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,18 @@ class _MyHomePageState extends State<MyHomePage> {
5757
// The Flutter framework has been optimized to make rerunning build methods
5858
// fast, so that you can just rebuild anything that needs updating rather
5959
// than having to individually change instances of widgets.
60-
return Scaffold(
60+
return WeUIApp(child:Scaffold(
6161
appBar: null,
6262
body: NotificationListener(
6363
onNotification: (e){
64-
print(e.toString());
64+
// print(e.toString());
6565
},
6666
child: ListView(
6767
//physics: BouncingScrollPhysics(),
6868
children: <Widget>[
69+
// Button(),
70+
// Button(type: ButtonType.primay,),
71+
// Button(type: ButtonType.warn,),
6972
SizedBox(
7073
height: 200,
7174
child: PageView(
@@ -92,6 +95,15 @@ class _MyHomePageState extends State<MyHomePage> {
9295
// horizontal).
9396
mainAxisAlignment: MainAxisAlignment.center,
9497
children: <Widget>[
98+
Button(),
99+
Button(type: ButtonType.primay,),
100+
Button(type: ButtonType.warn,),
101+
Button(loading: true,),
102+
Button(type: ButtonType.primay,loading: true,),
103+
Button(type: ButtonType.warn,loading: true,),
104+
Button(disabled: true,),
105+
Button(type: ButtonType.primay,disabled: true,),
106+
Button(type: ButtonType.warn,disabled: true,),
95107
buildCells(context),
96108
Ink(
97109
decoration: BoxDecoration(color: Colors.red, borderRadius: BorderRadius.all(Radius.circular(50))),
@@ -124,10 +136,11 @@ class _MyHomePageState extends State<MyHomePage> {
124136
),
125137
onPressed: () {},
126138
),
139+
WeUIApp(child:
127140
Footer(
128-
text: "sdfsfd",
141+
// text: "sdfsfd",
129142
links: [Link(text: "sdfds"), Link(text: "sdfsf")],
130-
),
143+
),),
131144
Article(
132145
children: <Widget>[
133146
H1(
@@ -179,7 +192,7 @@ class _MyHomePageState extends State<MyHomePage> {
179192
tooltip: 'Increment',
180193
child: Icon(Icons.add),
181194
), // This trailing comma makes auto-formatting nicer for build methods.
182-
);
195+
));
183196
}
184197

185198
Cells buildCells(BuildContext context) {

‎lib/src/button.dart‎

Lines changed: 123 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,168 @@
11
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';
34
import 'package:flutter/cupertino.dart';
45

56
///按钮的三种类型
7+
68
enum ButtonType {
7-
btnPrimary,
8-
btnWarn,
9-
btnDefault,
9+
primay,
10+
warn,
11+
default_,
1012
}
1113

1214
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;
2128
@override
22-
State<StatefulWidget> createState() => _Button();
29+
_ButtonState createState() => _ButtonState();
2330
}
2431

25-
class _ButtonColor {
32+
class _ButtonStateextendsState<Button> {
2633
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;
5140

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;
6742

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);
7444

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;
9246

9347
@override
9448
void initState() {
9549
super.initState();
50+
}
9651

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;
10588
}
89+
markNeedBuild();
10690
}
10791

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();
11396
}
11497

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();
121104
}
122105

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();
129110
}
130111

112+
markNeedBuild() => setState(() {});
113+
131114
@override
132115
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;
133121
return GestureDetector(
134-
onTapDown: _onTapDown,
135-
onTapUp: _onTapUp,
122+
onTapDown: onTapDown,
123+
onTapUp: onTapUp,
124+
onTapCancel: onTapCancel,
125+
behavior: HitTestBehavior.opaque,
136126
child: Container(
137-
height: wuiBtnHeight,
127+
width: btnWidth,
128+
decoration: BoxDecoration(borderRadius: BorderRadius.circular(radius), color: bgColor),
138129
child: Row(
139130
mainAxisAlignment: MainAxisAlignment.center,
140-
crossAxisAlignment: CrossAxisAlignment.center,
141131
children: <Widget>[
142-
widget.loading == true
132+
widget.loading == true&&!widget.disabled
143133
? Padding(
144-
padding: constEdgeInsets.only(right: 10),
145-
child: CupertinoActivityIndicator(),
134+
padding: EdgeInsets.only(right: 0.34* fontSize),
135+
child: CupertinoActivityIndicator(radius:9),
146136
)
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+
),
153146
),
154-
)
147+
),
155148
],
156149
),
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),
164151
),
165152
);
166153
}
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+
}
167168
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /