首页 > 解决方案 > SizedBox 在 listview-Flutter 中不起作用

问题描述

 Scaffold(
  appBar: AppBar(
    leading: Icon(Icons.arrow_back),
    centerTitle: true,
    title: Text("Select Time Slot"),
  ),
  body: ListView(
    children: <Widget>[
      SizedBox(
        width: 150,
        child: Card(
          child: Text(
            "11 AM - 1 PM",
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
        ),
      )
    ],
  ),
);

我希望该卡占用 150 宽,但它占用了完整的可用宽度。

在此处输入图像描述

我应该怎么做才能得到预期的结果?

标签: flutterdart

解决方案


如果您想以 150 宽度居中卡片,请使用中心小部件将其包裹起来。center 小部件会将卡片宽度设置为 150,并将通过将卡片居中到中间来填充剩余宽度。

      Center(child: 
      SizedBox(
        width: 150,
        child: Card(
          child: Text(
            "11 AM - 1 PM",
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
        ),
      ))
    ],

否则,您可以使用对齐小部件根据您的喜好对齐卡片。


推荐阅读