在flutter中,按钮组件有如下经常使用属性:less
class HomeContent extends StatelessWidget{ @override Widget build(BuildContext context) { return Column( children: <Widget>[ Row( children: <Widget>[ RaisedButton( child:Text('普通按钮'), onPressed: (){ print("这是一个普通按钮"); }, ), ], ), ], ); } }
上面使用RaisedButton组件实现了一个最简单的按钮,而后,能够在此基础上添加各类样式:ide
在上面的经常使用属性中,是没有宽高属性的,所以若是须要人为调整按钮的大小,须要在按钮的外层套一层Container,而后设置这个Container的宽高:ui
若是须要屡次使用按钮,每次都像上面那样写的话,会十分麻烦,所以,能够在按钮组件的基础上进行简单的封装,实现本身的按钮组件:this
class MyButton extends StatelessWidget { final text; final pressed; final double width; final double height; const MyButton({this.text='',this.pressed=null,this.width=80,this.height=30}) ; @override Widget build(BuildContext context) { return Container( height: this.height, width: this.width, child: RaisedButton( child: Text(this.text), onPressed:this.pressed , ), ); } }
代码下载:点这里(提取码:axtj)spa