Hi, 我是鱼板伯爵今天要教大家 Button 这个元件,教学内容只会撷取片段程序码,建议大家搭配完整程序码来练习。
可以加入小图标的按钮
color
:Icon的颜色highlightColor
:当按钮按下时的颜色splashColor
:当按钮按下时的水波颜色icon
:按钮IcononPressed
:按钮按下时所调用的逻辑,若为null
则不使用iconSize
: Icon大小,预设为24class DemoIconButton extends StatelessWidget {
const DemoIconButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: IconButton(
iconSize: 50,
color: Colors.amber,
// 水波和触发颜色为透明
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
icon: Icon(Icons.ac_unit),
onPressed: () {
log("click", name: "INFO");
},
),
);
}
}
文字按钮
style
:利用TextButton.styleFrom
变更按钮颜色、风格child
:子元件onPressed
:点击按钮时触发的逻辑onLongPress
:长压按钮时触发的逻辑class DemoTextButton extends StatelessWidget {
const DemoTextButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: TextButton(
child: Text(
"Hello",
style: TextStyle(color: Colors.white),
),
style: TextButton.styleFrom(
backgroundColor: Colors.green,
),
onPressed: () {
log("click", name: "INFO");
},
onLongPress: () {
log("long click", name: "INFO");
},
),
);
}
}
会有编框的按钮
style
:利用OutlinedButton.styleFrom
变更按钮颜色、风格child
:子元件onPressed
:点击按钮时触发的逻辑onLongPress
:长压按钮时触发的逻辑class DemoOutlinedButton extends StatelessWidget {
const DemoOutlinedButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: OutlinedButton(
child: Text("Hello"),
style: OutlinedButton.styleFrom(
primary: Colors.white,
backgroundColor: Colors.blueGrey,
),
onPressed: () {
log("click", name: "INFO");
},
onLongPress: () {
log("long click", name: "INFO");
},
),
);
}
}
这个按钮会有漂浮的感觉。
style
:利用ElevatedButton.styleFrom
变更按钮颜色、风格child
:子元件onPressed
:点击按钮时触发的逻辑onLongPress
:长压按钮时触发的逻辑class DemoElevatedButton extends StatelessWidget {
const DemoElevatedButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: ElevatedButton(
child: Text("Hello"),
style: ElevatedButton.styleFrom(
elevation: 20,
),
onPressed: () {
log("click", name: "INFO");
},
),
);
}
}
若将onPressed
後面带null
的话,按钮就会变成灰色不能按。
class DemoNullButton extends StatelessWidget {
const DemoNullButton({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: OutlinedButton(
child: Text("Hello"),
onPressed: null,
),
);
}
}
<<: [Day8]C# 鸡础观念- 如果没有如果,只有太多的如果switch陈述式
事件风暴 Event Storm 事件风暴 : 理解、访谈需求 目的 : 将商业流程视觉化,找出核心...
WebRTC技术经过多年的发展,已经非常成熟,它提供了HTML5流媒体技术的一整套解决方案及API,...
已经迈向第29天了,但我还在熟悉Nodejs的表面的感觉, 想在这倒数第二天做出有点技术的东西, 可...
在Constructor的章节中我们使用建立实体的方式 function Person(name, ...
Agenda 资安宣言 测试环境与工具 学习目标 技术原理与程序码 下期预告 资安宣言 撰写本系列文...