gpt4 book ai didi

listview - 使用新项目在列表中保持位置 Flutter ListView

转载 作者:IT王子 更新时间:2023-10-29 07:04:20 26 4
gpt4 key购买 nike

我正在构建一个 Flutter 应用程序,但遇到了 ListView.builder 的问题。

我当前的设置是一个 StreamBuilder,它有一个对象列表。每个对象都绑定(bind)到 ListView.builder 中的 1 个项目。

我的问题是,当添加新项目并调用 StreamController$add() 时,列表会自动滚动到列表顶部。我正在寻找要维护的列表位置,滚动到顶部取决于用户(或按钮)。

我已将键添加到列表项中,这对其他问题有帮助,但对这个问题没有帮助。我还在 ListView 中添加了 PageStorageKey 但这没有帮助。我也有一个滚动 Controller ,但无法弄清楚如何处理这种行为。

如果需要,我可以提供代码示例。提前致谢!

相关代码:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: albatrossDarkTheme,
home: TimelinePage(),
);
}
}

class TimelinePage extends StatefulWidget {
TimelinePage({Key key}) : super(key: key);

@override
_TimelinePageState createState() => _TimelinePageState();
}

class _TimelinePageState extends State<TimelinePage> {
AlbatrossClient _client;
GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
PageStorageKey _pageKey = PageStorageKey(8);
TimelineDatabase _database = TimelineDatabase();
StreamController<List<Tweet>> _controller;
ScrollController _scrollController;

Stream<List<Tweet>> _getStream() {
return _controller.stream;
}

Future<void> _refreshTimeline() async {
_client.refreshTimeline().then((result) {
if (result == 0)
_database.getTimeline(false).then((update) {
_controller.add(update);
});
});
}

_updateTimeline() async {
_database.getTimeline(false).then((update) => _controller.add(update));
}

@override
void initState() {
_client = AlbatrossClient();
_controller = StreamController<List<Tweet>>();
_scrollController = ScrollController(keepScrollOffset: true);
_database.getTimeline(false).then((saved) => _controller.add(saved));
_client.refreshTimeline().then((result) {
if (result == 0)
_database.getTimeline(false).then((update) {
_controller.add(update);
});
});

super.initState();
}

@override
void dispose() {
_controller.close();
_scrollController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: SearchAppbar(() {
_scaffoldKey.currentState.openDrawer();
}),
drawer: Drawer(
child: DrawerMain.getDrawer(context, _client.getProfile()),
),
body: Container(
child: RefreshIndicator(
onRefresh: _refreshTimeline,
child: StreamBuilder<List<Tweet>>(
stream: _getStream(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
key: _pageKey,
controller: _scrollController,
itemCount:
snapshot.data != null ? snapshot.data.length : 0,
itemBuilder: (context, index) {
return RowTweet(
tweet: snapshot.data[index],
update: _updateTimeline,
animate: true);
},
);
} else
return Container(
alignment: Alignment(0.0, 0.0),
child: CircularProgressIndicator(),
);
},
))),
floatingActionButton: FloatingActionButton(
tooltip: 'Compose Tweet',
onPressed: _composeTweet,
foregroundColor: Colors.white,
child: Image.asset(
"icons/ic_tweet_web.webp",
width: 38,
height: 38,
),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}

这是我的新尝试:

return Scaffold(
key: _scaffoldKey,
appBar: SearchAppbar(() {
_scaffoldKey.currentState.openDrawer();
}),
drawer: Drawer(
child: DrawerMain.getDrawer(context, _client.getProfile()),
),
body: Container(
child: RefreshIndicator(
onRefresh: _refreshTimeline,
child: _timeline.isNotEmpty
? ListView.builder(
key: _pageKey,
controller: _scrollController,
itemCount: _timeline.length,
itemBuilder: (context, index) {
return RowTweet(
tweet: _timeline[index],
update: _updateTimeline,
animate: true);
})
: Container(
alignment: Alignment(0, 0),
child: CircularProgressIndicator(),
))),
floatingActionButton: FloatingActionButton(
tooltip: 'Compose Tweet',
onPressed: _composeTweet,
foregroundColor: Colors.white,
child: Image.asset(
"icons/ic_tweet_web.webp",
width: 38,
height: 38,
),
), // This trailing comma makes auto-formatting nicer for build methods.
);

最佳答案

因此,每次通过流发出新值时,都会重建整个 ListView 。解决此问题的方法是将 ListView 移出流生成器,并仅修改流生成器中 ListView 的子项。

我之前没有广泛使用过 StreamBuilder,因此您必须在代码中弄明白。有两种方法可以解决此问题。

  1. 在成员变量中保存一个小部件列表,如果列表为空则返回您的指示器,否则返回一个 ListView(_yourMemberListOfWidgets)。在没有流生成器的情况下连接到流。当返回一个新值时,将其设置为作为成员变量保存的小部件列表,并调用 setState 来更新您的状态。

  2. 将您的 Stream 构建器移动到 ListView 中,编译并仅返回子项。如果那不可能,那么请考虑将 StreamBuilder 放在 ScrollView 中并返回一个 Column,其中包含所有子项作为小部件。这将产生相同的效果。如果您需要触摸输入,只需在列表项周围使用 GestureDetector 并实现 onTap。

关于listview - 使用新项目在列表中保持位置 Flutter ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54798473/

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