47

我只是在 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'),
              ),
            ],
          ),
        ),
      ),
    );
  }
4

9 回答 9

114

将您的按钮放入容器中,然后设置边距

new Container(
    margin: const EdgeInsets.only(top: 10.0),
    child : new RaisedButton(
                onPressed: _submit,
                child: new Text('Login'),
              ),
于 2018-05-05T11:22:42.080 回答
39

或者,您可以使用 Padding 包裹您的按钮。(这就是 Container 在内部所做的。)

Padding(
  padding: const EdgeInsets.all(20),
  child: ElevatedButton(
    onPressed: _submit,
    child: Text('Login'),
  ),
);

有关向小部件添加边距的更多信息,请参阅此答案。

注意RaisedButton自 Flutter 2.0 起已弃用。ElevatedButton是替代品。

于 2018-12-08T03:15:12.607 回答
4

在 Flutter 2.0 中, RaisedButton 已被弃用, ElevatedButton 已被替换。
因此,您可以使用 ElevatedButton 并将样式设置为如下所示以删除边距:

      ElevatedButton.icon(
        onPressed: () {},
        icon: Icon(Icons.add),
        label: Text(
          'Add Place',
        ),
        style: ButtonStyle(tapTargetSize: MaterialTapTargetSize.shrinkWrap),
      ),
于 2021-04-10T05:03:06.543 回答
2

图像边缘顶部:

        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,
                )
              ]),
              
            ],
          ),
        ),
      ),
    )
于 2021-10-06T11:17:22.703 回答
1

我找到了 EdgeInsets.symmetric:

child : new RaisedButton(
   onPressed: _submit,
   child: new Text('Login'),
   padding: EdgeInsets.symmetric(horizontal: 50, vertical: 10),
),
于 2020-09-10T02:59:43.127 回答
1

截屏:

在此处输入图像描述

使用推荐的ElevatedButton.style属性来提供填充。

ElevatedButton(
  onPressed: () {},
  child: Text('Button'),
  style: ElevatedButton.styleFrom(
    padding: EdgeInsets.all(20), // Set padding
  ),
)
于 2020-10-05T11:23:17.513 回答
1

这就是我一般设置小部件的大小、填充和边距的方式。这是按钮的示例:

   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(

在此处输入图像描述

于 2021-07-16T07:43:20.780 回答
0
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 
于 2020-09-14T12:19:47.673 回答
0

您可以像这样在Card上使用边距填充:-

 @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)),
              ),
            );
          },
        )
    );
  }
于 2021-01-06T06:23:43.570 回答