【问题标题】:bottom center some widgets in a container底部居中容器中的一些小部件
【发布时间】:2019-04-18 13:50:18
【问题描述】:

我想在我的容器中居中放置一个图像和按钮 .. 所以我使用了 align 并将其设置为 bottomCenter .. 但它不起作用!

这是我的容器:经过一些更新

           return new Container(
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            decoration: new BoxDecoration(
              image: new DecorationImage(
                image: new AssetImage(
                    globals.uiLabels['wthimg' + (index + 1).toString()]
                    [globals.currentLang]),
                fit: BoxFit.cover,
              ),
            ),
            child: Stack(
              children: <Widget>[
                Column(
                  mainAxisSize: MainAxisSize.max,
                  children: <Widget>[
                    new Padding(
                      padding: EdgeInsets.only(
                          top: MediaQuery.of(context).size.height/2 +100),
                      child: new Column(
                        children: <Widget>[
                          new Text(
                            globals.uiLabels[
                            'wthtitle' + (index + 1).toString()]
                            [globals.currentLang],
                            style: TextStyle(
                                fontSize: 18.0,
                                color: Colors.white,
                                fontWeight: ui.FontWeight.bold),
                          ),],),),
                    new Padding(
                      padding:
                      EdgeInsets.only(top: 20, right: 10, left: 10),
                      child: new Column(
                        children: <Widget>[
                          new Text(
                            globals.uiLabels[
                            'wthdesc' + (index + 1).toString()]
                            [globals.currentLang],
                            softWrap: true,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              fontSize: 16.0,
                              color: Colors.white,
                            ),),],),),],),
                Align(
                  child: Padding(
                    padding: EdgeInsets.only(top: 10),
                    child: Column(
                      children: <Widget>[
                        Padding(
                          padding: EdgeInsets.only(top: 20.0),
                          child: new Image(
                            image: AssetImage(globals.uiLabels[
                            'wthpager' +
                                (index + 1).toString()]
                            [globals.currentLang]),
                            width: 70,
                          ),
                        ),
                        new RaisedButton(
                          shape: new RoundedRectangleBorder(
                              borderRadius:
                              new BorderRadius.circular(5.0)),
                          child: new Text(
                            globals.uiLabels['skip']
                            [globals.currentLang],
                            style: new TextStyle(
                                color: Colors.white, fontSize: 18.0),
                          ),
                          color: Color(0x0049C275),
                          elevation: 0.0,
                          onPressed: () {
                            navigationPage();
                          },),],),),
                  alignment: Alignment.bottomCenter,
                )],),);

我想要底部居中..是对齐小部件内的部分...

尝试了对齐、堆栈和行..但没有一个成功...

如何解决这个问题?并使其始终以底部为中心...

跳过它上面的图像..我希望它们位于页面的底部中心..但现在它们位于顶部中心

【问题讨论】:

  • 因为ColumnRaisedButtonColumn 内部居中,但 Column 如果不适合或居中在 Container 内部
  • 使用堆栈小部件并在其中使用对齐小部件。
  • @AndreyTurkovsky 那么我该如何解决呢?
  • @ibhavikmakwana 试图这样做,但也没有用..
  • @pskink 得知 debugPaintSizeEnabled 未定义 ...

标签: dart flutter


【解决方案1】:

更新:

1 - 顶部填充使用- 100 而不是+100。其实改用mainAxisAlignment: MainAxisAlignment.center,会更好

2 - 删除不必要的Columns

3 - 在最后一个Column 设置mainAxisSize: MainAxisSize.min,

Container(
  height: MediaQuery.of(context).size.height,
  width: MediaQuery.of(context).size.width,
  color: Colors.black45,
  child: Stack(
    children: <Widget>[
      Column(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.only(
                top: MediaQuery.of(context).size.height/2 - 100),
            child: Text('this is the title',
                  style: TextStyle(
                      fontSize: 18.0,
                      color: Colors.white,
                      fontWeight: ui.FontWeight.bold),
                ),),
          Padding(
            padding:
            EdgeInsets.only(top: 20, right: 10, left: 10),
            child: Text('this is a description... ' * 3,
                  softWrap: true,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 16.0,
                    color: Colors.white,
                  ),),),],),
      Align(
        child: Padding(
          padding: EdgeInsets.only(top: 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(top: 20.0),
                child: new Image(
                  image: AssetImage('assets/fav_list_full.png'),
                  width: 70,
                ),
              ),
              new RaisedButton(
                shape: new RoundedRectangleBorder(
                    borderRadius:
                    new BorderRadius.circular(5.0)),
                child: new Text('skip',
                  style: new TextStyle(
                      color: Colors.white, fontSize: 18.0),
                ),
                color: Color(0x0049C275),
                elevation: 0.0,
                onPressed: () {
                  navigationPage();
                },),],),),
        alignment: Alignment.bottomCenter,
      )],),);

【讨论】:

  • 然后尝试将父 Column 包裹在 Expanded
  • 还是一样的:(
  • 还是一样的T____T!!它放置在第二个文本之后..所以如果我有 2 行文本或 3 行文本,它将位于不同的位置,而不是始终放在页面末尾...
【解决方案2】:

尝试使用以下代码:

已创建示例:

child: Container(
          height: 300.0,
          width: 300.0,
          color: Colors.orange,
          child: Stack(
            children: <Widget>[
              Column(
                children: <Widget>[
                  Text("One", style: TextStyle(fontSize: 48.0),),
                  Text("Two", style: TextStyle(fontSize: 48.0),),
                ],
              ),
              Align(
                child: FlutterLogo(size: 48,),
                alignment: Alignment.bottomCenter,
              )
            ],
          ),
        ),

【讨论】:

  • 刚刚试过这个..这出于某种原因将子元素对齐在页面的顶部中心而不是底部!
猜你喜欢
  • 2021-08-11
  • 2023-01-09
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
相关资源
最近更新 更多