本以為像Listview一樣贫奠,加一個(gè)scrollDirection: Axis.horizontal屬性就可以了裁奇,但是SliverList并不支持設(shè)置滑動(dòng)方向
只能換個(gè)思路嘗試用SliverToBoxAdapter嵌套ListView序愚,行了注意需要設(shè)置高度爆雹,不然會(huì)報(bào)錯(cuò) Horizontal viewport was given unbounded height
class _DemoState extends State<Demo> {
@override
Widget build(BuildContext context) {
return CustomScrollView(
slivers: <Widget>[
//頭部
SliverAppBar(
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text('水平橫向滑動(dòng)'),
),
),
//橫向滑動(dòng)區(qū)域
SliverToBoxAdapter(
child: Container(
margin: EdgeInsets.only(top: 10),
height: 100.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 10,
itemBuilder: (context, index) {
return Container(
width: 100.0,
child: Card(
color: Colors.primaries[index],
),
);
},
),
),
),
//垂直列表
SliverList(
delegate: SliverChildBuilderDelegate((content, index) {
return Card(
color: Colors.primaries[index % Colors.primaries.length],
child: Container(
height: 100,
alignment: Alignment.center,
child: Text(index.toString()),
),
);
}, childCount: 30),
)
],
);
}
}