如何在 Flutter 中为按钮设置边距
How to set margin for a Button in Flutter
我只是在 Flutter 中创建一个表单。我无法设置按钮的上边距。
class _MyHomePageState extends State<MyHomePage> {
String firstname;
String lastname;
final scaffoldKey = new GlobalKey<ScaffoldState>();
final formKey = new GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
key: scaffoldKey,
appBar: new AppBar(
title: new Text('Validating forms'),
),
body: new Padding(
padding: const EdgeInsets.all(16.0),
child: new Form(
key: formKey,
child: new Column(
children: [
new TextFormField(
decoration: new InputDecoration(labelText: 'First Name'),
validator: (val) =>
val.length == 0 ?"Enter FirstName" : null,
onSaved: (val) => firstname = val,
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Password'),
validator: (val) =>
val.length ==0 ? 'Enter LastName' : null,
onSaved: (val) => lastname = val,
obscureText: true,
),
new RaisedButton(
onPressed: _submit,
child: new Text('Login'),
),
],
),
),
),
);
}
将按钮放在 Container 内,然后设置边距
new Container(
margin: const EdgeInsets.only(top: 10.0),
child : new RaisedButton(
onPressed: _submit,
child: new Text('Login'),
),
或者,您可以使用 Padding 包裹按钮。 (这就是 Container 内部所做的。)
Padding(
padding: const EdgeInsets.all(20),
child: ElevatedButton(
onPressed: _submit,
child: Text('Login'),
),
);
请参阅 更多关于向小部件添加边距的信息。
注意:从 Flutter 2.0 开始,RaisedButton
已弃用。 ElevatedButton
是替换。
我找到了 EdgeInsets.symmetric:
child : new RaisedButton(
onPressed: _submit,
child: new Text('Login'),
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 10),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(data.dataList[index].codeName, style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: UtilColors.darkGrey)),
Row(
//TODO to edit employee details
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Expanded(child: Text(BasicAction.formatDate(data.dataList[index].payDate), style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500, color: UtilColors.darkGrey))),
Padding(
padding: EdgeInsets.all(4),
child: Text('Edit', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: UtilColors.blueColor)),
),
],
),
],
),
in this on edit text how to set clicklistenr
截图:
使用推荐的 ElevatedButton.style
属性 来提供填充。
ElevatedButton(
onPressed: () {},
child: Text('Button'),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.all(20), // Set padding
),
)
您可以像这样在 Card 上使用 margin 和 padding :-
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: _newsArticles.length,
padding: EdgeInsets.symmetric(horizontal: 2,vertical: 2),
itemBuilder: (context, index) {
return Card( // <-- Card widget
shadowColor: Colors.grey,
elevation: 3,
margin: EdgeInsets.all(5.0),
child: ListTile(
title: _newsArticles[index].urlToImage == null ?
Image.asset(Constants.NEWS_PLACEHOLDER_IMAGE_ASSET_URL) :
Image.network(_newsArticles[index].urlToImage),
subtitle: Text(_newsArticles[index].title, style: TextStyle(fontSize: 14)),
),
);
},
)
);
}
在 Flutter 2.0 中,RaisedButton 被弃用,ElevatedButton 被替代。
所以你可以使用 ElevatedButton 并将样式设置为波纹管以删除边距:
ElevatedButton.icon(
onPressed: () {},
icon: Icon(Icons.add),
label: Text(
'Add Place',
),
style: ButtonStyle(tapTargetSize: MaterialTapTargetSize.shrinkWrap),
),
这就是我一般设置小部件的大小、填充和边距的方式。这是按钮的示例:
Container(
margin: EdgeInsets.only(top: 10), // Top Margin
child:
ElevatedButton(
style: TextButton.styleFrom(
// Inside Padding
padding: EdgeInsets.symmetric(horizontal: 0, vertical: 20),
// Width,Height
minimumSize: Size(300, 30),
),
child: Text('Upload Data'),
onPressed: () {submitForm();},
),
),
其他选项是
Container(
margin: EdgeInsets.only(top:20),
child:
ElevatedButton(
设置水平和垂直边距
Container(
margin: EdgeInsets.symmetric(horizontal: 0, vertical: 20),
child:
Container(
margin: EdgeInsets.all(20),
child:
ElevatedButton(
Container(
margin: EdgeInsets.fromLTRB(5,10,5,10),
child:
ElevatedButton(
图像上边距:
Scaffold(
backgroundColor: Color.fromARGB(255, 238, 237, 237),
body: Center(
child: Container(
margin: const EdgeInsets.only(top: 25),
child: Column(
children: <Widget>[
Column(children: <Widget>[
Image.asset(
'images/logo.png',
fit: BoxFit.contain,
width: 130,
)
]),
],
),
),
),
)
这对我有用!
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Global.deepOrange,
padding: const EdgeInsets.all(20), // at this line you can add margin.
),
onPressed: () {},
child: const Text('Add New Poll Item')),
我只是在 Flutter 中创建一个表单。我无法设置按钮的上边距。
class _MyHomePageState extends State<MyHomePage> {
String firstname;
String lastname;
final scaffoldKey = new GlobalKey<ScaffoldState>();
final formKey = new GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return new Scaffold(
key: scaffoldKey,
appBar: new AppBar(
title: new Text('Validating forms'),
),
body: new Padding(
padding: const EdgeInsets.all(16.0),
child: new Form(
key: formKey,
child: new Column(
children: [
new TextFormField(
decoration: new InputDecoration(labelText: 'First Name'),
validator: (val) =>
val.length == 0 ?"Enter FirstName" : null,
onSaved: (val) => firstname = val,
),
new TextFormField(
decoration: new InputDecoration(labelText: 'Password'),
validator: (val) =>
val.length ==0 ? 'Enter LastName' : null,
onSaved: (val) => lastname = val,
obscureText: true,
),
new RaisedButton(
onPressed: _submit,
child: new Text('Login'),
),
],
),
),
),
);
}
将按钮放在 Container 内,然后设置边距
new Container(
margin: const EdgeInsets.only(top: 10.0),
child : new RaisedButton(
onPressed: _submit,
child: new Text('Login'),
),
或者,您可以使用 Padding 包裹按钮。 (这就是 Container 内部所做的。)
Padding(
padding: const EdgeInsets.all(20),
child: ElevatedButton(
onPressed: _submit,
child: Text('Login'),
),
);
请参阅
注意:从 Flutter 2.0 开始,RaisedButton
已弃用。 ElevatedButton
是替换。
我找到了 EdgeInsets.symmetric:
child : new RaisedButton(
onPressed: _submit,
child: new Text('Login'),
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 10),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(data.dataList[index].codeName, style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: UtilColors.darkGrey)),
Row(
//TODO to edit employee details
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Expanded(child: Text(BasicAction.formatDate(data.dataList[index].payDate), style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500, color: UtilColors.darkGrey))),
Padding(
padding: EdgeInsets.all(4),
child: Text('Edit', style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: UtilColors.blueColor)),
),
],
),
],
),
in this on edit text how to set clicklistenr
截图:
使用推荐的 ElevatedButton.style
属性 来提供填充。
ElevatedButton(
onPressed: () {},
child: Text('Button'),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.all(20), // Set padding
),
)
您可以像这样在 Card 上使用 margin 和 padding :-
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: _newsArticles.length,
padding: EdgeInsets.symmetric(horizontal: 2,vertical: 2),
itemBuilder: (context, index) {
return Card( // <-- Card widget
shadowColor: Colors.grey,
elevation: 3,
margin: EdgeInsets.all(5.0),
child: ListTile(
title: _newsArticles[index].urlToImage == null ?
Image.asset(Constants.NEWS_PLACEHOLDER_IMAGE_ASSET_URL) :
Image.network(_newsArticles[index].urlToImage),
subtitle: Text(_newsArticles[index].title, style: TextStyle(fontSize: 14)),
),
);
},
)
);
}
在 Flutter 2.0 中,RaisedButton 被弃用,ElevatedButton 被替代。
所以你可以使用 ElevatedButton 并将样式设置为波纹管以删除边距:
ElevatedButton.icon(
onPressed: () {},
icon: Icon(Icons.add),
label: Text(
'Add Place',
),
style: ButtonStyle(tapTargetSize: MaterialTapTargetSize.shrinkWrap),
),
这就是我一般设置小部件的大小、填充和边距的方式。这是按钮的示例:
Container(
margin: EdgeInsets.only(top: 10), // Top Margin
child:
ElevatedButton(
style: TextButton.styleFrom(
// Inside Padding
padding: EdgeInsets.symmetric(horizontal: 0, vertical: 20),
// Width,Height
minimumSize: Size(300, 30),
),
child: Text('Upload Data'),
onPressed: () {submitForm();},
),
),
其他选项是
Container(
margin: EdgeInsets.only(top:20),
child:
ElevatedButton(
设置水平和垂直边距
Container(
margin: EdgeInsets.symmetric(horizontal: 0, vertical: 20),
child:
Container(
margin: EdgeInsets.all(20),
child:
ElevatedButton(
Container(
margin: EdgeInsets.fromLTRB(5,10,5,10),
child:
ElevatedButton(
图像上边距:
Scaffold(
backgroundColor: Color.fromARGB(255, 238, 237, 237),
body: Center(
child: Container(
margin: const EdgeInsets.only(top: 25),
child: Column(
children: <Widget>[
Column(children: <Widget>[
Image.asset(
'images/logo.png',
fit: BoxFit.contain,
width: 130,
)
]),
],
),
),
),
)
这对我有用!
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Global.deepOrange,
padding: const EdgeInsets.all(20), // at this line you can add margin.
),
onPressed: () {},
child: const Text('Add New Poll Item')),