【问题标题】:Add a GridView to the end of listview将 GridView 添加到列表视图的末尾
【发布时间】:2021-06-05 04:43:43
【问题描述】:

我是 Flutter 的新手,我正在尝试将 GridView 添加到我的列表视图的末尾,我的目标是在屏幕末尾找到 gridview,但我有错误:

 The relevant error-causing widget was: 
  GridView file:///C:/Users/Fares/AndroidStudioProjects/tl_fantasy/lib/tl/tl.dart:211:33
When the exception was thrown, this was the stack: 
#0      RenderViewport.performResize.<anonymous closure> (package:flutter/src/rendering/viewport.dart:1356:15)
#1      RenderViewport.performResize (package:flutter/src/rendering/viewport.dart:1417:6)
#2      RenderObject.layout (package:flutter/src/rendering/object.dart:1756:9)
#3      RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:113:14)
#4      RenderObject.layout (package:flutter/src/rendering/object.dart:1777:7)

只有当我在文件末尾添加 gridview 的容器时才会出现此错误

英雄我的代码:

import 'package:flutter/material.dart';

class TL extends StatelessWidget{
  List<String> images = [
    "https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png",
    "https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png",
    "https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png",
    "https://static.javatpoint.com/tutorial/flutter/images/flutter-logo.png"
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                colors: [Colors.purple, Colors.blue])
        ),
        child: ListView(
          children: [
            SizedBox(
              height: 20,
            ),
            Container(
              margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
              child:  Text(
                "Tunisian League",
                style: TextStyle(fontSize: 25, fontWeight: FontWeight.w900, color: Colors.white),
              ),
            ),

            SizedBox(
              height: 30,
            ),
            Container(
              margin: EdgeInsets.fromLTRB(15, 0, 15, 0),
              child: Column(
                children: [
                  ButtonTheme(
                    minWidth: double.infinity,
                    child: RaisedButton(
                        child: Align(
                          child: Text("Click Here", style: TextStyle(fontSize: 17),textAlign: TextAlign.right, ),
                          alignment: Alignment.centerLeft,
                        ),
                        onPressed: (){},
                        color: Colors.grey.shade300,
                        textColor: Colors.black,
                        padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
                        splashColor: Colors.blue,
                        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                    ),
                  ),
                  SizedBox(
                    height: 1,
                  ),
                  ButtonTheme(
                    minWidth: double.infinity,
                    child: RaisedButton(
                        child: Align(
                          child: Text("Click Here", style: TextStyle(fontSize: 17),textAlign: TextAlign.right, ),
                          alignment: Alignment.centerLeft,
                        ),
                        onPressed: (){},
                        color: Colors.grey.shade300,
                        textColor: Colors.black,
                        padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
                        splashColor: Colors.blue,
                        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                    ),
                  ),
                  SizedBox(
                    height: 1,
                  ),
                  ButtonTheme(
                    minWidth: double.infinity,
                    child: RaisedButton(
                        child: Align(
                          child: Text("Click Here", style: TextStyle(fontSize: 17),textAlign: TextAlign.right, ),
                          alignment: Alignment.centerLeft,
                        ),
                        onPressed: (){},
                        color: Colors.grey.shade300,
                        textColor: Colors.black,
                        padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
                        splashColor: Colors.blue,
                        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  ButtonTheme(
                    minWidth: double.infinity,
                    child: RaisedButton(
                        child: Align(
                          child: Text("Click Here", style: TextStyle(fontSize: 17),textAlign: TextAlign.right, ),
                          alignment: Alignment.centerLeft,
                        ),
                        onPressed: (){},
                        color: Colors.grey.shade300,
                        textColor: Colors.black,
                        padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
                        splashColor: Colors.blue,
                        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                    ),
                  ),
                  SizedBox(
                    height: 1,
                  ),
                  ButtonTheme(
                    minWidth: double.infinity,
                    child: RaisedButton(
                        child: Align(
                          child: Text("Click Here", style: TextStyle(fontSize: 17),textAlign: TextAlign.right, ),
                          alignment: Alignment.centerLeft,
                        ),
                        onPressed: (){},
                        color: Colors.grey.shade300,
                        textColor: Colors.black,
                        padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
                        splashColor: Colors.blue,
                        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                    ),
                  ),
                  SizedBox(
                    height: 1,
                  ),
                  ButtonTheme(
                    minWidth: double.infinity,
                    child: RaisedButton(
                        child: Align(
                          child: Text("Click Here", style: TextStyle(fontSize: 17),textAlign: TextAlign.right, ),
                          alignment: Alignment.centerLeft,
                        ),
                        onPressed: (){},
                        color: Colors.grey.shade300,
                        textColor: Colors.black,
                        padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
                        splashColor: Colors.blue,
                        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  ButtonTheme(
                    minWidth: double.infinity,
                    child: RaisedButton(
                        child: Align(
                          child: Text("Click Here", style: TextStyle(fontSize: 17),textAlign: TextAlign.right, ),
                          alignment: Alignment.centerLeft,
                        ),
                        onPressed: (){},
                        color: Colors.grey.shade300,
                        textColor: Colors.black,
                        padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
                        splashColor: Colors.blue,
                        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                    ),
                  ),
                  SizedBox(
                    height: 1,
                  ),
                  ButtonTheme(
                    minWidth: double.infinity,
                    child: RaisedButton(
                        child: Align(
                          child: Text("Click Here", style: TextStyle(fontSize: 17),textAlign: TextAlign.right, ),
                          alignment: Alignment.centerLeft,
                        ),
                        onPressed: (){},
                        color: Colors.grey.shade300,
                        textColor: Colors.black,
                        padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
                        splashColor: Colors.blue,
                        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                    ),
                  ),
                  SizedBox(
                    height: 1,
                  ),
                  ButtonTheme(
                    minWidth: double.infinity,
                    child: RaisedButton(
                        child: Align(
                          child: Text("Click Here", style: TextStyle(fontSize: 17),textAlign: TextAlign.right, ),
                          alignment: Alignment.centerLeft,

                        ),

                        onPressed: (){},
                        color: Colors.grey.shade300,
                        textColor: Colors.black,
                        padding: EdgeInsets.fromLTRB(15, 8, 8, 8),
                        splashColor: Colors.blue,
                        shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                    ),
                  ),
                  SizedBox(
                    height: 30,
                  )

                ],
              ),
            ),
            Container(
                padding: EdgeInsets.all(12.0),
                child: GridView.builder(
                  itemCount: images.length,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      crossAxisSpacing: 4.0,
                      mainAxisSpacing: 4.0
                  ),
                  itemBuilder: (BuildContext context, int index){
                    return Image.network(images[index]);
                  },
                )
            )

          ],
        ),
      )
    );
  }

}

我希望我能找到解决这个问题的方法

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    将“shrinkWrap”属性添加到 GridView。

    Container(
                padding: EdgeInsets.all(12.0),
                child: GridView.builder(
                  shrinkWrap: true,
                  itemCount: images.length,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      crossAxisSpacing: 4.0,
                      mainAxisSpacing: 4.0
                  ),
                  itemBuilder: (BuildContext context, int index){
                    return Image.network(images[index]);
                  },
                )
            )
    

    有关为什么需要 'shrinkWrap' 属性的更多详细信息,您可以参考:What does the shrink wrap property do in flutter?

    【讨论】:

      【解决方案2】:

      您所要做的就是将 shrinkWrap 添加到您的 Gridview 中,如下所示:

      Container(
                    padding: EdgeInsets.all(12.0),
                    child: GridView.builder(
                      shrinkWrap: true,
                      itemCount: images.length,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,
                          crossAxisSpacing: 4.0,
                          mainAxisSpacing: 4.0
                      ),
                      itemBuilder: (BuildContext context, int index) {
                        return Image.network(images[index]);;
                      },
                    )
                ) 
      

      【讨论】:

        猜你喜欢
        • 2017-01-28
        • 1970-01-01
        • 1970-01-01
        • 2018-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-30
        相关资源
        最近更新 更多