简体   繁体   English

如何让我的 gridView 滚动占据 Flutter 中的整个页面?

[英]How do I make my gridView scroll take up the whole page in Flutter?

I have a gridView which on scrolling must take up the whole page.我有一个 gridView,它在滚动时必须占据整个页面。 It currently only scrolls in the bottom half of the page and looks like shown below.它目前仅在页面的下半部分滚动,如下所示。

When I scroll the Grid View containing the elements only the bottom part of the page is scrolling当我滚动包含元素的网格视图时,只有页面的底部在滚动

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Container(
        color: DesignCourseAppTheme.nearlyWhite,
        child: PageView(
          scrollDirection: Axis.vertical,
          children: [
            Scaffold(
              backgroundColor: DesignCourseAppTheme.nearlyWhite,
              body: Container(
                child: Column(
                  children: <Widget>[
                    SizedBox(
                      height: MediaQuery.of(context).padding.top,
                    ),
                    getAppBarUI(),
                    Expanded(
                      child: Container(
                        height: MediaQuery.of(context).size.height,
                        child: Column(
                          children: <Widget>[
                            getCategoryUI(),
                            Flexible(
                              child: getPopularCourseUI(),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

Here the gridView is called as:这里的 gridView 被称为:

  Widget getPopularCourseUI() {
    return Padding(
      padding: const EdgeInsets.only(top: 8.0, left: 18, right: 16),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            'Featured Games',
            textAlign: TextAlign.left,
            style: TextStyle(
              fontFamily: "Netflix",
              fontWeight: FontWeight.w800,
              fontSize: 24,
              letterSpacing: 0.27,
              color: HexColor('FF8C3B'),
            ),
          ),
          Flexible(
            child: GamesGridView(
              callBack: () {},
            ),
          )
        ],
      ),
    );
  }

Thank you for your help!感谢您的帮助!

  1. You can wrap your widget which is inside Scaffold body with ListView .您可以使用ListView包装位于Scaffold body内的小部件。

  2. Then you should remove all flex widgets from your Column .然后你应该从你的Column删除所有 flex 小部件。

  3. Your GridView should include你的GridView应该包括

shrinkWrap: true收缩包装:真
physics: const ClampingScrollPhysics()物理:const ClampingScrollPhysics()

Refer this,参考这个,

import "package:flutter/material.dart";

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

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  Widget build(BuildContext context) {
    return Material(
      color: Colors.white, //DesignCourseAppTheme.nearlyWhite,
      child: PageView(
        scrollDirection: Axis.vertical,
        children: [
          Scaffold(
            body: SafeArea(
              child: ListView(
                padding: EdgeInsets.symmetric(horizontal: 30),
                children: <Widget>[
                  getAppBarUI(),
                  getCategoryUI(),
                  getPopularCourseUI(),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget getCategoryUI(){
    return SizedBox(
      height: 300,
      child: PageView(
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 40.0,horizontal: 30.0),
            child: Material(
              color: Colors.blue,
              elevation: 3.0,
              borderRadius: BorderRadius.circular(20.0),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 40.0,horizontal: 30.0),
            child: Material(
              color: Colors.green,
              elevation: 3.0,
              borderRadius: BorderRadius.circular(20.0),
            ),
          ),
        ],
      ),
    );
  }

  Widget getAppBarUI(){
    return Text(
      'Games for Fun!',
      style: TextStyle(
        fontFamily: "Netflix",
        fontWeight: FontWeight.w800,
        fontSize: 32.0,
        letterSpacing: 0.27,
        color: Colors.red, //HexColor('FF8C3B'),
      ),
    );
  }

  Widget getPopularCourseUI() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text(
          'Featured Games',
          style: TextStyle(
            fontFamily: "Netflix",
            fontWeight: FontWeight.w800,
            fontSize: 24.0,
            letterSpacing: 0.27,
            color: Colors.red, //HexColor('FF8C3B'),
          ),
        ),
        const SizedBox(height: 8.0),
        GamesGridView(
          callBack: () {},
        )
      ],
    );
  }
}

class GamesGridView extends StatelessWidget {
  final VoidCallback callBack;

  const GamesGridView({Key key, this.callBack}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GridView.count(
      shrinkWrap: true, //TODO: must be included
      physics: const ClampingScrollPhysics(), //TODO: must be included
      crossAxisCount: 2,
      mainAxisSpacing: 50.0,
      crossAxisSpacing: 50.0,
      children: <Widget>[
        RaisedButton(child: Text("Button"), onPressed: () {}),
        RaisedButton(child: Text("Button"), onPressed: () {}),
        RaisedButton(child: Text("Button"), onPressed: () {}),
        RaisedButton(child: Text("Button"), onPressed: () {}),
        RaisedButton(child: Text("Button"), onPressed: () {}),
        RaisedButton(child: Text("Button"), onPressed: () {}),
        RaisedButton(child: Text("Button"), onPressed: () {}),
      ],
    );
  }
}

Here getCategoryUI can scroll horizontally too.这里getCategoryUI也可以水平滚动。

If I understood your issue correctly, using a CustomScrollView with SliverAppBar and a SliverGrid should do what you want:如果我正确理解您的问题,使用带有 SliverAppBar 和 SliverGrid 的 CustomScrollView 应该可以满足您的需求:

class GridViewIssue extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: <Widget>[
        SliverAppBar(
          title: Center(child: Text('Banner')),
          expandedHeight: 250.0,
        ),
        SliverGrid(
          delegate: SliverChildBuilderDelegate(
            (BuildContext context, int index) {
              return Container(
                alignment: Alignment.center,
                color: Colors.teal[100 * (index % 9)],
                child: Text('grid item $index'),
              );
            },
            childCount: 8
          ),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2
          )
        )
      ],
    );
  }
}

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

相关问题 如何将 listView 转换为 GridView 颤振? - How do I convert a listView to a GridView flutter? 我如何在 flutter 中去除我的文本字段小部件的额外填充? - How do i take off extra padding off my text field widget in flutter? Flutter:不小心在 360x780 上创建了我的整个 UI。 如何使其在其他手机屏幕上均匀调整大小? - Flutter: Accidentaly created my whole UI on 360x780. How can I make it to be evenly resized on other phone screens? 我如何让文本段落占据 flutter 中页面的 100% - How I can let text paragraph take 100% of the page in flutter Shinydashboard:我的盒子不想占据整个专栏 - shinydashboard: my boxes don't want to take up the whole column 如何使此滚动布局起作用? - How do I make this scroll layout work? 如何在我的 tk UI 中创建一个按钮,使程序在按下时执行大量操作而不冻结 window? - How do I make a button in my tk UI that makes the program do a whole ton of stuff when it is pressed not freeze the window? 随着聊天的进行,如何使聊天程序向下滚动? - How do I make my chat program scroll down as the chat moves along? Flutter这个UI设计怎么做 - How do I make this UI design in Flutter 如何为 Flutter 中的所有设备制作网格视图? - How to make a gridview for all devices in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM