颤振安全设置约束

时间:2020-10-27 13:14:38

标签: flutter flutter-layout

我想在ListView中添加Column,以修正错误

在performResize()期间引发了以下断言:水平视口的高度不受限制。视口在横轴上扩展以填充其容器,并约束其子代以使其在横轴上的范围匹配。在这种情况下,水平视口将获得无限量的垂直空间用于扩展。

我必须在水平列表中添加高度限制。例如。高度包装在容器中

如何安全设置height

我当前的方法是: 1-设置较小的高度2-看到抖动的黄色/黑色条纹3-将OVERFLOWED BY值添加到步骤1

但是我看到我的方法在某些设备中有效,而在某些其他设备中无效,如果我将其设置为较高的高度,则具有难看的空白空间;如果我将其设置为较小的高度,则将其裁切为视图,如何设置精确的高度?< / p>

在下面的代码中,我添加了BoxConstraints(maxHeight: 108),如果将其设置为107,则有1个像素溢出,以下代码在某些设备中是可以的,但在其他设备中,其臀部会从底部变小

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Column(
        children: [
          Text('Part A'),
          MyList(),
          SizedBox(height: 50),
          Text('Part B'),
          MyList(),
          SizedBox(height: 50),
          Text('Part C'),
          MyList(),
        ],
      ),
    );
  }
}

class MyList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: BoxConstraints(maxHeight: 108),
      child: ListView(
        scrollDirection: Axis.horizontal,
        children: List<Widget>.generate(50, (index) {
          return Padding(
            padding: const EdgeInsets.all(5),
            child: Column(
              children: [
                Container(
                  height: 50,
                  width: 200,
                  color: Colors.amber[600],
                ),
                RaisedButton(
                  child: Text('test'),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(18.0),
                      side: BorderSide(color: Colors.red)),
                )
              ],
            ),
          );
        }),
      ),
    );
  }
}

0 个答案:

没有答案