简体   繁体   English

如何在 Flutter 中制作具有两行的水平 ListView?

[英]How to make a horizontal ListView with two row in Flutter?

i am trying to show the catalog list in two row within horizontal GridView or ListView我正在尝试在水平 GridView 或 ListView 内的两行中显示目录列表
instead of single row the list data comes from server The horizontal ListView working fine with one row How can I achieve this as the GIF bellow ?而不是单行列表数据来自服务器水平ListView与一行工作正常我怎样才能实现这一点作为GIF波纹管?

bellow image what i have波纹管图像我有什么

bellow gif what i need下面 gif 我需要什么

在此处输入图像描述 [ [ListView 根据需要1

below full code The horizontal ListView which is working fine with one row and i need to convert it to GridView so it can show two row and horizontal scrollable下面的完整代码水平 ListView 与一行工作正常,我需要将其转换为 GridView 以便它可以显示两行和水平滚动

_catList() {
return Container(
  height: 150,
  child: ListView.builder(
    itemCount: catList.length < 10 ? catList.length : 10,
    scrollDirection: Axis.horizontal,
    shrinkWrap: true,
    physics: AlwaysScrollableScrollPhysics(),
    itemBuilder: (context, index) {
      return Padding(
        padding: const EdgeInsetsDirectional.only(end: 10),
        child: GestureDetector(
          onTap: () async {
            if (catList[index].subList == null ||
                catList[index].subList.length == 0) {
              await Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => ProductList(
                        name: catList[index].name,
                        id: catList[index].id,
                        tag: false,
                        updateHome: widget.updateHome),
                  ));
              if (mounted) setState(() {});
            } else {
              await Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => SubCat(
                        title: catList[index].name,
                        subList: catList[index].subList,
                        updateHome: widget.updateHome),
                  ));
              if (mounted) setState(() {});
            }
          },
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Padding(
                padding: const EdgeInsetsDirectional.only(bottom: 5.0),
                child: new ClipRRect(
                  borderRadius: BorderRadius.circular(25.0),
                  child: new FadeInImage(
                    fadeInDuration: Duration(milliseconds: 50),
                    image: NetworkImage(
                      catList[index].image,
                    ),
                    height: 100.0,
                    width: 100.0,
                    fit: BoxFit.cover,
                    imageErrorBuilder: (context, error, stackTrace) =>
                        erroWidget(100),

                    //  errorWidget: (context, url, e) => placeHolder(50),
                    placeholder: placeHolder(100),
                  ),
                ),
              ),
              Container(
                child: Text(
                  catList[index].name,
                  style: Theme.of(context).textTheme.caption.copyWith(
                      color: colors.fontColor,
                      fontWeight: FontWeight.w600,
                      fontSize: 15),
                  overflow: TextOverflow.ellipsis,
                  textAlign: TextAlign.center,
                ),
                width: 100,
              ),
            ],
          ),
        ),
      );
    },
  ),
);

} }

You can use grid view to archive this thing.你可以使用网格视图来存档这个东西。 Here is a snippet of the configuration.这是配置的片段。

GridView.count(
                  shrinkWrap: true,
                  crossAxisCount: 2,
                  physics: const ClampingScrollPhysics(),
                  scrollDirection: Axis.horizontal,
                  crossAxisSpacing: 10,
                  mainAxisSpacing: 20,
                  children: ...)

Here crossAxisCount: 2,scrollDirection: Axis.horizontal is what you need.这里crossAxisCount: 2,scrollDirection: Axis.horizontal就是你需要的。

You can either use ListView.builder with itemCount: catList.length/2 and return a Column with two items (index and index+1)您可以将ListView.builderitemCount: catList.length/2一起使用并返回一个包含两个项目(索引和索引 + 1)的Column

or use GridView.count :或使用GridView.count

Container(
  height: 150,
  child: GridView.count(
    shrinkWrap: true,
    scrollDirection: Axis.horizontal,
    crossAxisCount: 2,
    children: catList.isEmpty ? [Container()] : catList.map((cat) {
      return Padding(
        padding: const EdgeInsetsDirectional.only(end: 10),
         child: GestureDetector(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Padding(
                padding: const EdgeInsetsDirectional.only(bottom: 5.0),
                child: new ClipRRect(
                  borderRadius: BorderRadius.circular(25.0),
                  child: new FadeInImage(
                    fadeInDuration: Duration(milliseconds: 50),
                    image: NetworkImage(
                      cat.image,
                    ),
                    height: 100.0,
                    width: 100.0,
                    fit: BoxFit.cover,
                    imageErrorBuilder: (context, error, stackTrace) =>
                        erroWidget(100),
                    placeholder: placeHolder(100),
                  ),
                ),
              ),
              Container(
                child: Text(
                  cat.name,
                  style: Theme.of(context).textTheme.caption.copyWith(
                      color: colors.fontColor,
                      fontWeight: FontWeight.w600,
                      fontSize: 15),
                  overflow: TextOverflow.ellipsis,
                  textAlign: TextAlign.center,
                ),
                width: 100,
              ),
            ],
          ),
        ),
      );
    }).toList(),
  ),
);

i liked @pedro pimont approach.我喜欢@pedro pimont 方法。

Now i can render multiple row in horizontal list view with minimal code.现在我可以用最少的代码在水平列表视图中渲染多行。 This is working example.这是工作示例。

int itemCount = 12;
int divison = 2;
SizedBox(
              height: MediaQuery.of(context).size.height * .35,
              child: ListView.separated(
                separatorBuilder: (_, __) => const SizedBox(width: 20),
                scrollDirection: Axis.horizontal,
                itemCount: itemCount ~/ divison,
                itemBuilder: (context, index) {
                  return Column(
                    children: [
                      productItem((itemCount ~/ divison) * 0 + index),
                      productItem((itemCount ~/ divison) * 1 + index),
                    ],
                  );
                },
              ),
            )

Widget productItem(int index) => Card(
        color: Colors.grey[50],
        elevation: 1,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5.0),
        ),
        child: Column(
          children: [
            SizedBox(
              height: 80,
              child: Card(
                elevation: 0,
                margin: EdgeInsets.zero,
                semanticContainer: true,
                color: secondaryLight,
                clipBehavior: Clip.antiAliasWithSaveLayer,
                shape: const RoundedRectangleBorder(
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(5),
                      topRight: Radius.circular(5)),
                ),
                child: CachedNetworkImage(
                  imageUrl: 'https://picsum.photos/250?image=9',
                  fit: BoxFit.cover,
                ),
              ),
            ),
            Text("${index + 1}"),
          ],
        ),
      );

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

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