【发布时间】:2021-07-06 16:51:22
【问题描述】:
当消息流在列表视图中不断流动时,在阅读特定消息时继续向下滚动会干扰。有没有办法在阅读一些已经可用的流消息时避免自动滚动? 这是我试图在列表视图中显示流消息的代码
List<Comment> comments = <Comment>[];
StreamSubscription<Comment>? sub;
Future<void> redditmain() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
Reddit reddit = await Reddit.createScriptInstance(
clientId: cId,
clientSecret: cSecret,
userAgent: uAgent,
username: uname,
password: upassword, // Fake
);
sub = reddit.subreddit('cricket').stream.comments().listen((comment) {
if (comment != null) {
setState(() {
comments.insert(0, comment);
});
}
}) as StreamSubscription<Comment>?;
}
@override
Future<void> didChangeDependencies() async {
super.didChangeDependencies();
redditmain();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body:Center(
child: Container(
child: Scrollbar(
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: comments.length,
itemBuilder: (context, index) {
if (comments.isNotEmpty) {
final Comment comment = comments[index];
return Card(
elevation: 3,
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
color: getMyColor(comment
.data!["author_flair_richtext"].isNotEmpty
? comment.data!['author_flair_richtext'][0]['a']
: ''),
child: ListTile(
onTap: () {
_launchURL(comment.permalink);
},
trailing: Text(
comment.author,
style: TextStyle(color: text_color),
),
title: Text(
comment.body ?? '',
style: TextStyle(color: text_color),
),
subtitle: Text(comment.data?['link_title'] ?? '',
style: TextStyle(color: text_color)),
leading: Image.network(comment
.data!["author_flair_richtext"].isNotEmpty
? comment.data!['author_flair_richtext'][0]
['u'] ??
"https://png.pngtree.com/png-clipart/20190927/ourmid/pngtree-cricket-stumps-and-ball-png-image_1733735.jpg"
: "https://png.pngtree.com/png-clipart/20190927/ourmid/pngtree-cricket-stumps-and-ball-png-image_1733735.jpg"),
// Text(comment.data?['author_flair_css_class'] ?? ''),
),
);
} else
return Theme(
data: Theme.of(context)
.copyWith(accentColor: Colors.red),
child: new CircularProgressIndicator(),
);
},
),
),
),
));
}
这使得流以更高的速率连续流动,因此当我向下滚动阅读特定消息时,它会随着新的流消息进入而继续移动。 我想在不从传入流向下滚动页面的情况下阅读消息有没有办法实现这一点?对此的任何建议都会非常有帮助,期待您的解决方案。
编辑我尝试了@downgrade 给出的解决方案,并更新了我的代码,如下所示。最初,它加载消息流并滚动到屏幕底部显示。当我滚动到顶部时,所有列表视图都会变成一个圆形指示器,如 所示。
List<Comment> comments = <Comment>[];
StreamSubscription<Comment>? sub;
late StreamQueue<Comment?> commentQueue;
static const int commentLimit = 100;
Future<void> redditmain() async {
Reddit reddit = await Reddit.createScriptInstance(
clientId: cId,
clientSecret: cSecret,
userAgent: uAgent,
username: uname,
password: upassword, // Fake
);
setState(() {
commentQueue = StreamQueue<Comment?>(
reddit.subreddit('cricket').stream.comments(limit: commentLimit));
});
}
@override
Future<void> didChangeDependencies() async {
super.didChangeDependencies();
// getEmail();
redditmain();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: cId == null
? Container(
color: Colors.transparent,
child: Center(
child: CircularProgressIndicator(
valueColor:
AlwaysStoppedAnimation<Color>(Colors.deepPurpleAccent),
),
),
)
: Center(
child: Container(
child: Scrollbar(
child: ListView.builder(
reverse: true,
scrollDirection: Axis.vertical,
itemCount: commentLimit,
itemBuilder: (context, index) => FutureBuilder<Comment?>(
future: commentQueue.next,
builder:
(BuildContext ctx, AsyncSnapshot<Comment?> snap) {
final Comment? comment = snap.data;
if (comment == null) {
return Card(
elevation: 3,
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
child: ListTile(
title: CircularProgressIndicator(),
),
);
}
return Card(
elevation: 3,
// color: (index % 2 == 0) ? Colors.yellow : Colors.white,
semanticContainer: true,
clipBehavior: Clip.antiAliasWithSaveLayer,
// child:Image.network('https://placeimg.com/640/480/any',fit:BoxFit.fill),
color: getMyColor(comment!
.data!["author_flair_richtext"].isNotEmpty
? comment!.data!['author_flair_richtext'][0]['a']
: ''),
child: ListTile(
onTap: () {
_launchURL(comment.permalink);
},
trailing: Text(
comment.author,
style: TextStyle(color: text_color),
),
title: Text(
comment.body ?? '',
style: TextStyle(color: text_color),
),
subtitle: Text(comment.data?['link_title'] ?? '',
style: TextStyle(color: text_color)),
leading: Image.network(comment
.data!["author_flair_richtext"].isNotEmpty
? comment.data!['author_flair_richtext'][0]
['u'] ??
"https://png.pngtree.com/png-clipart/20190927/ourmid/pngtree-cricket-stumps-and-ball-png-image_1733735.jpg"
: "https://png.pngtree.com/png-clipart/20190927/ourmid/pngtree-cricket-stumps-and-ball-png-image_1733735.jpg"),
// Text(comment.data?['author_flair_css_class'] ?? ''),
),
);
},
),
),
),
),
),
);
}
【问题讨论】:
标签: android flutter listview dart stream