页面内多视图的联动就需要使用sliver组件了,首先要借助视窗CustomScrollview,CustomScrollView才可以将多个Sliver"粘"在一起,在内部使用sliver,sliver包含了多种组件,都是以sliver开头,比如SliverList、sliverGrid、SliverAppBar、SliverPadding、SliverFixedExtentList。
import 'package:flutter/material.dart';
class CustomScrollViewL extends StatefulWidget {
CustomScrollViewL({Key key, List<Widget> slivers}) : super(key: key);
@override
_CustomScrollViewState createState() => _CustomScrollViewState();
}
class _CustomScrollViewState extends State<CustomScrollViewL> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
//AppBar,包含一个导航栏
SliverAppBar(
pinned: true,
expandedHeight: 200.0,
flexibleSpace: FlexibleSpaceBar(
title: const Text('CustomScrollView'),
background: Image.asset(
"assets/images/splash_bg.png",
fit: BoxFit.cover,
),
),
),
SliverPadding(
padding: const EdgeInsets.all(8.0),
sliver: SliverGrid(
//Grid
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3, //Grid按两列显示
mainAxisSpacing: 10.0,
crossAxisSpacing: 10.0,
childAspectRatio: 3,
),
delegate: new SliverChildBuilderDelegate(
(BuildContext context, int index) {
//创建子widget
return new Container(
alignment: Alignment.center,
color: Colors.cyan[100 * (index % 9)],
child: new Text('grid item $index'),
);
},
childCount: 20,
),
),
),
//List
SliverFixedExtentList(
itemExtent: 50.0,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
//创建列表项
return Container(
alignment: Alignment.center,
color: Colors.lightBlue[100 * (index % 9)],
child: new Text('list item $index'),
);
}, childCount: 50 //50个列表项
),
),
],
),
);
}
}

网友评论