【问题标题】:Flutter using ListView.builder使用 ListView.builder 颤振
【发布时间】:2020-09-18 07:17:50
【问题描述】:

如何使用如下模型和数据应用listview builder来实现截图中的布局:

型号

class Location {
  String name;
  String imagePath;
  String summary;

  Location(this.name, this.imagePath, this.summary);
  
}

数据

import 'package:app_data_model/model/location.dart';

var locationData = [
  Location(
      'Statue of Liberty',
      'assets/images/new-york-city-statue-of-liberty.jpg',
      'The Statue of Liberty was France\'s gift to America. Built in 1886, it remains a famous world symbol of freedom and one of the greatest American icons. '),
  Location(
      'Central Park',
      'assets/images/new-york-city-central-park-lake-bridge-boats.jpg',
      'A walk, peddle, or carriage ride through the crisscrossing pathways of Central Park is a must-do on anyone\'s New York City itinerary. '),
  Location(
      'Empire State Building',
      'assets/images/new-york-city-empire-state-building.jpg',
      'The Empire State Building is one of New York\'s most famous landmark buildings and key tourist attractions. The 381-meter-tall, 102-storey building was the tallest in the world until the 1 World Trade Center tower rose higher, 41 years later. ')
];

【问题讨论】:

标签: flutter dart


【解决方案1】:

根据您的需要添加了一个演示:


class StackOver extends StatelessWidget {
  var locationData = [
    Location(
        'Statue of Liberty',
        'assets/images/new-york-city-statue-of-liberty.jpg',
        'The Statue of Liberty was France\'s gift to America. Built in 1886, it remains a famous world symbol of freedom and one of the greatest American icons. '),
    Location(
        'Central Park',
        'assets/images/new-york-city-central-park-lake-bridge-boats.jpg',
        'A walk, peddle, or carriage ride through the crisscrossing pathways of Central Park is a must-do on anyone\'s New York City itinerary. '),
    Location(
        'Empire State Building',
        'assets/images/new-york-city-empire-state-building.jpg',
        'The Empire State Building is one of New York\'s most famous landmark buildings and key tourist attractions. The 381-meter-tall, 102-storey building was the tallest in the world until the 1 World Trade Center tower rose higher, 41 years later. ')
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Solve Before Downvote !'),
      ),
      body: ListView.builder(
        // give the listview a length based on your location data
        itemCount: locationData.length,
        itemBuilder: (context, index) {
          // return a custom widget based on your preference 
          return ListTile(
            // access the imagePath of your [locationData] using the index provided by the itembuilder
            leading: Image.asset(
              locationData[index].imagePath,
            ),
            // access the name of your [locationData] using the index provided by the itembuilder
            title: Text(
              locationData[index].name,
            ),
          );
        },
      ),
    );
  }
}

结果:

【讨论】:

  • 完美,成功了。如果我想使用 map 达到同样的效果怎么办?我们什么时候使用地图?
  • 使用 map 是什么意思?您能否更新您的问题或提出一个单独的问题以包含有关您所问内容的更多详细信息?
  • stackflow 不允许添加更多代码,因此提供了上面带有地图的代码截图。 ListView.builder 工作正常,但我想使用地图读取列表中的每个项目以生成动态列表视图?
  • 为什么要将ListView 放在另一个ListView 中?
  • 是的,我很困惑。我试图实现的是使用地图创建一个动态列表视图。我指的是从 firebase 提取数据的本指南,但我将数据放在 dart 文件中。 hackernoon.com/…
【解决方案2】:

ListView.builder(itemCount: locationData.length, (BuildContext context,index)=>ListTile(前导: Image.network(locationData[index].image),title :Text(locationData[index].locationName);)

检查 Location 模型以获得正确的属性

【讨论】:

  • 为了使您的代码在 Location 构造方法中易于阅读和易于使用,请将 {} 添加到您的属性中,这样您在调用构造方法时就不会感到困惑
猜你喜欢
  • 2022-01-14
  • 2021-12-22
  • 2021-10-22
  • 1970-01-01
  • 2021-09-19
  • 2022-01-23
  • 1970-01-01
  • 2020-07-13
  • 2020-03-09
相关资源
最近更新 更多