动态列表组件两种展现形式

动态列表组件两种展现形式

 

 

  List listData=[
      {
          "title": ‘Candy Shop‘,
          "author": ‘Mohamed Chahin‘,
          "imageUrl": ‘https://www.itying.com/images/flutter/1.png‘,
      },
       {
          "title": ‘Childhood in a picture‘,
          "author": ‘Google‘,
          "imageUrl": ‘https://www.itying.com/images/flutter/2.png‘,
      },
      {
          "title": ‘Alibaba Shop‘,
          "author": ‘Alibaba‘,
          "imageUrl": ‘https://www.itying.com/images/flutter/3.png‘,
      },
      {
          "title": ‘Candy Shop‘,
          "author": ‘Mohamed Chahin‘,
          "imageUrl": ‘https://www.itying.com/images/flutter/4.png‘,
      },
       {
          "title": ‘Tornado‘,
          "author": ‘Mohamed Chahin‘,
          "imageUrl": ‘https://www.itying.com/images/flutter/5.png‘,
      },
      {
          "title": ‘Undo‘,
          "author": ‘Mohamed Chahin‘,
          "imageUrl": ‘https://www.itying.com/images/flutter/6.png‘,
      },
      {
          "title": ‘white-dragon‘,
          "author": ‘Mohamed Chahin‘,
          "imageUrl": ‘https://www.itying.com/images/flutter/7.png‘,
      }      

  ];

1

import ‘package:flutter/material.dart‘;
import ‘res/listData.dart‘;

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text("flutterDemo")),
      body: HomeContent(),
    ));
  }
}

class HomeContent extends StatelessWidget {
  // 自定义方法
  List<Widget> _getData() {
    // List<Widget> list = [];

    // for (var i = 0; i < 20; i++) {
    //   list.add(ListTile(
    //     title: Text(‘我是${i}个列表‘),
    //   ));
    // }
    // return list;

    var tempList = listData.map((value) {
      return ListTile(
        leading: Image.network(value["imageUrl"]),
        title: Text(value["title"]),
        subtitle: Text(value["author"]),
      );
    });

    return tempList.toList();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: this._getData(),
    );
  }
}
 
2
 
import ‘package:flutter/material.dart‘;
import ‘res/listData.dart‘;

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text("flutterDemo")),
      body: HomeContent(),
    ));
  }
}

class HomeContent extends StatelessWidget {
// 自定义方法
  Widget _getListData(context, index) {
    return ListTile(
      leading: Image.network(listData[index]["imageUrl"]),
      title: Text(listData[index]["title"]),
      subtitle: Text(listData[index]["author"]),
    );
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: listData.length, itemBuilder: this._getListData);
  }
}
 
 

 

动态列表组件两种展现形式

上一篇:Django开发阶段静态文件404解决;nostatic的作用


下一篇:数据类型转换