Dart 滑动应用条内容在滚动时消失
当AppBar重新设置动画时(在滚动条到达顶部后),SliveAppBar无法显示内容 包括下面的示例项目,以演示该问题。复制/粘贴下面的代码(将rxdart依赖项添加到pubspec.yaml)并运行应用程序。请注意,SliveAppBar包含一些来自流的文本(请参见下面的函数1和2)。现在将计数器列表向下滚动到足够远的位置,以使滑动条消失。滚动备份以将AppBar恢复到视图中,注意现在有两个ProgressIndicator,但没有文本 当AppBar再次出现在视图中时,要求文本出现,理想情况下无需再次调用以下函数(这些实际上是真实应用程序中的api调用): 滑动条内容函数Dart 滑动应用条内容在滚动时消失,dart,flutter,Dart,Flutter,当AppBar重新设置动画时(在滚动条到达顶部后),SliveAppBar无法显示内容 包括下面的示例项目,以演示该问题。复制/粘贴下面的代码(将rxdart依赖项添加到pubspec.yaml)并运行应用程序。请注意,SliveAppBar包含一些来自流的文本(请参见下面的函数1和2)。现在将计数器列表向下滚动到足够远的位置,以使滑动条消失。滚动备份以将AppBar恢复到视图中,注意现在有两个ProgressIndicator,但没有文本 当AppBar再次出现在视图中时,要求文本出现,理想情
bloc.fetchTestAppBarTxt1();
bloc.fetchTestAppBarTxt2();
示例应用程序
void main() => runApp(TestApp());
class TestApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
TestBloc bloc = TestBloc();
bloc.fetchTestTimeline();
bloc.fetchTestAppBarTxt1();
bloc.fetchTestAppBarTxt2();
return MaterialApp(
home: new Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
backgroundColor: Colors.blueGrey,
elevation: 0.0,
),
body: CustomScrollView(slivers: <Widget>[
SliverAppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.grey[400],
expandedHeight: 200.0,
floating: true,
snap: true,
flexibleSpace: FlexibleSpaceBar(
background: Column(
children: <Widget>[
Container(
padding: EdgeInsets.only(left: 2.0),
height: 200,
child: Column(
children: <Widget>[
StreamBuilder(
stream: bloc.testAppBarTxt1,
initialData: null,
builder: (BuildContext context,
AsyncSnapshot<String> snapshot) {
if (snapshot.data == null)
return buildProgressIndicator(true);
return Expanded(
child: Text('${snapshot.data}'));
}),
StreamBuilder(
stream: bloc.testAppBarTxt2,
initialData: null,
builder: (BuildContext context,
AsyncSnapshot<String> snapshot) {
if (snapshot.data == null)
return buildProgressIndicator(true);
return Expanded(
child: Text('${snapshot.data}'));
}),
],
),
)
],
),
)),
timelineList(bloc)
])),
);
}
Widget timelineList(TestBloc bloc) {
return StreamBuilder(
stream: bloc.getTestTimeline,
initialData: null,
builder: (BuildContext context, AsyncSnapshot<List<int>> snapshot) {
List<int> val = snapshot.data;
if (val == null)
return SliverToBoxAdapter(child: buildProgressIndicator(true));
if (val.isNotEmpty) {
addToTimelineList(val, bloc);
return SliverList(
delegate: SliverChildListDelegate(new List<Widget>.generate(
bloc.listTest.length, (int index) {
if (index == bloc.listTest.length) {
return SliverToBoxAdapter(
child: buildProgressIndicator(bloc.isPerformingRequest));
} else {
return bloc.listTest[index];
}
})
));
}
});
}
void addToTimelineList(List<int> list, TestBloc bloc) {
for (var val in list) {
bloc.listTest.add(Text('$val'));
}
}
}
class TestBloc {
List<Text> listTest = new List<Text>();
bool isPerformingRequest = false;
final _testAppBarText1 = PublishSubject<String>();
Observable<String> get testAppBarTxt1 => _testAppBarText1.stream;
final _testAppBarText2 = PublishSubject<String>();
Observable<String> get testAppBarTxt2 => _testAppBarText2.stream;
final _testTimeline = PublishSubject<List<int>>();
Observable<List<int>> get getTestTimeline => _testTimeline.stream;
fetchTestTimeline() async {
List item = await Future.delayed(
Duration(seconds: 2), () => List<int>.generate(100, (i) => i));
_testTimeline.sink.add(item);
}
fetchTestAppBarTxt1() async {
String val = await Future.delayed(Duration(seconds: 2), () => "Text One");
_testAppBarText1.sink.add(val);
}
fetchTestAppBarTxt2() async {
String val = await Future.delayed(Duration(seconds: 2), () => "Text Two");
_testAppBarText2.sink.add(val);
}
dispose() {
_testAppBarText1.close();
_testAppBarText2.close();
_testTimeline.close();
}
}
void main()=>runApp(TestApp());
类TestApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
TestBloc bloc=TestBloc();
bloc.fetchTestTimeline();
bloc.fetchTestAppBarTxt1();
bloc.fetchTestAppBarTxt2();
返回材料PP(
家:新脚手架(
背景颜色:颜色。灰色[200],
appBar:appBar(
背景颜色:颜色。蓝灰色,
标高:0.0,
),
主体:自定义滚动视图(条子:[
滑杆(
自动嵌入:false,
背景颜色:颜色。灰色[400],
扩展高度:200.0,
浮动:是的,
是的,
flexibleSpace:FlexibleSpaceBar(
背景:专栏(
儿童:[
容器(
填充:仅限边缘设置(左侧:2.0),
身高:200,
子:列(
儿童:[
StreamBuilder(
流:bloc.testAppBarTxt1,
initialData:null,
生成器:(BuildContext上下文,
异步快照(快照){
如果(snapshot.data==null)
返回指示灯(真);
扩大回报(
子级:文本('${snapshot.data}');
}),
StreamBuilder(
流:bloc.testAppBarTxt2,
initialData:null,
生成器:(BuildContext上下文,
异步快照(快照){
如果(snapshot.data==null)
返回指示灯(真);
扩大回报(
子级:文本('${snapshot.data}');
}),
],
),
)
],
),
)),
时间清单(集团)
])),
);
}
Widget timelineList(TestBloc bloc){
返回流生成器(
流:bloc.getTestTimeline,
initialData:null,
生成器:(BuildContext上下文,异步快照){
List val=snapshot.data;
if(val==null)
返回SliveToboxAdapter(子项:buildProgressIndicator(true));
if(val.isNotEmpty){
addToTimelineList(val,集团);
返回银表(
委托:SliverChildListDelegate(新建List.generate(
bloc.listTest.length(int索引){
if(index==bloc.listTest.length){
回程滑动双轴适配器(
子级:buildProgressIndicator(bloc.isPerformingRequest));
}否则{
返回区列表测试[索引];
}
})
));
}
});
}
void addToTimelineList(列表列表,测试组){
for(列表中的var val){
添加(文本(“$val”);
}
}
}
类测试区{
List listTest=新列表();
bool isPerformingRequest=false;
最终的_testAppBarText1=PublishSubject();
可观测的get-testAppBarTxt1=>_-testAppBarTxt1.stream;
最终的_testAppBarText2=PublishSubject();
Observable get testAppBarTxt2=>\u testAppBarTxt2.stream;
最终_testTimeline=PublishSubject();
Observable get getTestTimeline=>\u testTimeline.stream;
fetchTestTimeline()异步{
列表项=等待未来。延迟(
持续时间(秒数:2),()=>List.generate(100,(i)=>i));
_testTimeline.sink.add(项);
}
fetchTestAppBarTxt1()异步{
String val=wait Future.delayed(持续时间(秒数:2),()=>“Text One”);
_testAppBarText1.sink.add(val);
}
fetchTestAppBarTxt2()异步{
String val=wait Future.delayed(持续时间(秒数:2),()=>“文本二”);
_testAppBarText2.sink.add(val);
}
处置{
_testAppBarText1.close();
_testAppBarText2.close();
_testTimeline.close();
}
}
这是因为每次您再次隐藏和显示小部件时,内部的StreamBuilder都会等待数据,而您没有使用initialData
要修复代码,您必须使用BehaviorSubject
和ValueObservable
,请检查这些修复:
final _testAppBarText1 = BehaviorSubject<String>();
ValueObservable<String> get testAppBarTxt1 => _testAppBarText1.stream;
final _testAppBarText2 = BehaviorSubject<String>();
ValueObservable<String> get testAppBarTxt2 => _testAppBarText2.stream;
StreamBuilder(
stream: bloc.testAppBarTxt1,
initialData: bloc.testAppBarTxt1.value,
StreamBuilder(
stream: bloc.testAppBarTxt2,
initialData: bloc.testAppBarTxt2.value,
final_testAppBarText1=BehaviorSubject();
ValueObservable获取testAppBarTxt1=>\u testAppBarTxt1.stream;
final _testAppBarText2=行为主体();
ValueObservable获取testAppBarTxt2=>\u testAppBarTxt2.stream;
StreamBuilder(
流:bloc.testAppBarTxt1,
initialData:bloc.testAppBarTxt1.value,
StreamBuilder(