Material widget库中提供了多种按钮Widget如RaisedButton、FlatButton、OutlineButton等,它们都是直接或间接对RawMaterialButton的包装定制,所以他们大多数属性都和RawMaterialButton一样。在介绍各个按钮时我们先介绍其默认外观,而按钮的外观大都可以通过属性来自定义,我们在后面统一介绍这些属性。另外,所有Material 库中的按钮都有如下相同点:

  • 按下时都会有“水波动画”。
  • 有一个onPressed属性来设置点击回调,当按钮按下时会执行该回调,如果不提供该回调则按钮会处于禁用状态,禁用状态不响应用户点击。
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
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new MaterialApp(
title: 'flutter demo',
home: _home(),
);
}
}

class _home extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _homeState();
}
}

class _homeState extends State<_home> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
appBar: new AppBar(
title: Text("title"),
centerTitle: true,
),
body: new Column(
children: <Widget>[
RaisedButton(
child: Text("RaisedButton"),
onPressed: _pressed,//onPressed点击回调 这里设置的是一个空的方法 默认是null,为null时会禁用点击事件
elevation: 2.0,
//正常状态下的阴影
highlightElevation: 4.0,
//按下时的阴影
disabledElevation: 0.0,
// 禁用时的阴影
),
FlatButton(
child: Text("FlatButton"),
onPressed: _pressed,
),
OutlineButton(
child: Text("OutlineButton"),
onPressed: _pressed,
),
IconButton(
icon: Icon(Icons.thumb_up),
onPressed: _pressed,
),
FlatButton(
child: Text("自定义样式"),
//child按钮中的内容
textColor: Colors.white,
//文字颜色
disabledTextColor: Colors.red,
//按钮禁用时的文字颜色
color: Colors.lightBlue,
//背景颜色
disabledColor: Colors.grey,
//按钮禁用时的背景颜色
highlightColor: Colors.amber,
//按钮按下时的背景颜色
splashColor: Colors.black12,
//点击时,水波动画中水波的颜色
padding: EdgeInsets.all(2.0),
//内边距
colorBrightness: Brightness.dark,
////按钮主题,默认是浅色主题
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(4.0)),
//外形
onPressed: _pressed,
),
],
),
);
}
void _pressed() {}
}

效果图
#####监听文本变化
1.设置onChange回调

1
2
3
4
5
6
7
8
9
10
11
12
TextField(
autofocus: false,
keyboardType: TextInputType.number,
decoration: InputDecoration(
hintText: "用户密码",
prefixIcon: Icon(Icons.lock),
),
obscureText: true,
onChanged: (value){//输入内容改变的回调监听
print(value);
},
),

2.通过controller监听

1
2
3
 controller: textEditingController,//点击提交时的回调
TextEditingController textEditingController=new TextEditingController();
//在用户点击提交或其他情况下通过textEditingController.text获取输入的值