Skip to content

Commit d96ad3b

Browse files
committed
feat: listView
1 parent a1f661b commit d96ad3b

File tree

6 files changed

+163
-10
lines changed

6 files changed

+163
-10
lines changed
Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
1-
## **ListView**
1+
## **ListView**
2+
3+
>
4+
滚动列表控件
5+
* 与GridView相似,基本上市slivers里面只包含一个SliverList的CustomScrollView
6+
* 在Flutter中有几种构建ListView的方式,分别为:默认 **List, ListView.builder, ListView.separated, ListView.custom**
7+
8+
### 构造方法
9+
``` dart
10+
ListView({
11+
Key key,
12+
Axis scrollDirection = Axis.vertical,
13+
bool reverse = false,
14+
ScrollController controller,
15+
bool primary,
16+
ScrollPhysics physics,
17+
bool shrinkWrap = false,
18+
EdgeInsetsGeometry padding,
19+
this.itemExtent,
20+
bool addAutomaticKeepAlives = true,
21+
bool addRepaintBoundaries = true,
22+
bool addSemanticIndexes = true,
23+
double cacheExtent,
24+
List<Widget> children = const <Widget>[],
25+
int semanticChildCount,
26+
})
27+
```
28+
29+
### 属性介绍
30+
* scrollDirection: 滚动方向,值为Axis.vertical/horizontal, 默认为垂直方向(Axis.vertical)
31+
* reverse:默认是从上到下或者从左到右滚动的,这个属性控制是否反向,默认值为false,不反向滚动
32+
* controller: 控制child滚动时候的位置
33+
* physics: ScrollPhysics类,影响视图与用户输入交互
34+
* padding: 边距区域
35+
* shrinkWrap: 默认false, 滚动视图内容展开和收缩时不重新计算视图大小
36+
* itemExtent: ListView在滚动方向上每个item所占的高度值
37+
* children:ListView中的内容Widget
38+
39+
> ListView默认方式
40+
* 默认List方式,是把数据添加到列表中,之后直接添加到ListView即可,适用于只有少量子项
41+
42+
> ListView.builder()
43+
* 适用于具有大量(或无限)子项数的列表视图
44+
* 设置单个item的属性,懒加载,假如有1000个列表,初始渲染时不会对所有都进行渲染,而只会对特定数量的item进行渲染,这对于性能和用户体验来说是很好的提升
45+
> ListView.separated()
46+
* separated相对于builder多了一个separatorBuilder参数,它的作用是返回一个分割线,用于控制列表各个元素的间隔如何渲染
47+
> ListView.custom()
48+
* 必须参数是childrenDelegate,然后传入一个实现了SliverChildDelegate的组件,如SliverChildListDelegate或者SliverChildBuilderDelegate

flutter-ui

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit d9e49e6358a786d3eb3eb567557ce84fa9af77b7

lib/widget/regular/listview/demo.dart

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,42 @@ class _IndexState extends State<Index> {
1919
'http://pic1.win4000.com/wallpaper/2019-02-14/5c65108043791.jpg',
2020
'http://pic1.win4000.com/wallpaper/2019-02-14/5c651084373de.jpg'
2121
];
22+
var scrollDirections = Axis.vertical;
23+
2224
@override
2325
Widget build(BuildContext context) {
2426
return Scaffold(
2527
appBar: AppBar(title: Text('ListView'),),
2628
body: ListView(
27-
scrollDirection: Axis.vertical,
29+
scrollDirection: scrollDirections,
30+
reverse: false,
31+
controller: ScrollController(
32+
initialScrollOffset: ScreenUtil().setWidth(420),
33+
keepScrollOffset: true
34+
),
35+
primary: false,
36+
physics: ScrollPhysics(),
37+
shrinkWrap: false,
38+
padding: EdgeInsets.all(20.0),
39+
// itemExtent: ScreenUtil().setWidth(420),
2840
children: List.generate(10, (index) {
29-
return ListTile(
30-
title: Text('title $index'),
31-
subtitle: Text('subtitle $index'),
32-
leading: CircleAvatar(
33-
backgroundImage: NetworkImage(listview[index]),
41+
return InkWell(
42+
onTap: () {
43+
this.setState(() {
44+
scrollDirections == Axis.vertical ? scrollDirections = Axis.horizontal : scrollDirections = Axis.vertical;
45+
});
46+
},
47+
child: Container(
48+
width: ScreenUtil().setWidth(420),
49+
height: ScreenUtil().setWidth(220),
50+
decoration: BoxDecoration(
51+
image: DecorationImage(
52+
image: NetworkImage(listview[index]),
53+
fit: BoxFit.cover
54+
),
55+
),
3456
),
35-
trailing: Icon(Icons.arrow_right),
36-
);
57+
);
3758
}),
3859
),
3960
);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:flutter/material.dart';
2+
3+
class Index extends StatefulWidget {
4+
@override
5+
_IndexState createState() => _IndexState();
6+
}
7+
8+
class _IndexState extends State<Index> {
9+
List listview = [
10+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c3e1d90c.jpg',
11+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c40f3bc2.jpg',
12+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c4406144.jpg',
13+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c46823f8.jpg',
14+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c48c73d0.jpg',
15+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c4b4dc2f.jpg',
16+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c51a2a45.jpg',
17+
'http://pic1.win4000.com/wallpaper/2019-02-14/5c65107a0ee05.jpg',
18+
'http://pic1.win4000.com/wallpaper/2019-02-14/5c65108043791.jpg',
19+
'http://pic1.win4000.com/wallpaper/2019-02-14/5c651084373de.jpg'
20+
];
21+
22+
@override
23+
Widget build(BuildContext context) {
24+
return Scaffold(
25+
appBar: AppBar(title: Text('ListView.custom'),),
26+
body: ListView.custom(
27+
childrenDelegate: SliverChildBuilderDelegate(
28+
(context, index) {
29+
return Image.network(listview[index]);
30+
},
31+
childCount: listview.length
32+
),
33+
),
34+
);
35+
}
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import 'package:flutter/material.dart';
2+
3+
class Index extends StatefulWidget {
4+
@override
5+
_IndexState createState() => _IndexState();
6+
}
7+
8+
class _IndexState extends State<Index> {
9+
List listview = [
10+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c3e1d90c.jpg',
11+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c40f3bc2.jpg',
12+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c4406144.jpg',
13+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c46823f8.jpg',
14+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c48c73d0.jpg',
15+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c4b4dc2f.jpg',
16+
'http://pic1.win4000.com/wallpaper/2019-02-15/5c664c51a2a45.jpg',
17+
'http://pic1.win4000.com/wallpaper/2019-02-14/5c65107a0ee05.jpg',
18+
'http://pic1.win4000.com/wallpaper/2019-02-14/5c65108043791.jpg',
19+
'http://pic1.win4000.com/wallpaper/2019-02-14/5c651084373de.jpg'
20+
];
21+
22+
@override
23+
Widget build(BuildContext context) {
24+
return Scaffold(
25+
appBar: AppBar(title: Text('ListView.separated'),),
26+
body: ListView.separated(
27+
itemCount: listview.length,
28+
itemBuilder: (BuildContext context, int index) {
29+
return ListTile(
30+
title: Text('title $index'),
31+
subtitle: Text('subtitle $index'),
32+
leading: CircleAvatar(
33+
backgroundImage: NetworkImage(listview[index]),
34+
),
35+
trailing: Icon(Icons.arrow_right),
36+
);
37+
},
38+
separatorBuilder: (BuildContext context, int index) {
39+
return Image.network(listview[index]);
40+
},
41+
),
42+
);
43+
}
44+
}

lib/widget/regular/listview/index.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
22
import 'package:efox_flutter/components/widgetComp.dart' as WidgetComp;
33
import 'demo.dart' as Demo;
44
import 'demo_builder.dart' as DemoBuilder;
5+
import 'demo_separated.dart' as DemoSeparated;
6+
import 'demo_custom.dart' as DemoCustom;
57

68
class Index extends StatefulWidget {
79
static String title = 'ListView';
@@ -20,7 +22,9 @@ class _IndexState extends State<Index> {
2022
mdUrl: Index.mdUrl,
2123
demoChild: <Widget>[
2224
Demo.Index(),
23-
DemoBuilder.Index()
25+
DemoBuilder.Index(),
26+
DemoSeparated.Index(),
27+
DemoCustom.Index()
2428
],
2529
);
2630
}

0 commit comments

Comments
 (0)