简体   繁体   English

Flutter:具有不同高度约束的水平 ListView 视图

[英]Flutter: Horizontal ListView view with different height contraint

I have a horizontal list bar with fixed height 100 (used inside Column).我有一个固定高度为 100 的水平列表栏(在 Column 内使用)。 Height 100 is good for iPhone8.高度 100 适合 iPhone8。 But the pixel overflows in iPhone-X due to limited 100 height.但由于 100 高度限制,iPhone-X 中的像素溢出。 If I make it 140, both are good.如果我做到140,两者都很好。 But the bar looks no good in iphone8.但是在 iphone8 中该栏看起来并不好。 So I want 120 height in one device and 140 in other device.所以我想要一台设备的高度为 120,而另一台设备的高度为 140。 I thought I could achieve it using ConstrainedBox like below:我想我可以使用 ConstrainedBox 来实现它,如下所示:

ConstrainedBox(constraints: BoxConstraints(minHeight: 100, maxHeight: 140), child: ListView(...))

But listview goes with 140 height all the time.但是 listview 一直以 140 高度运行。 So How to achive dynamic height contraint for different device?那么如何实现不同设备的动态高度约束呢?

Sample widget:示例小部件:

class MyHorizontalBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 100, // 140
      child: ListView(
        scrollDirection: Axis.horizontal,
        children: [
          _myCard(),
          _myCard(),
          _myCard(),
        ],
      ),
    );
  }

  Widget _myCard(){
    return Container(
      width: 115,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('Top', style: TextStyle(fontSize: 12)),
          Text('Middle', style: TextStyle(fontSize: 25)),
          Text('Bottom', style: TextStyle(fontSize: 18)),
        ],
      ),
    );
  }
}

Try using MediaQuery.of(context).size.height or width in place of constant height and width.尝试使用 MediaQuery.of(context).size.height 或 width 代替恒定的高度和宽度。

You can use LayoutBuilder widget.您可以使用LayoutBuilder小部件。 But such problems are usually solved with a flexible layout .但是这些问题通常可以通过灵活的布局来解决。

eg defining your horizontal bar height as 1/3 of available height.例如,将您的水平条高度定义为可用高度的 1/3。

Use MediaQuery.of(context)使用 MediaQuery.of(context)

try the following code.试试下面的代码。

class MyHorizontalBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    double magicNumber = 5; // Try playing with this number to get the appearence. 
    return SizedBox(
      height: MediaQuery.of(context).size.height / magicNumber, 
      child: ListView(
        scrollDirection: Axis.horizontal,
        children: [
          _myCard(),
          _myCard(),
          _myCard(),
        ],
      ),
    );
  }

  Widget _myCard() {
    return Container(
      width: 115,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
              Text('Top', style: TextStyle(fontSize: 12)),
              Text('Middle', style: TextStyle(fontSize: 25)),
              Text('Bottom', style: TextStyle(fontSize: 18)),
            ],
          ),
        );
      }
    }

Try playing with the variable magicNumber.尝试使用变量 magicNumber。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM