flutter - 使用ListView.builder flutter

标签 flutter dart

如何使用以下模型和数据应用listview builder,以实现屏幕截图中的布局:
enter image description here
模型

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. ')
];

最佳答案

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

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,
            ),
          );
        },
      ),
    );
  }
}
结果:
result

关于flutter - 使用ListView.builder flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63950986/

相关文章:

android - 有人可以解释一下为什么我的 flutter 代码不起作用

asynchronous - 如何使异步 Dart 调用同步?

flutter - 如何在 Flutter 中的 EditText 上更改气泡(光标下)的颜色

flutter - 如何取消选择 flutter 中的单选按钮?

android - 为什么需要shrinkWrap?

android - 无法发送到 Cloud Firestore

android - 如何在 flutter 中制作音频修剪小部件和声音波形图

flutter - 如何在命名构造函数中使用异步?

flutter - 如何让 flutter run -d chrome 使用 https?

android - 如果我的函数调用异步函数,是否会迫使我让我的函数返回 Future?