按钮的使用大致相同,无非样式和各种交互的回调,这里以 TextButton 为例:
1 2 3 4 5 6 7 8 9 10
| TextButton( child: Text('TextButton'), onPressed: () {}, onLongPress: (){}, style: ButtonStyle( foregroundColor: MaterialStateProperty.all(Colors.cyan), textStyle: MaterialStateProperty.all(TextStyle(fontSize: 20)), ), )
|
注意:字体颜色的设置不通过textStyle 设置,而是通过 foregroundColor
样式的构造函数如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| const ButtonStyle({ this.textStyle, this.backgroundColor, this.foregroundColor, this.overlayColor, this.shadowColor, this.elevation, this.padding, this.minimumSize, this.side, this.shape, this.mouseCursor, this.visualDensity, this.tapTargetSize, this.animationDuration, this.enableFeedback, });
|
shape 属性可用的值举例:
- BeveledRectangleBorder 尖端斜角
- CircleBorder 圆形
- RoundedRectangleBorder 圆角
- StadiumBorder 半圆角(腰圆)
其他的按钮还有:
- RaisedButton(Material 风格凸起的按钮,1.22版本前,不建议使用,使用 ElevatedButton)
- FlatButton ( Material 风格扁平的按钮,1.22版本前,推荐使用 TextButton)
- OutlinedButton(带边框的按钮,1.22 前有 OutlineButton)
- IconButton(图标按钮)
- DropdownButton(Material Style 下拉菜单按钮)
- PopupMenuButton(菜单选择框按钮)
- FloatingActionButton(悬浮按钮)
1.22 以后出现的新按钮,可以作统一全局的样式管理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| MaterialApp( title: 'Flutter Demo', theme: ThemeData( textButtonTheme: TextButtonThemeData( style: ButtonStyle() ), elevatedButtonTheme: ElevatedButtonThemeData( style: ButtonStyle() ), outlinedButtonTheme: OutlinedButtonThemeData( style: ButtonStyle() ) ), home: MyHomePage(title: 'Flutter Demo Home Page'), )
|