插件:sticky_headers
https://pub.dev/packages/sticky_headers
代码:
import 'package:flutter/material.dart';
import 'package:sticky_headers/sticky_headers.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Widget> timeHeaders = new List<Widget>();
Map<int, List<Widget>> roomRow = new Map<int, List<Widget>>();
double fontSize = 18;
double leadingWidth = 120;
double tileWidth = 84; // CHANGE THIS IF NEEDED
double tileHeight = 30;
double tileBorderWidth = 2;
@override
void initState() {
// TODO: implement initState
super.initState();
timeHeaders.add(
new Container(
height: tileHeight,
width: leadingWidth,
),
);
for (int i = 1; i <= 5; i++) {
timeHeaders.add(
new Container(
height: tileHeight,
width: tileWidth,
decoration: BoxDecoration(
color: Colors.grey[400],
border: Border.all(
color: Colors.black,
width: 2,
),
),
child: Center(
child: Text(
'$i',
style: TextStyle(fontSize: 18),
),
),
),
);
}
for (int i = 0; i < 20; i++) {
roomRow[i + 1] = [
Container(
height: tileHeight,
width: leadingWidth,
child: Center(
child: Text(
'HEADER$i',
style: TextStyle(fontSize: 18),
),
),
),
];
roomRow[i + 1].add(Container(
height: tileHeight,
width: 75,
decoration: BoxDecoration(
color: Colors.lightGreen[300],
border: Border.all(
color: Colors.black,
width: tileBorderWidth,
),
),
child: Container(
margin: EdgeInsets.all(1),
child: Center(
child: Text(
'x',
style: TextStyle(
fontSize: 10,
),
),
),
),
));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
padding: EdgeInsets.all(15.0),
child: Column(
children: <Widget>[
Text('header'),
Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SizedBox(
width: (timeHeaders.length.toDouble() - 1) * tileWidth +
leadingWidth,
child: StickyHeader(
header: Row(children: timeHeaders),
content: ListView.separated(
separatorBuilder: (BuildContext context, int index) =>
Divider(),
itemCount: 21,
itemBuilder: (BuildContext context, int index) {
return index == 0
? Container(height: tileHeight)
: Row(children: roomRow[index]);
},
),
),
),
),
),
],
),
),
);
}
}
错误日志:
I/flutter (18193): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter (18193): The following assertion was thrown during performLayout():
I/flutter (18193): RenderStickyHeader does not meet its constraints.
I/flutter (18193): Constraints: BoxConstraints(w=540.0, h=594.0)
I/flutter (18193): Size: Size(540.0, 624.0)
I/flutter (18193): If you are not writing your own RenderBox subclass, then this is not your fault. Contact support:
I/flutter (18193): https://github.com/flutter/flutter/issues/new?template=BUG.md
624-594=30
,它与tileHeight
变量的高度相同(设置为30)。因此,具有垂直列表视图的功能已关闭。有人知道吗?
答案 0 :(得分:0)
没关系。我刚刚加入
overlapHeaders: true
在StickyHeader
小部件内