Flutter 中按鈕這種 widget 種類可是不少:
RaisedButton
- 凸起按鈕,帶陰影RaisedButton
- 扁平按鈕,不帶陰影OutlineButton
- 自帶邊框的按鈕,不能設置背景色color
MaterialButton
- MD 風格的按鈕,可以設置寬高值,是 Flutter 中所有按鈕的基類IconButton
- 在Icon
基礎上加上點擊事件,不能設置背景色和邊框FloatingActionButton
- 懸浮按鈕ButtonBar
按鈕組RawMaterialButton
橫向按鈕組
MaterialButton
MaterialButton
是 Flutter 中所有 Button
類型的基類,MaterialButton
的參數再其他 Button
中都是通用的,我們簡單看一下:
const MaterialButton({
Key key,
@required this.onPressed, // 點擊事件,必須給值,寫null的話表示不可用
this.onHighlightChanged,
this.textTheme, //默認文字樣式
this.textColor, // 文字顏色
this.disabledTextColor, // 不可用時文字顏色
this.color, // 背景色
this.disabledColor, // 按鈕被禁用時的文字顏色
this.highlightColor, // 按鈕的水波紋亮起的顏色,點擊之后的顏色,注意這個顏色點擊之后就一直在這里了,不是那種一瞬間顯示
this.splashColor, // 波浪紋顏色
this.colorBrightness, // 按鈕主題高亮
this.elevation, // Z軸、陰影,正常應該小于 5
this.highlightElevation, // 按鈕高亮時的下面的陰影長度
this.disabledElevation,
this.padding,
this.shape, // 按鈕樣式
this.clipBehavior = Clip.none,
this.materialTapTargetSize,
this.animationDuration,
this.minWidth,
this.height,
this.child, // 按鈕內部widget,一般都是text
})
1. textTheme
系統默認提供的配色方案:
ButtonTextTheme.normal:
ButtonTextTheme.accent:
ButtonTextTheme.primary:
2. shape
提供了4種預設的按鈕形狀:
BeveledRectangleBorder
CircleBorder
RoundedRectangleBorder
StadiumBorder
3. 按鈕按下時圖示:
RaisedButton
RaisedButton
- 凸起按鈕,帶陰影的那種
class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () {
print("AAAAAA");
},
color: Colors.blueAccent,
child: Text(
"浮動按鈕",
style: TextStyle(fontSize: 30),
),
textColor: Colors.white,
elevation: 10,
disabledColor: Colors.grey,
padding: EdgeInsets.all(20),
splashColor: Colors.red,
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(10),
));
}
}
FlatButton
FlatButton
- 扁平按鈕,不能帶陰影,其他和 RaisedButton 一樣
class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FlatButton(
onPressed: () {
print("AAAAAA");
},
color: Colors.blueAccent,
child: Text(
"浮動按鈕",
style: TextStyle(fontSize: 30),
),
textColor: Colors.white,
disabledColor: Colors.grey,
padding: EdgeInsets.all(20),
splashColor: Colors.red,
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(10),
));
}
}
OutlineButton
OutlineButton
- 自帶邊框的按鈕,不能設置背景色color
class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new OutlineButton(
onPressed: () {
print(BorderStyle.values);
},
borderSide: BorderSide(
color: Colors.blue,
width: 2.0,
style: BorderStyle.solid
),
child: new Text('pressed'),
);
}
}
MaterialButton
MaterialButton
- MD 風格的按鈕,屬性同上,但是可以設置寬高值
class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialButton(
minWidth: 200,
height: 80,
color: Colors.blue,
textColor: Colors.white,
onPressed: () {
print("AAAAAA");
},
child: new Text('button'),
);
}
}
IconButton
IconButton
- 在 Icon
基礎上加上點擊事件,不能設置背景色和邊框,圖就沒必要放了,child
除了可以接受Icon
之外,還可以接受RichText
class DD extends StatelessWidget {
@override
Widget build(BuildContext context) {
return IconButton(
onPressed: () {
print("AAAAAA");
},
color: Colors.red,
iconSize: 50,
icon: new Icon(Icons.star),
padding: const EdgeInsets.all(12.0)
);
}
}
FloatingActionButton
基本設置
FloatingActionButton
這個大家都是耳熟能詳了吧,如果有時間,可以看看:FlatButton的Material設計理念,這是官方指導,FloatingActionButton
所有典型應用案例都在里面了,雖說是英文的,但是大家看圖就知道咋回事了圖隨便找的
child
很隨意,可以是 Text、icon, etctooltip
長按提示foregroundColor
child 沒有設置顏色時生效backgroundColor
背景色elevation
陰影highlightElevation
按下時陰影shape
形狀,設置shape時,默認的elevation將會失效,默認為CircleBordermini
是小圖標還是大圖標heroTag
hero效果使用的tag,系統默認會給所有FAB使用同一個tag,方便做動畫效果isExtended
是否為”extended”類型
@override
Widget build(BuildContext context) {
return FloatingActionButton(
child: Icon(Icons.adb),
tooltip: "tips",
backgroundColor: Colors.blueAccent,
foregroundColor: Colors.amberAccent,
onPressed: (){
print("tips!");
},
);
}
FloatingActionButton 3種樣式
regular
大mini
小extended
擴展
regular
和 mini
就是按鈕大小的事,通過 min
true/false 就能切換,但是 extended
就得借助 FloatingActionButton.extended
這個專門的構造函數了,其樣式是 icon 在前,文字在后,沒有 child 標簽了,而是 icon 和 label 了
FloatingActionButton 定位問題
android 的 FloatingActionButton 是可以定位的,Flutter 也一樣,甚至還可以和頂部的 ActionBar
底部的 BottomNavigationBar
聯動,經典的樣式就是這樣:
一共有6個位置:
-
一般定位:
-
endFloat
右小角,距離底部有一定距離 -
endDocked
右小角,頂著底部顯示 -
centerFloat
居中下至,距離底部有一定距離 -
聯動定位:
-
startTop
左上角,距離左邊有一定距離,錨定ActionBar
中間 -
endTop
右上角,距離右邊有一定距離,錨定ActionBar
中間 -
centerDocked
居中下至,錨定BottomNavigationBar
中間
和 BottomNavigationBar
聯動的例子
return new Scaffold(
floatingActionButton: new Builder(builder: (BuildContext context) {
return new FloatingActionButton(
child: const Icon(Icons.add),
tooltip: "Hello",
heroTag: null,
foregroundColor: Colors.white,
backgroundColor: Colors.black,
elevation: 7.0,
highlightElevation: 14.0,
);
}),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: new Theme(
data: Theme.of(context).copyWith(
// sets the background color of the `BottomNavigationBar`
canvasColor: Colors.black12,
// sets the active color of the `BottomNavigationBar` if `Brightness` is light
primaryColor: Colors.red,
textTheme: Theme.of(context)
.textTheme
.copyWith(caption: new TextStyle(color: Colors.white))),
child: new BottomNavigationBar(
fixedColor: Colors.lightBlue,
items: [
new BottomNavigationBarItem(
icon: new Icon(Icons.home), title: new Text("每日干貨")),
new BottomNavigationBarItem(
icon: new Icon(Icons.category),
title: new Text("分類閱讀"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.whatshot),
title: new Text("匠心寫作"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.search),
title: new Text("搜索"),
),
],
type: BottomNavigationBarType.fixed,
onTap: (int selected) {
setState(() {
});
},
),
),
);
ButtonBar
這個比較簡單
alignment
布局方向,默認 MainAxisAlignment.endmainAxisSize
主軸大小,默認 MainAxisSize.maxchildren
RawMaterialButton
RawMaterialButton
和上面差不舵,也是一組按鈕,沒有背景 沒有邊框 有點擊效果的按鈕。其實寫不寫都沒事,主要是讓大家認識下,別覺得沒見過
和 FlatButton 沒有什么區別