Build connection between flame and flutter - flame

I have a flutter widget ProgressBar, which needs to be notified when a change happens to a value in Flame,
I followed a tutorial which is using overlays.notifyListeners(); to update the StatelessWidget progressBar, but after I updated to the newest Flame version (v1.6.0), this method can not be used anymore, what is the correct way of doing this now?

There are a few different ways to do this, either you can use state management libraries like flame_riverpod or flame_bloc to achieve this, or you can wrap your value in a ChangeNotifier/ValueNotifier and update the state once the value updates.

Related

what can prevent setstate from working correctly in flutter?

i am new to flutter in one of my project pages set state doesn't work at all no matter what I do I tried every almost everything every time doesn't work so what think can stop set state from working?
note: I am using provider and shared preference
setState(){} only works in StatefulWidget class, I think you are using StatelessWidget. If you want to use it change your class to StatefulWidget.
actually, nothing can stop it from working, and because you are new I am inviting you to accept that there is not something wrong with flutter but with your skill in flutter.
if you provide some code we might be able to help

How access to value of a variable in flutter package from other widget?

I wrote a package for Flutter and added it to the main.dart and it works well but How can I access the value of one of the variables in the package? or how return a variables from it ? For example, in the slider widget, we have access to the value of the slider with on change .
You want to learn how to manage state in flutter.
There are couple of good state management tools:
provider seems to be the easiest one.
Here is a nice tutorial on provider:
https://blog.codemagic.io/flutter-tutorial-app-arhitecture-beginners/

How to create custom flutter sdk widget, rebuild flutter and use new widget

I'm trying to figure out how to build a custom flutter sdk with a widget that I added.
background: I realized that the PaginatedDataTable requires a header (per this: https://github.com/flutter/flutter/issues/38604) and I'd like to update that widget to make it optional.
Github user wolfcro1984 has a pull request to do the same thing here: https://github.com/flutter/flutter/pull/26352/files
and I'd like to use that code in my current flutter version. However, I'm not sure how to actually build flutter again after that change so that I can use that widget.
How do you build the flutter sdk in order to make a custom flutter version? Is there documentation out there somewhere?
The best way is to copy the code in your own dart file and do the modification. You can go to paginated_data_table.dart, grab all the code and put it in your own .dart file.
How to remove the header :
Go to paginated_data_table.dart (equivalent)
Remove #required before 'header' parameter.
Remove assert(header != null)
Go to Widget build method and remove Semantics. This is under CARD section in comments.
I took a sample from here, did the modification and the result is :
The edits I made are here.
Note : You'll need to change the import part when you copy the standard code in your custom widget.

Const variables and instances storing

I just have read about "const" widget allocation in build method, so in this scenario, widgets get allocated when the app starts, how they stores and where?. Do they increase apps size?
How compiled Flutter widgets are stored and where?
Flutter application is AOT-compiled (ahead-of-time) to native platform code. Each Flutter widget is compiled to native widgets and they are rendered to Skia canvas which is shown to the end user platform. Platform sends back all events which are than handled by compiled Flutter code.
I do highly recommend to check those articles:
https://github.com/flutter/flutter/wiki/The-Engine-architecture
https://medium.com/47billion/flutter-how-does-it-works-6e4c73842e67
When to use const widgets in Flutter?
Use const widgets where possible in StatefullWidget
Why? Simply remember that calling StatefullWidget setState will rebuild the whole widget. By specifying const for the child widgets of StatefullWidget you'll say that they do not need to be rebuilt on setState call.
Here is the link to official documentation:
https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html#performance-considerations
Regarding whether they increase app size:
I believe Dart 2.0 made const optional, that is, the compiler will mark it as const on its own given the right context. Here are a few resources that point to this:
"Don't use const redundantly" - Effective Dart Usage
"Dart 2 introduces optional new and const" - Announcing Dart 2.0
So, if the compiler does this automatically, I think it's safe to assume that there won't be a compromise on app/size performance. I hope this answers your question.

How should I tell flutter that my app needs rebuilding externally?

I want my app to get an intent, and depending on the intent to display something in the app.
Normally, when a widget depends on state, you put it in a State and run setState().
The issue is that when I try starting my flutter app with different intents, I just get I/FlutterActivityDelegate( 4472): onResume setting current activity to this. And in a way it makes sense - I'm not saying anywhere in the flutter code that my widget needs repainting - since I get my intent through Java.
On the other hand, there should be a way to tell flutter to repaint my widget by intent? Or is there something else I should do?
Specifically to get notified about onResume event you can use WidgetsBindingObserver. Implement its didChangeAppLifecycleState method and respond to AppLifecycleState.resumed by doing whatever it is you want the UI to do, such as call setState to trigger rebuild. That said, there needs to be an effective change for the UI to be repainted. Simply calling setState with no effective state change may not result (in fact, should not) in actual UI changes.
In general, you can send a message from Java (or Objective-C/Swift) to Flutter using BasicMessageChannel and make your app react to the message (e.g. call setState or schedule frames).