gpt4 book ai didi

dart - SliverAppBar 内容在滚动时消失

转载 作者:IT王子 更新时间:2023-10-29 06:45:38 25 4
gpt4 key购买 nike

当 AppBar 重新设置动画时(滚动条到达顶部后),SliverAppBar 无法显示内容。

下面包含一个示例项目来演示该问题。复制/粘贴下面的代码(将 rxdart 依赖项添加到 pubspec.yaml)并运行应用程序。请注意,SliverAppBar 包含一些来自流的文本(请参阅下面的函数 1 和 2)。现在向下滚动计数器列表足够远,以便 SliverAppBar 消失。滚动备份以将 AppBar 带回 View ,请注意现在有两个 ProgressIndicators 但没有文本。

要求文本在 AppBar 重新出现在 View 中时出现,理想情况下无需再次调用以下函数(这些实际上是真实应用程序中的 api 调用):

SliverAppBar 内容函数

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();
}
}

最佳答案

那是因为每次你隐藏和再次显示你的Widget时,里面的StreamBuilder都在等待数据,而你并没有使用initialData。

要修复您的代码,您必须使用 BehaviorSubjectValueObservable,检查这些修复:

          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,

关于dart - SliverAppBar 内容在滚动时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54225296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com