Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
84 lines (73 sloc) 2.06 KB
import 'package:flutter/material.dart';
import 'mock/list.dart' as newsList;
const TITLE = '标题标题标题标题标题标题标题';
const SUB_TITLE = '二级标题二级标题二级标题二级标题二级标题二级标题二级标题二级标题二';
const IMAGE_SRC =
'https://cdn.pixabay.com/photo/2019/05/20/13/22/portugal-4216645_1280.jpg';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('ListView Widget'),
backgroundColor: Colors.pink,
),
body: HomeContent(),
),
);
}
}
class HomeContent extends StatelessWidget {
// item build 方法
Widget _buildListItem(BuildContext context, int index) {
Map newsItem = newsList.news[index];
return ListItem(
title: newsItem['title'],
subTitle: newsItem['time'],
cover: newsItem['imgurl']);
}
// 无分割线的列表
// @override
// Widget build(BuildContext context) {
// return ListView.builder(
// itemCount: newsList.news.length,
// itemBuilder: this._buildListItem); // 接收的两个参数
// }
// 能够生成下划线的列表
@override
Widget build(BuildContext context) {
return ListView.separated(
itemCount: newsList.news.length,
itemBuilder: this._buildListItem,
separatorBuilder: (BuildContext context, int index) {
return Divider(
color: Colors.grey[350],
);
},
); // 接收的两个参数
}
}
class ListItem extends StatelessWidget {
ListItem({this.title, this.subTitle, this.cover});
final String title;
final String subTitle;
final String cover;
@override
Widget build(BuildContext context) {
return ListTile(
leading: Container(
child: Image.network(cover),
color: Colors.grey,
width: 80,
height: 80,
),
trailing: Icon(Icons.chevron_right),
title: Text(this.title),
subtitle: Text(this.subTitle),
);
}
}
You can’t perform that action at this time.