How to set state from another widget?

前端 未结 2 1390
一个人的身影
一个人的身影 2020-12-02 11:25

I\'m trying to change the state from a different widget in Flutter. For example, in the following example I set the state after a few seconds.

Here is the code for

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 11:50

    Actually the most effective way to do this is using BLoC package in flutter and implement it from the top of the widget tree so all inheriting widgets can use the same bloc. If you have worked with Android before - it works like Android Architecture Components - you separate data and state management from the UI - so you do not setState in the UI, but instead use the block to manage state. So you can set and access the same data - from any widget that inherits from the top widget where the bloc is implemented, for more complex apps, it is very useful.

    This is where you can find the package: https://pub.dev/packages/flutter_bloc#-readme-tab-

    Write-up: https://www.didierboelens.com/2018/08/reactive-programming-streams-bloc/

    And a great tutorial on youtube https://www.youtube.com/watch?v=hTExlt1nJZI&list=PLB6lc7nQ1n4jCBkrirvVGr5b8rC95VAQ5&index=7

提交回复
热议问题