![](/img/trans.png)
[英]Flutter | How to make custom button of BottomNavigationBar in flutter?
[英]Flutter: How to make a button like this?
class Test extends StatelessWidget {
const Test({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
const SizedBox(height: 10,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconWithTextButton(icon:const Icon(Icons.lock, color: Colors.red,),text: "Lock",onClicked: (){},),
IconWithTextButton(icon:const Icon(Icons.alarm, color: Colors.amber),text: "Alarm",onClicked: (){},),
IconWithTextButton(icon:const Icon(Icons.call, color: Colors.green),text: "call",onClicked: (){},),
IconWithTextButton(icon:const Icon(Icons.wrong_location_sharp, color: Colors.greenAccent),text: "location",onClicked: (){},),
IconWithTextButton(icon:const Icon(Icons.add, color: Colors.tealAccent),text: "Add",onClicked: (){},),
],
),
const SizedBox(height: 20,),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconWithTextButton(icon: const Icon(Icons.lock, color: Colors.red,),text: "Lock",onClicked: (){},),
IconWithTextButton(icon:const Icon(Icons.alarm, color: Colors.amber),text: "Alarm",onClicked: (){},),
IconWithTextButton(icon:const Icon(Icons.call, color: Colors.green),text: "call",onClicked: (){},),
IconWithTextButton(icon:const Icon(Icons.wrong_location_sharp, color: Colors.greenAccent),text: "location",onClicked: (){},),
IconWithTextButton(icon:const Icon(Icons.add, color: Colors.tealAccent),text: "Add",onClicked: (){},),
],
)
],
),
),
);
}
}
class IconWithTextButton extends StatelessWidget {
String text;
Icon icon;
VoidCallback onClicked;
IconWithTextButton({Key? key, required this.text, required this.onClicked, required this.icon}) : super(key: key);
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onClicked,
child: Container(
child: Column(
children: [
icon,
const SizedBox(height: 10,),
Text(text),
],
),
),
);
}
}
您可以使用 Column Widget 並使用 Icon 和 TextField 填充其子項。 然后用 GestureDetector 包裹整個小部件以實現按下功能。 你也可以用 InkWell 和 Material 包裹它,以獲得像其他按鈕一樣的飛濺效果。
你不需要用柱子包裹。
TextButton(
onPressed:(){},
child: Column(
children: [
Icon(Icons.play_arrow,color:Colors.green),
Text(
'Hello',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
我認為 GirdView Widget 是制作這樣的按鈕的最佳方式-
GridView.count(
crossAxisCount: 4,
children: List<Widget>.generate(16, (index) {
return GridTile(
child:GestureDetector(
onTap:(){
print(index.toString());
},
child: Card(
color: Colors.blue.shade200,
child: Column(
children:[ Text('tile $index'),
Icon(Icons.alarm)
])
)),
);
}),
),
借助索引參數,您可以根據按鈕類型傳遞 function。 如果這個答案對您有幫助,請給答案點贊。 謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.