在中心的颤振位置堆栈小部件

问题描述 投票:4回答:4

我有一个堆栈中的小部件,所以我想将我的按钮栏放在堆栈的底部中心,但没有任何作用。小部件只是粘在左侧。这是我的代码。

new Positioned(
            bottom: 40.0,
            child: new Container(
              margin: const EdgeInsets.all(16.0),
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Align(
                    alignment: Alignment.bottomCenter,
                    child: new ButtonBar(
                      alignment: MainAxisAlignment.center,
                      children: <Widget>[
                        new OutlineButton(
                          onPressed: () {
                            Navigator.push(
                                context,
                                new MaterialPageRoute(
                                    builder: (context) => new LoginPage()));
                          },
                          child: new Text(
                            "Login",
                            style: new TextStyle(color: Colors.white),
                          ),
                        ),
                        new RaisedButton(
                          color: Colors.white,
                          onPressed: () {
                            Navigator.push(
                                context,
                                new MaterialPageRoute(
                                    builder: (context) =>
                                        new RegistrationPage()));
                          },
                          child: new Text(
                            "Register",
                            style: new TextStyle(color: Colors.black),
                          ),
                        )
                      ],
                    ),
                  )
                ],
              ),
            ),
          )

我确实尝试了每个中心对齐,请帮忙

dart flutter
4个回答
11
投票

删除所有内容,但是:

Align(
  alignment: Alignment.bottomCenter,
  child: new ButtonBar(
    alignment: MainAxisAlignment.center,
    children: <Widget>[
      new OutlineButton(
        onPressed: () {
          Navigator.push(
            context,
            new MaterialPageRoute(
              builder: (context) => new LoginPage()));
        },
        child: new Text(
          "Login",
          style: new TextStyle(color: Colors.white),
        ),
      ),
      new RaisedButton(
        color: Colors.white,
        onPressed: () {
          Navigator.push(
            context,
            new MaterialPageRoute(
              builder: (context) =>
                new RegistrationPage()));
        },
        child: new Text(
          "Register",
          style: new TextStyle(color: Colors.black),
        ),
      )
    ],
  ),
)

在我的理论中,额外的Container正在摧毁它。我建议你通过添加填充来包围它:

Padding(
  padding: EgdeInsets.only(bottom: 20.0),
  child: Align...
),

这对我来说似乎比Positioned更合理,而且我也不太了解你的Column只有一个孩子。


6
投票

问题是获得尽可能小的容器。

只需给容器一个width:(红色),你就完成了。

width: MediaQuery.of(context).size.width

enter image description here

  new Positioned(
  bottom: 0.0,
  child: new Container(
    width: MediaQuery.of(context).size.width,
    color: Colors.red,
    margin: const EdgeInsets.all(0.0),
    child: new Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        new Align(
          alignment: Alignment.bottomCenter,
          child: new ButtonBar(
            alignment: MainAxisAlignment.center,
            children: <Widget>[
              new OutlineButton(
                onPressed: null,
                child: new Text(
                  "Login",
                  style: new TextStyle(color: Colors.white),
                ),
              ),
              new RaisedButton(
                color: Colors.white,
                onPressed: null,
                child: new Text(
                  "Register",
                  style: new TextStyle(color: Colors.black),
                ),
              )
            ],
          ),
        )
      ],
    ),
  ),
),

5
投票

你可以在Stack中使用Positioned.fill和Align:

Stack(
 children: <Widget>[      
        Positioned.fill(
                child: Align(
                  alignment: Alignment.centerRight,
                  child: ....

                ),
              ),
           ],
         ),

3
投票

您可以更改堆栈内的定位:

Align(
  alignment: Alignment.bottomCenter,
  child: ... ,
),

有关Stack:Exploring Stack的更多信息

© www.soinside.com 2019 - 2024. All rights reserved.