简体   繁体   English

如何从 Flutter 中的另一个小部件滚动小部件

[英]How to scroll a widget from another widget in Flutter

The right way to interact between widgets in flutter is Provider. flutter 中的小部件之间交互的正确方法是提供程序。 But Provider manages a data, but with just scrolling, no data is supposed to be changed.但是 Provider 管理数据,但只是滚动,不应该更改任何数据。 How is it possible to send a scroll command (like jumpTo) between widgets placed on same level?如何在放置在同一级别的小部件之间发送滚动命令(如 jumpTo)? (not like parent\child relation) (不像父\子关系)

For example: there is a button that adds a new item to the ListView with provider like that:例如:有一个按钮向 ListView 添加一个新项目,提供者如下:

listProvider.createItem("New item");

And the list should be scrolled down to the end after that.之后列表应该向下滚动到最后。
How to do that?怎么做?

Widget A{

@override build (ctx){
return B(_scrollController) ;

}


Widget B{
ScrollController scrollController;
B{this._scrollController}

}

Use this right after listProvider.createItem("New item") to scroll to bottom of listview.listProvider.createItem("New item")之后使用它来滚动到列表视图的底部。 _scrollController.animateTo(_scrollController.position.maxScrollExtent)

Assuming you have access to the scrollController provided to the listView: You can call scrollController.position.moveTo(scrollcontroller.position.maxScrollExtent) after you've added the element to the ListView.假设您可以访问提供给 listView 的 scrollController:您可以在将元素添加到 ListView 后调用scrollController.position.moveTo(scrollcontroller.position.maxScrollExtent)

You can also add a duration to moveTo to get to scroll down more naturally.您还可以向 moveTo 添加持续时间,以便更自然地向下滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM