简体   繁体   English

flutter 中的可点击水平列表视图。 如何使用列表中的图像制作可点击的列表视图

[英]tappable Horizontal list view in flutter. how do i make a clickable list view with images from the list

i am new to flutter and dart and i wanted to make a horizontal list with images that one can click and that will take them to the product page.我是 flutter 和 dart 的新手,我想制作一个水平列表,其中包含可以单击的图像,并将它们带到产品页面。 i want the product details to be taken from the var product_list which i will use to make the product page.我希望从我将用来制作产品页面的var product_list中获取产品详细信息。 the below code runs but the list is not visible下面的代码运行但列表不可见

import 'package:flutter/material.dart';

import 'package:app/pages/product_details.dart';

class Mostpop extends StatelessWidget {
  final product_name;
  final product_picture;
  final product_price;

  Mostpop({
    this.product_name,
    this.product_picture,
    this.product_price
});
  var product_list = [
    {
      "name": "The Bundle",
      "picture": "images/thumbnails/t1.jpg",
      "price": 99,
    },
    {
      "name": "The Bundle",
      "picture": "images/thumbnails/t1.jpg",
      "price": 99,
    },
    {
      "name": "The Bundle",
      "picture": "images/thumbnails/t1.jpg",
      "price": 99,
    },
    {
      "name": "The Bundle",
      "picture": "images/thumbnails/t1.jpg",
      "price": 99,
    },
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Horizontal ListView'),
      ),
      body: Container(
        padding: EdgeInsets.symmetric(),
        height: MediaQuery.of(context).size.height * 0.35,
        child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: product_list.length, itemBuilder: (context, index) {
          return Container(
            width: MediaQuery.of(context).size.width * 0.6,
            child: Card(
              color: Colors.blue,
              child: Container(
                child: Center(child: Image.asset(product_picture,fit: BoxFit.cover,)),
              ),
            ),
          );
        }),
      ),
    );
  }
}
  1. Make a series of container widgets that will be held in the list view制作一系列将保存在列表视图中的容器小部件
  2. Map the list in order to pass the product_pictures to each of the Image.asset features Map 列表,以便将 product_pictures 传递给每个 Image.asset 功能

see => Dart: mapping a list (list.map)参见 => Dart:映射列表 (list.map)

with what you have now when you call product_picture within Imageasset nothing is being referenced because product_picture is within a list, you need to use the list when calling the product picture within the dictionary与您现在在 Imageasset 中调用 product_picture 时所拥有的一样,没有任何内容被引用,因为 product_picture 在列表中,您需要在字典中调用产品图片时使用该列表

The issue is caused by the following line该问题是由以下行引起的

child: Center(child: Image.asset(product_picture,fit: BoxFit.cover,)),

product_picture is not initialized (its null). product_picture 未初始化(其为空)。 Also when you use the ListView.Builder you can take advantage of the index parameter to return the Widget for every item in product list.此外,当您使用 ListView.Builder 时,您可以利用 index 参数为产品列表中的每个项目返回小部件。

You can modify the code to the following -您可以将代码修改为以下内容 -

class Mostpop extends StatelessWidget {
  final product_name;
  final product_picture;
  final product_price;

  Mostpop({this.product_name, this.product_picture, this.product_price});
  var product_list = [
    {
      "name": "The Bundle",
      "picture": "assets/images/t1.png",
      "price": 99,
    },
    {
      "name": "The Bundle",
      "picture": "assets/images/t1.png",
      "price": 99,
    },
    {
      "name": "The Bundle",
      "picture": "assets/images/t1.png",
      "price": 99,
    },
    {
      "name": "The Bundle",
      "picture": "assets/images/t1.png",
      "price": 99,
    },
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Horizontal ListView'),
      ),
      body: Container(
        padding: EdgeInsets.symmetric(),
        height: MediaQuery.of(context).size.height * 0.35,
        child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: product_list.length,
            itemBuilder: (context, index) {
              return Container(
                width: MediaQuery.of(context).size.width * 0.6,
                child: Card(
                  color: Colors.blue,
                  child: Container(
                    child: Center(
                        child: Image.asset(
                      product_list[index]["picture"],
                      fit: BoxFit.cover,
                    )),
                  ),
                ),
              );
            }),
      ),
    );
  }
}

截屏

Also you should consider separating the widget tree with the model definition, also strongly typed model will help you detect all the type checking errors at compile time as otherwise they may arrive during the runtime.此外,您应该考虑将小部件树与 model 定义分开,强类型 model 将帮助您在编译时检测所有类型检查错误,否则它们可能会在运行时到达。

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

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