ListView 基本列表:

  1. child: ListView(
  2. children: [
  3. ListTile(
  4. // leading 可以插入图片 或者icon
  5. leading: Icon(
  6. Icons.access_alarm,
  7. ),
  8. title: Text('中华名族伟大复兴'),
  9. subtitle: Text('中国人民最强'),
  10. trailing: Icon(
  11. Icons.sanitizer,
  12. ),
  13. ),
  14. ListTile(
  15. leading: Image.asset(
  16. 'images/pexels-kira-schwarz-6922718.jpg',
  17. fit: BoxFit.cover,
  18. ),
  19. title: Text('中华名族伟大复兴'),
  20. subtitle: Text('中国人民最强'),
  21. trailing: Icon(
  22. Icons.sanitizer,
  23. ),
  24. ),
  25. ],
  26. ),

垂直列表以及水平列表:

  1. Container(
  2. height: 180,
  3. child: ListView(
  4. scrollDirection: Axis.horizontal, // 默认垂直列表, horizontal表示水平列表
  5. children: [
  6. Container(
  7. width: 180,
  8. height: 180,
  9. color: Colors.pink,
  10. ),
  11. Container(
  12. width: 180,
  13. height: 180,
  14. color: Colors.blue,
  15. ),
  16. Container(
  17. width: 180,
  18. height: 180,
  19. color: Colors.orange,
  20. ),
  21. Container(
  22. width: 180,
  23. height: 180,
  24. color: Colors.yellow,
  25. ),
  26. Container(
  27. width: 180,
  28. height: 180,
  29. color: Colors.teal,
  30. ),
  31. Container(
  32. width: 180,
  33. height: 180,
  34. color: Colors.cyan,
  35. ),
  36. ],
  37. ),
  38. );

动态列表:

  1. import 'package:flutter/material.dart';
  2. import 'data/newList.dart';
  3. void main() => runApp(MyApp());
  4. class MyApp extends StatelessWidget {
  5. @override
  6. Widget build(BuildContext context) {
  7. return MaterialApp(
  8. home: Scaffold(
  9. appBar: AppBar(
  10. title: Text('Welcome to Flutter'),
  11. ),
  12. body: HomeContent(),
  13. ),
  14. theme: ThemeData(
  15. primaryColor: Colors.pink,
  16. ),
  17. );
  18. }
  19. }
  20. class HomeContent extends StatelessWidget {
  21. List<Widget> _getData(){
  22. var tempList = listData.map((e){
  23. return ListTile(
  24. leading: Image.network(
  25. e['imageUrl']
  26. ),
  27. title: Text(e['title']),
  28. subtitle: Text(e['author']),
  29. );
  30. });
  31. return tempList.toList();
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return ListView(
  36. children: this._getData(),
  37. );
  38. }
  39. }
  40. // data
  41. List listData = [
  42. {
  43. "title": "Candy Shop",
  44. "author": "Mohamed Chahin",
  45. "imageUrl": "https://images.pexels.com/photos/6569318/pexels-photo-6569318.jpeg?cs=srgb&dl=pexels-david-underland-6569318.jpg&fm=jpg",
  46. },
  47. {
  48. "title": "Candy Shop1",
  49. "author": "Mohamed Chahin",
  50. "imageUrl": "https://images.pexels.com/photos/6922718/pexels-photo-6922718.jpeg?cs=srgb&dl=pexels-kira-schwarz-6922718.jpg&fm=jpg",
  51. },
  52. {
  53. "title": "Candy Shop2",
  54. "author": "Mohamed Chahin",
  55. "imageUrl": "https://images.pexels.com/photos/5994821/pexels-photo-5994821.jpeg?cs=srgb&dl=pexels-matteus-silva-de-oliveira-5994821.jpg&fm=jpg",
  56. },
  57. {
  58. "title": "Candy Shop3",
  59. "author": "Mohamed Chahin",
  60. "imageUrl": "https://images.pexels.com/photos/7173820/pexels-photo-7173820.jpeg?cs=srgb&dl=pexels-lisa-fotios-7173820.jpg&fm=jpg",
  61. },
  62. {
  63. "title": "Candy Shop4",
  64. "author": "Mohamed Chahin",
  65. "imageUrl": "https://images.pexels.com/photos/7042988/pexels-photo-7042988.jpeg?cs=srgb&dl=pexels-jc-siller-7042988.jpg&fm=jpg",
  66. },
  67. {
  68. "title": "Candy Shop5",
  69. "author": "Mohamed Chahin",
  70. "imageUrl": "https://images.pexels.com/photos/7137430/pexels-photo-7137430.jpeg?cs=srgb&dl=pexels-monica-turlui-7137430.jpg&fm=jpg",
  71. },
  72. {
  73. "title": "Candy Shop6",
  74. "author": "Mohamed Chahin",
  75. "imageUrl": "https://images.pexels.com/photos/7137430/pexels-photo-7137430.jpeg?cs=srgb&dl=pexels-monica-turlui-7137430.jpg&fm=jpg",
  76. },
  77. ];
  78. // 方式二, ListView.builder 遍历
  79. import 'package:flutter/material.dart';
  80. import 'data/newList.dart';
  81. void main() => runApp(MyApp());
  82. class MyApp extends StatelessWidget {
  83. @override
  84. Widget build(BuildContext context) {
  85. return MaterialApp(
  86. home: Scaffold(
  87. appBar: AppBar(
  88. title: Text('Welcome to Flutter'),
  89. ),
  90. body: HomeContent(),
  91. ),
  92. theme: ThemeData(
  93. primaryColor: Colors.pink,
  94. ),
  95. );
  96. }
  97. }
  98. class HomeContent extends StatelessWidget {
  99. @override
  100. Widget build(BuildContext context) {
  101. // ListView.builder 遍历
  102. return ListView.builder(
  103. itemCount: listData.length, // 数据
  104. itemBuilder: (context, index){
  105. return ListTile(
  106. title: Text(listData[index]["title"]),
  107. subtitle: Text(listData[index]["author"]),
  108. trailing: Image.network(listData[index]['imageUrl']),
  109. );
  110. },
  111. );
  112. }
  113. }