Flutter 案例學習之:ListView
GitHub:https://github.com/happy-python/flutter_demos/tree/master/listview_demo
lib/main.dart
import 'package:flutter/material.dart';
import 'package:listview_demo/pages/homepage.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Flutter Tutorial",
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
lib/model/city.dart
class City {
final String name;
final String image;
final String population;
final String country;
City({this.name, this.image, this.population, this.country});
static List<City> allCities() {
var cities = List<City>();
cities.add(City(
name: "Delhi",
country: "India",
population: "19 mill",
image: "delhi.png",
));
cities.add(City(
name: "London",
country: "Britain",
population: "8 mill",
image: "london.png",
));
cities.add(City(
name: "Vancouver",
country: "Canada",
population: "2.4 mill",
image: "vancouver.png",
));
cities.add(City(
name: "New York",
country: "USA",
population: "8.1 mill",
image: "newyork.png",
));
cities.add(City(
name: "Paris",
country: "France",
population: "2.2 mill",
image: "paris.png",
));
cities.add(City(
name: "Berlin",
country: "Germany",
population: "3.7 mill",
image: "berlin.png",
));
return cities;
}
}
lib/pages/homepage.dart
import 'package:flutter/material.dart';
import 'package:listview_demo/model/city.dart';
class HomePage extends StatelessWidget {
final List<City> _allCities = City.allCities();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Cites around world"),
),
body: Padding(
padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
return _getItemUI(context, index);
},
itemCount: _allCities.length,
),
),
);
}
Widget _getItemUI(BuildContext context, int index) {
return Card(
child: Column(
children: <Widget>[
ListTile(
leading: Image.asset(
_allCities[index].image,
fit: BoxFit.cover,
width: 100.0,
),
title: Text(
_allCities[index].name,
style: TextStyle(fontSize: 14.0, fontWeight: FontWeight.bold),
),
subtitle: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_allCities[index].country,
style: TextStyle(
fontSize: 13.0,
),
),
Text(
"Population: ${_allCities[index].population}",
style: TextStyle(
fontSize: 11.0,
),
),
],
),
onTap: () {
_showSnackBar(context, _allCities[index]);
},
)
],
),
);
}
_showSnackBar(BuildContext context, City item) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text("${item.name} is a city in ${item.country}"),
backgroundColor: Colors.amber,
),
);
}
}
由於所有這些影象都靜態地包含在專案中,為了顯示影象,我們需要在pubspec.yaml
(主配置檔案)中進行很少的配置,如下所示:
assets:
- berlin.png
- delhi.png
- london.png
- newyork.png
- paris.png
- vancouver.png
相關文章
- flutter系列之:flutter中listview的高階用法FlutterView
- Flutter - Listview 詳解FlutterView
- [譯]Flutter - 掌握ListViewFlutterView
- Flutter基礎(七)Scrolling Widget之ListView、GridView、PageViewFlutterView
- Flutter 基礎(七)Scrolling Widget 之 ListView、GridView、PageViewFlutterView
- Flutter ListView 用法詳解FlutterView
- Flutter 中 ListView 的使用FlutterView
- Flutter ListView 原始碼分析FlutterView原始碼
- Flutter 1.12 個人練手學習小案例Flutter
- Flutter的ListView(靜態列表)FlutterView
- Flutter——ListView的建立方式FlutterView
- [- Flutter 必備 -] ListView的使用FlutterView
- Flutter ListView 實戰快速上手FlutterView
- Flutter學習系列之Flutter上手環境準備Flutter
- Flutter之FutureBuilder的學習和使用FlutterRebuild
- Flutter學習之檢視體系Flutter
- Flutter 學習 - Widget 之 Image和IconFlutter
- Flutter學習之Dart語法(一)FlutterDart
- Flutter學習之”相對佈局“Flutter
- Flutter學習之Dart語法(二)FlutterDart
- Flutter學習之Dart語法(三)FlutterDart
- Flutter學習之Dart語法特性FlutterDart
- Flutter Canvas學習之繪圖篇FlutterCanvas繪圖
- Flutter可滾動Widgets-ListViewFlutterView
- Flutter學習之入門和體驗Flutter
- Flutter學習之認知基礎元件Flutter元件
- 學習flutter必備之原理理解Flutter
- Flutter 學習筆記:再次重新學習FlutterFlutter筆記
- Flutter 例項 - 載入更多的ListViewFlutterView
- Flutter 滾動控制元件篇-->ListViewFlutter控制元件View
- Flutter滾動型容器元件 - ListView篇Flutter元件View
- flutter中ListView做一個掘金列表FlutterView
- Flutter 學習Flutter
- Web 開發者如何理解 Flutter 佈局之 —— 5. ListView 和 ScrollBarWebFlutterView
- (七)Flutter學習之開發環境搭建Flutter開發環境
- Flutter學習之佈局、互動、動畫Flutter動畫
- Flutter 佈局(十)- ListBody、ListView、CustomMultiChildLayout詳解FlutterView
- Flutter 實戰系列:個性化 ListView physicsFlutterView