Using the same automated dart signal in multiple widgets #505
2bndy5
started this conversation in
Show and tell
Replies: 1 comment 4 replies
-
Using inherited state management with |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I needed a way to use the same signal stream in multiple widgets to dynamically adjust the app theme's colors.
The Problem
Rinf docs do a great job explaining how to use signals to interact (call and respond) with rust. However, if you want to use the same exact response signal in multiple flutter widgets, a multitude of the same exact automated call signals (to instigate the response) can have undesired side effects throughout the entire widget tree.
Consider a root widget that uses dart signals to display the app's colors and maybe some other state(s) all managed in rust. Now imagine a child widget that uses the same signal to only display and change the app's state of colors. When the root widget updates the colors, it will also have to update any other state it gets from rust, and the subsequent widget tree is rebuilt (including the child widget that manipulates the colors). In my experience, this results in a infinite loop of calls-and-responses (evident in
debug_print!()
outputs).So, how do we extend the data from 1 stream of an automated dart signal into the scope of child widgets? We could pass the stream's data into constructors of child widgets, but that ends up with bloated code and memory consumption, especially if a grandchild needs the data but not it's immediate parent.
The Solution
Use the provider (flutter favorite) plugin to propagate 1 response from rust into multiple widgets (children of the StreamProvider widget).
Note
It might be possible to instigate a response from a
StatefulWidget.initState()
orStatelessWidget.build()
, but this can lead to a bloated queue of signals that both dart and rust have to contend.The following code snippets might be a little rough around the edges, but it works as expected.
Here is a sample project: rinf_provider.zip
Third-party dependencies (other than
rinf
)Flutter
Rust
In flutter
Let app settings propagate up the widget tree from
lib/main.dart
Warning
In the above dart example, I had to manually instantiate a
RustSignal<T>
as a default value to theStreamProvider.initialData
field. Developers should beware the difference between[DART-SIGNAL-BINARY]
and[DART-SIGNAL]
! In the solution provided, I never use thebinary
value of a dart signal. Thus, I was able to useUint8List(0)
as a placeholder until the stream actually received data.And change the color settings from a child widget in
lib/setting/global.dart
In ProtoBuf
I declared the signals to communicate app settings in messages/settings.proto
In Rust
I used the recommended Actor model to handle signals in
native/hub/src/settings.rs
And I manage the cache in
native/hub/src/cache.rs
Beta Was this translation helpful? Give feedback.
All reactions