【问题标题】:How to set margin for a Button in Flutter如何在 Flutter 中为 Button 设置边距
【发布时间】:2018-10-15 15:03:58
【问题描述】:

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

【问题讨论】:

    标签: button flutter margin flutter-layout


    【解决方案1】:

    将您的按钮放在Container 中,然后设置边距

    new Container(
        margin: const EdgeInsets.only(top: 10.0),
        child : new RaisedButton(
                    onPressed: _submit,
                    child: new Text('Login'),
                  ),
    

    【讨论】:

      【解决方案2】:

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

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

      请参阅this answer 详细了解为小部件添加边距。

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

      【讨论】:

      • 这应该被接受,因为 Padding 可以标记为 const,而 Container 不能。
      【解决方案3】:

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

            ElevatedButton.icon(
              onPressed: () {},
              icon: Icon(Icons.add),
              label: Text(
                'Add Place',
              ),
              style: ButtonStyle(tapTargetSize: MaterialTapTargetSize.shrinkWrap),
            ),
      

      【讨论】:

        【解决方案4】:

        图片页边距:

                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,
                        )
                      ]),
                      
                    ],
                  ),
                ),
              ),
            )
        

        【讨论】:

          【解决方案5】:

          我找到了 EdgeInsets.symmetric:

          child : new RaisedButton(
             onPressed: _submit,
             child: new Text('Login'),
             padding: EdgeInsets.symmetric(horizontal: 50, vertical: 10),
          ),
          

          【讨论】:

            【解决方案6】:

            截图:

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

            ElevatedButton(
              onPressed: () {},
              child: Text('Button'),
              style: ElevatedButton.styleFrom(
                padding: EdgeInsets.all(20), // Set padding
              ),
            )
            

            【讨论】:

              【解决方案7】:

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

                 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(
              

              【讨论】:

                【解决方案8】:
                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 
                

                【讨论】:

                  【解决方案9】:

                  您可以像这样在 Card 上使用 ma​​rginpadding:-

                   @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)),
                                ),
                              );
                            },
                          )
                      );
                    }
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2011-05-19
                    • 2022-10-21
                    • 1970-01-01
                    • 2020-05-25
                    • 1970-01-01
                    • 2023-03-08
                    • 2012-09-16
                    • 2017-12-15
                    相关资源
                    最近更新 更多