简体   繁体   English

如何在 Flutter 中使用 ViewPager 创建 GridView

[英]how create GridView with ViewPager in flutter

I am trying to create the grid view inside the page view, what I want is, In grid view, I want to display only 6 items and other items I want go to another page of the page view, what I mean is I only want 6 items on each page of the page view,我正在尝试在页面视图中创建网格视图,我想要的是,在网格视图中,我只想显示 6 个项目,而其他项目我想转到页面视图的另一个页面,我的意思是我只想要页面视图的每个页面上的 6 个项目,

To clarify more, if I have 6 items, the page view will create only one page in which 6 items will appear from the gridview, or if I have 12 items, the page view will create two pages and each page will display 6 items from the gridview, or if I have 16 items, the page view will create three Pages per page 6 items, etc.,为了澄清更多,如果我有 6 个项目,页面视图将只创建一个页面,其中 6 个项目将从 gridview 中显示,或者如果我有 12 个项目,页面视图将创建两个页面,每个页面将显示 6 个项目gridview,或者如果我有 16 个项目,则页面视图将创建每页 6 个项目的三个页面,等等,

This is the code that I tried to do, but it did not work.这是我试图做的代码,但它不起作用。 Can someone help me with it?有人可以帮我吗? Thank you谢谢


 Expanded(
            child: Container(
              decoration: const BoxDecoration(
                color: Color(0xFFE6E6E6),
                image: DecorationImage(
                  image: AssetImage('assets/images/background_image.png'),
                  fit: BoxFit.fill,
                ),
              ),
              child: PageView.builder(
                controller: _controller,
                physics: const BouncingScrollPhysics(),
                itemCount: items.length % 6,
                itemBuilder: (context, index) => GridView.builder(
                  padding:
                      EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.h),
                  physics: const ClampingScrollPhysics(),
                  shrinkWrap: true,
                  scrollDirection: Axis.vertical,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    crossAxisSpacing: 20.w,
                    mainAxisSpacing: 20.h,
                    childAspectRatio: 4 / 1.5,
                  ),
                  itemCount: items.length,
                  primary: false,
                  itemBuilder: (context, index) => Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(15.r),
                    ),
                    child: Row(
                      children: [
                        Container(
                          width: 140.w,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(15.r),
                            image: DecorationImage(
                              image: NetworkImage(
                                items[index].photoUrl,
                              ),
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                        Expanded(
                          child: Padding(
                            padding: EdgeInsets.symmetric(
                              horizontal: 16.w,
                              vertical: 18.h,
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  items[index].categoryName,
                                  style: TextStyle(
                                    fontSize: 10.sp,
                                    color: const Color(0xFFBCBCBC),
                                  ),
                                ),
                                Text(
                                  items[index].itemName,
                                  style: TextStyle(
                                    fontSize: 12.sp,
                                    color: const Color(0xFFFC4747),
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                                const Spacer(),
                                Text(
                                  '${items[index].price}د.ع',
                                  style: TextStyle(
                                    color: const Color(0xFF40484E),
                                    fontSize: 12.sp,
                                  ),
                                ),
                                Row(
                                  children: [
                                    Icon(
                                      CupertinoIcons.time,
                                      size: 8.sp,
                                      color: const Color(0xFFBCBCBC),
                                    ),
                                    Text(
                                      '${items[index].time}m',
                                      style: TextStyle(
                                        fontSize: 8.sp,
                                        color: const Color(0xFFBCBCBC),
                                      ),
                                    ),
                                  ],
                                )
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),


A picture showing the shape of the elements显示元素形状的图片

I modified your code the way you wanted Here is the solution : My modified code will divide your main list into to small lists with 6 items.我按照您想要的方式修改了您的代码这是解决方案:我修改后的代码会将您的主列表分成包含 6 个项目的小列表。 Here I take main list length is 16 so it's divide into 6 : 6 : 4 items lists.在这里,我将主列表长度设为 16,因此它分为 6:6:4 项列表。

Output :输出 : 输出 Gif

Code :代码 :

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<int> items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
  var gridViewLists = [];
  int listBreakerSize = 6;
  @override
  void initState() {
    for (var i = 0; i < items.length; i += listBreakerSize) {
      gridViewLists.add(items.sublist(
          i, i + listBreakerSize > items.length ? items.length : i + listBreakerSize));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: Container(
            margin: EdgeInsets.only(top: 40),
            decoration: const BoxDecoration(
              color: Color(0xFFE6E6E6),
            ),
            child: PageView.builder(
              // controller: _controller,
              physics: const BouncingScrollPhysics(),
              itemCount: (items.length / 6).ceil(),
              itemBuilder: (context, i) => GridView.builder(
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
                physics: const ClampingScrollPhysics(),
                shrinkWrap: true,
                scrollDirection: Axis.vertical,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  crossAxisSpacing: 20,
                  mainAxisSpacing: 20,
                  childAspectRatio: 4 / 1.5,
                ),
                itemCount: gridViewLists[i].length,
                primary: false,
                itemBuilder: (context, index) => Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(15),
                  ),
                  child: Row(
                    children: [
                      Container(
                        width: 116,
                        decoration: BoxDecoration(
                          color: Colors.green,
                          borderRadius: BorderRadius.circular(15),
                        ),
                      ),
                      Expanded(
                        child: Padding(
                          padding: EdgeInsets.symmetric(
                            horizontal: 16,
                            vertical: 18,
                          ),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                //items[index].categoryName,
                                'categoryName',
                                style: TextStyle(
                                  fontSize: 10,
                                  color: const Color(0xFFBCBCBC),
                                ),
                              ),
                              Text(
                                //items[index].itemName,
                                'itemName',
                                style: TextStyle(
                                  fontSize: 12,
                                  color: const Color(0xFFFC4747),
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                              const Spacer(),
                              Text(
                                // '${items[index].price}د.ع',
                                'price',
                                style: TextStyle(
                                  color: const Color(0xFF40484E),
                                  fontSize: 12,
                                ),
                              ),
                              Row(
                                children: [
                                  Icon(
                                    CupertinoIcons.time,
                                    size: 8,
                                    color: const Color(0xFFBCBCBC),
                                  ),
                                  Text(
                                    //'${items[index].time}m',
                                    'time',
                                    style: TextStyle(
                                      fontSize: 8,
                                      color: const Color(0xFFBCBCBC),
                                    ),
                                  ),
                                ],
                              )
                            ],
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

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

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