dart - 即使展开时,Listview 中的 GridView 也会导致 "Vertical viewport was given unbounded height"

标签 dart flutter

我还是 Flutter 的新手,正在尝试创建以下布局:

eLayout Of What I'm trying to achieve

我将布局分为 3 个主要部分:

  1. 标题小部件(带有“Title Here”的小部件)
  2. 信息文本小部件(带有图标和“Info Goes Here”的小部件 文字)
  3. 包含按钮的 GridView <--- 这是导致问题的原因

这是我的 Main.dart:

import 'package:flutter/material.dart';
import 'package:ifp_poc/Screens/Home.dart';
import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;

void main() {
//  debugPaintSizeEnabled = true;
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: new ThemeData(
        brightness: Brightness.light,
        primaryColor: Colors.blue,
        primaryColorLight: Colors.white,
        primaryColorDark: Colors.black,
        platform: Theme.of(context).platform,
      ),
      title: "POC",
      home: new Home(),
    );
  }
}

这是我的家庭类(class):

import 'package:flutter/material.dart';
import 'package:ifp_poc/Components/ifp/listBoxHQ.dart';
import 'package:ifp_poc/Components/ifp/infoHQ.dart';
import 'package:ifp_poc/Components/ifp/titleHQ.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      body: new Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          new TitleHQ(
            logoAsset: "assets/images/HQ-LOGO.png",
            title: "Hospitality Qatar",
            subTitle: "06-08 Nov 2018",
            info: "3:00 PM - 9:00 PM",
            url: "http://www.ifpexpo.com",
          ),
          new Container(
            padding: EdgeInsets.only(top: 15.0, left: 26.0, right: 26.0),
            child: new Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                new InfoHQ(text: "Qatar's Premiere International Hospitlaity & Horeca Trade Show",),
                new Divider(height: 20.0, color: Theme.of(context).primaryColorDark,),
                new InfoHQ(text: "Doha Exhibition & Convention Centre (DECC)",icon: Icons.location_on,),
                new Divider(height: 20.0, color: Theme.of(context).primaryColorDark,),
                new ListBoxHQ()
              ],
            ),
          )

        ],
      ),
    );
  }
}

这是我的 listBoxHQ() 类:

import 'package:flutter/material.dart';
import 'package:ifp_poc/Components/ifp/boxHQ.dart';

class ListBoxHQ extends StatefulWidget {
  @override
  _ListBoxHQState createState() => _ListBoxHQState();
}

class _ListBoxHQState extends State<ListBoxHQ> {
  @override
  Widget build(BuildContext context) {

    return GridView.count(
          primary: false,
          crossAxisSpacing: 5.0,
          crossAxisCount: 3,
          children: <Widget>[
            BoxHQ(
              height: 100.0,
              text: "Overview",
              icon: Icons.ac_unit,
            ),
            BoxHQ(
              height: 100.0,
              text: "Overview",
              icon: Icons.ac_unit,
            ),
            BoxHQ(
              height: 100.0,
              text: "Overview",
              icon: Icons.ac_unit,
            ),
          ]
      );
  }
}

最后是我的 BoxHQ 类(class):

import 'package:flutter/material.dart';

class BoxHQ extends StatefulWidget {
  final double height;
  final IconData icon;
  final String text;

  BoxHQ({Key key, @required this.height, @required this.icon, @required this.text}): super(key: key);

  @override
  _BoxHQState createState() => _BoxHQState();
}

class _BoxHQState extends State<BoxHQ> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height,
      width: double.infinity,
      padding: EdgeInsets.all(5.0),
      margin: EdgeInsets.all(7.0),
      decoration:  new BoxDecoration(
        borderRadius: new BorderRadius.all(const Radius.circular(10.0)),
        color: Colors.red,
      ),
      child: Column( // Replace with a Row for horizontal icon + text
        children: <Widget>[
          Icon(widget.icon == null? Icons.info : widget.icon, color: Theme.of(context).primaryColorLight, size: 50.0,),
          new Padding(padding: EdgeInsets.symmetric(vertical: 5.0)),
          Text(widget.text, style: new TextStyle(color: Theme.of(context).primaryColorLight, fontSize: 20.0),),
        ],
      ),
    );
  }
}

问题 (TLDR):

运行上述代码时,出现以下错误:

I/Choreographer( 6979): Skipped 35 frames!  The application may be doing too much work on its main thread.
D/EGL_emulation( 6979): eglMakeCurrent: 0xa6cac080: ver 2 0 (tinfo 0x970aa0c0)
I/flutter ( 6979): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 6979): The following assertion was thrown during performResize():
I/flutter ( 6979): Vertical viewport was given unbounded height.
I/flutter ( 6979): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 6979): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 6979): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter ( 6979): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter ( 6979): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter ( 6979): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter ( 6979): the height of the viewport to the sum of the heights of its children.

我尝试过但没有成功的方法:

  • 从扩展的 listBoxHQ 类中添加 GridView.count
  • 在 Expanded 中从 Home 添加 Container 的每个项目
  • 在 Expanded 中添加 Home 主列的每个子项

只有当我用 SizedBox 和固定高度包装 listBoxHQ 的 GridView.count 时,它才有效。 但我不能这样做,因为我的按钮是动态的,而且我不知道我可能有多少个按钮。

非常感谢您阅读所有内容,如果您能与我分享您的知识并帮助我解决此问题,我将不胜感激。

非常感谢,真的!

最佳答案

您应该在 GridView 中添加这行代码:

shrinkWrap: true

所以你的 GridView 应该是这样的:

return GridView.count(
  shrinkWrap: true,
  crossAxisCount: 3,
  children: <Widget>[
    BoxHQ(
      height: 100.0,
      text: "Overview",
      icon: Icons.ac_unit,
    ),
    BoxHQ(
      height: 100.0,
      text: "Overview",
      icon: Icons.ac_unit,
    ),
    BoxHQ(
      height: 100.0,
      text: "Overview",
      icon: Icons.ac_unit,
    ),
  ]
);

该值强制 GridView 占用与其子项一样多的空间。默认情况下,GridView 想要填充所有可用空间,以便在子项不能一次全部容纳时提供滚动。在您的场景中, GridView 想要获取所有可用高度,但其父级 ListView 没有指定可用高度。这是因为 ListView 本身想要滚动不适合的 child ,因此他们可以随心所欲。

关于dart - 即使展开时,Listview 中的 GridView 也会导致 "Vertical viewport was given unbounded height",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52934629/

相关文章:

dart - Dart lang中的DOM更改监听器

android - 如何在 flutter 中隐藏android的底部导航栏

FlutterMap LateInitialization错误状态管理

flutter - 按钮onPressed不会运行该功能

flutter - 如何获得 Dart flutter 上的映射?

dart - TabBar中的TabBarView占用剩余空间

firebase - 为什么此快照两次将每个文档获取两次? flutter

firebase - Flutter Firestore 检索随机 Firestore 文档

android-studio - Flutter Android Studio使用所有内存

flutter - 要在 Flutter 中列出的复杂字符串