【问题标题】:How to position a Widget in a Stack, but without matching parent's width/height?如何在堆栈中定位小部件,但不匹配父级的宽度/高度?
【发布时间】:2019-06-23 20:27:58
【问题描述】:

我想在 Stack 的顶部中心放置一个小部件,但它被拉伸到父级的宽度,如下面的屏幕截图:

这是我正在使用的代码:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      key: key,
      appBar: AppBar(
        title: Text("Just a test app"),
      ),
      body: Stack(
        children: <Widget>[
          Positioned(
            left: 0,
            right: 0,
            child: Card(
              child: Text("A variable sized text"),
            ),
          ),
        ],
      ),
    );
  }

我想要实现的是这个(但在堆栈内):

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    将您的Card 包装成AlignCenter

     Stack(
        children: <Widget>[
          Positioned( 
            top: 50,
            left: 0,
            right: 0,
            child: Center(
              child: Card(
                child: Text("A variable sized text"),
              ),
            ),
          ),
          Align(
            alignment: Alignment.topCenter, //that also works
            child: Card(
              child: Text("Another variable sized text"),
            ),
          )
        ],
      ),   
    

    【讨论】:

    • Positioned 毫无意义。我们可以使用Align 结合Alignment.topCenter
    猜你喜欢
    • 2019-12-27
    • 1970-01-01
    • 2020-12-03
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多