我正在嘗試在 Twitter 上發布帖子,每個帖子都是一個單獨的專案,但它們都像大單卡一樣,沒有分開。我對 Flutter 有點陌生,不擅長解釋,但我希望你明白了。
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: _writePost,
tooltip: 'Increment',
child: Icon(Icons.create, color: Colors.grey[300]),
),
body: SizedBox(
height: 500,
child:
StreamBuilder<List<Post>>(
initialData: const [],
stream: _socketStream.stream,
builder: (context, snapshot) {
if (_isLoading) {
return const Center(
child: CircularProgressIndicator(),
);
}
return Card(child: ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: [
...snapshot.data!.map<Widget>(
(post) => Padding(
key: ValueKey(post.id),
padding: const EdgeInsets.symmetric(vertical: 10),
child: ListTile(
title: Text(
post.content,
style: const TextStyle(fontSize: 20),
),
trailing: MaterialButton(
onPressed: () {
_deletePost(post.id);
},
child: const Icon(
Icons.delete,
size: 30,
),
),
),
),
)
],
) );
},
),
));
}
外觀示例:

我試圖找出檔案中的錯誤,但是......如果您需要更多代碼或解釋,請寫下。如果可以的話請幫助我<3
uj5u.com熱心網友回復:
您需要添加資料的每個映射項Card而不是 parent ListView。像這樣的東西:
return ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
children: [
...snapshot.data!.map<Widget>(
(post) => Padding(
key: ValueKey(post.id),
padding: const EdgeInsets.symmetric(vertical: 10),
child:
// Use card here.
Card(child: ListTile(
title: Text(
post.content,
style: const TextStyle(fontSize: 20),
),
trailing: MaterialButton(
onPressed: () {
_deletePost(post.id);
},
child: const Icon(
Icons.delete,
size: 30,
),
),
),
),
),
)
],
);
uj5u.com熱心網友回復:
你可以試試 ListView.separated
uj5u.com熱心網友回復:
嘗試:
List.generate(lentgh, (index) {
return Text("$index"); // custom widget
}),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/449665.html
下一篇:打開/關閉過濾器選單
