Flutter 有没有办法在颤振中使用像sliverappbar这样的容器?
两天前,我在Flatter中学习了sliver小部件的使用,当时我正在使用它来提高我的技能。我在做一个想象中的项目,我在那里遇到了一个问题 让我们想想,在我身体的容器中有15个容器在x小部件下允许它们垂直滚动。现在,我的目标是,当我滚动时,我想当容器号5到达顶部时,容器号5会像appbar或Silver appbar一样固定在那里,其他人会在它下面滚动 在这里,我使用CustomScrollView小部件下的sliver和SliverFixedExtentList来说明我的目标,如果还有其他没有sliver的选项,请随时与我分享。提前感谢:)Flutter 有没有办法在颤振中使用像sliverappbar这样的容器?,flutter,containers,custom-scrolling,sliverappbar,Flutter,Containers,Custom Scrolling,Sliverappbar,两天前,我在Flatter中学习了sliver小部件的使用,当时我正在使用它来提高我的技能。我在做一个想象中的项目,我在那里遇到了一个问题 让我们想想,在我身体的容器中有15个容器在x小部件下允许它们垂直滚动。现在,我的目标是,当我滚动时,我想当容器号5到达顶部时,容器号5会像appbar或Silver appbar一样固定在那里,其他人会在它下面滚动 在这里,我使用CustomScrollView小部件下的sliver和SliverFixedExtentList来说明我的目标,如果还有其他没有
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
正文:自定义滚动视图(
条子:[
滑杆(
行动:[
图标(
图标。摄像头前,
尺码:40,
)
],
标题:文本(“条子示例”),
前导:图标(图标.菜单),
背景颜色:Colors.green,
扩展高度:100.0,
浮动:是的,
是的,
SliverFixedExtentList(
项目范围:75,
委托:SliverChildListDelegate([
容器(
颜色:颜色,蓝色,
子项:文本(“1”),
),
容器(
颜色:颜色。粉红色,
子项:文本(“2”),
),
容器(
颜色:颜色,黄色,
儿童:文本(“3”),
),
容器(
颜色:颜色,红色,
儿童:文本(“4”),
),
容器(
颜色:颜色,黑色,
子:文本(
“所需的Appbar Conainer编号5,当到达顶部时,它将卡在那里,\n而不是SliveAppBar条子示例”,
样式:TextStyle(颜色:Colors.white),
),
),
容器(
颜色:颜色。琥珀色,
儿童:文本(“6”),
),
容器(
颜色:颜色,蓝色,
儿童:文本(“7”),
),
容器(
颜色:颜色,黄色,
儿童:文本(“8”),
),
容器(
颜色:颜色,蓝色,
儿童:文本(“9”),
),
容器(
颜色:颜色。粉红色,
儿童:文本(“10”),
),
容器(
颜色:颜色,蓝色,
儿童:文本(“11”),
),
容器(
颜色:颜色,黄色,
儿童:文本(“12”),
),
容器(
颜色:颜色,蓝色,
儿童:文本(“13”),
),
容器(
颜色:颜色。紫红色,
儿童:文本(“14”),
),
容器(
颜色:颜色,白色,
儿童:文本(“15”),
),
]),
),
],
),
),
);
}
}
您的想法仍然有效,只需稍作调整,而不是使用SliverFixedExtentList,您可以使用它,它提供粘性sliver list标题,对于您的情况,您可以只设置一个标题:
import 'package:flutter/material.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import '../common.dart';
class ListExample extends StatelessWidget {
const ListExample({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return CustomScrollView(
slivers: [
//SliverAppBar(),
//SliverList(),
_StickyHeaderList(index: 2),
//SliverList(),
],
);
}
}
class _StickyHeaderList extends StatelessWidget {
const _StickyHeaderList({
Key key,
this.index,
}) : super(key: key);
final int index;
@override
Widget build(BuildContext context) {
return SliverStickyHeader(
header: Header(index: index),
sliver: SliverList(
delegate: SliverChildBuilderDelegate(
(context, i) => ListTile(
leading: CircleAvatar(
child: Text('$index'),
),
title: Text('List tile #$i'),
),
childCount: 6,
),
),
);
}
}
您的想法仍然有效,只需稍作调整,而不是使用SliverFixedExtentList,您可以使用它,它提供了粘滞的sliver列表标题,对于您的情况,您可以只设置一个标题:
import 'package:flutter/material.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import '../common.dart';
class ListExample extends StatelessWidget {
const ListExample({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return CustomScrollView(
slivers: [
//SliverAppBar(),
//SliverList(),
_StickyHeaderList(index: 2),
//SliverList(),
],
);
}
}
class _StickyHeaderList extends StatelessWidget {
const _StickyHeaderList({
Key key,
this.index,
}) : super(key: key);
final int index;
@override
Widget build(BuildContext context) {
return SliverStickyHeader(
header: Header(index: index),
sliver: SliverList(
delegate: SliverChildBuilderDelegate(
(context, i) => ListTile(
leading: CircleAvatar(
child: Text('$index'),
),
title: Text('List tile #$i'),
),
childCount: 6,
),
),
);
}
}
似乎我找到了这个问题的有效解决方案。答案基于=> 这是我解决这个问题的代码-
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
SliverPersistentHeader makeHeader(String headerText) {
return SliverPersistentHeader(
pinned: true,
delegate: _SliverAppBarDelegate(
minHeight: 75.0,
maxHeight: 75.0,
child: Container(
color: Colors.black,
child: Center(
child: Text(
headerText,
style: TextStyle(color: Colors.white),
),
),
),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverFixedExtentList(
itemExtent: 75,
delegate: SliverChildListDelegate([
Container(
color: Colors.blue,
child: Text("1"),
),
Container(
color: Colors.pink,
child: Text("2"),
),
Container(
color: Colors.yellow,
child: Text("3"),
),
Container(
color: Colors.red,
child: Text("4"),
),
]),
),
makeHeader("Container Number 5"),
SliverFixedExtentList(
itemExtent: 75,
delegate: SliverChildListDelegate([
Container(
color: Colors.amber,
child: Text("6"),
),
Container(
color: Colors.blue,
child: Text("7"),
),
Container(
color: Colors.yellow,
child: Text("8"),
),
Container(
color: Colors.blue,
child: Text("9"),
),
Container(
color: Colors.pink,
child: Text("10"),
),
Container(
color: Colors.blue,
child: Text("11"),
),
Container(
color: Colors.yellow,
child: Text("12"),
),
Container(
color: Colors.blue,
child: Text("13"),
),
Container(
color: Colors.purpleAccent,
child: Text("14"),
),
Container(
color: Colors.white,
child: Text("15"),
),
Container(
color: Colors.yellow,
child: Text("12"),
),
Container(
color: Colors.blue,
child: Text("13"),
),
Container(
color: Colors.purpleAccent,
child: Text("14"),
),
Container(
color: Colors.white,
child: Text("15"),
),
Container(
color: Colors.yellow,
child: Text("12"),
),
Container(
color: Colors.blue,
child: Text("13"),
),
Container(
color: Colors.purpleAccent,
child: Text("14"),
),
Container(
color: Colors.white,
child: Text("15"),
),
Container(
color: Colors.yellow,
child: Text("12"),
),
Container(
color: Colors.blue,
child: Text("13"),
),
Container(
color: Colors.purpleAccent,
child: Text("14"),
),
Container(
color: Colors.white,
child: Text("15"),
),
]),
),
],
),
),
);
}
}
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
_SliverAppBarDelegate({
@required this.minHeight,
@required this.maxHeight,
@required this.child,
});
final double minHeight;
final double maxHeight;
final Widget child;
@override
double get minExtent => minHeight;
@override
double get maxExtent => math.max(maxHeight, minHeight);
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return new SizedBox.expand(child: child);
}
@override
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
return maxHeight != oldDelegate.maxHeight ||
minHeight != oldDelegate.minHeight ||
child != oldDelegate.child;
}
}
导入“包装:颤振/材料.省道”;
导入'dart:math'作为数学;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
SliverPersistentHeader生成标头(字符串标头文本){
回程滑动头(
对,,
代表:_SliverAppBarDelegate(
最小身高:75.0,
最大高度:75.0,
子:容器(
颜色:颜色,黑色,
儿童:中心(
子:文本(
标题文字,
样式:TextStyle(颜色:Colors.white),
),
),
),
),
);
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
正文:自定义滚动视图(
条子:[
SliverFixedExtentList(
项目范围:75,
委托:SliverChildListDelegate([
容器(
颜色:颜色,蓝色,
子项:文本(“1”),
),
容器(
颜色:颜色。粉红色,
子项:文本(“2”),
),
容器(
颜色:颜色,黄色,
儿童:文本(“3”),
),
容器(
颜色:颜色,红色,
儿童:文本(“4”),
),
]),
),
makeHeader(“5号集装箱”),
SliverFixedExtentList(
项目范围:75,
委托:SliverChildListDelegate([
容器(
颜色:颜色。琥珀色,
儿童:文本(“6”),
),
容器(
颜色:颜色,蓝色,