How can i globally handle a user interaction? - flutter

I want to handle a user interaction globally.
what solution is best for it ?

Copy and paste following code in init state your app's base view that has implemented GestureBinding.
#override
void initState() {
GestureBinding.instance.pointerRouter.addGlobalRoute(_globalUserInteractionHandler);
super.initState();
}
_globalUserInteractionHandler(PointerEvent event) {
print("user interaction detected. $event");
}

One way of handling actions anywhere in the app is by using provider. This guide has a complete sample on app state management using provider.

Related

Why should I use Provider in Mobx Flutter?

The official Mobx documentation for Flutter says that in order to transfer data correctly, you must use a Provider and refer to the context to retrieve the data.
But why can't I just call the Mobx class at the root of the application and access the global variable to get the data?
CbtStore cbt = CbtStore();
void main() async {
runApp(const MyApp());
}
Why should I be doing this?
void main() async {
runApp(MultiProvider(
providers: [
Provider<CbtStore>(create: (_) => CbtStore()),
],
child: MyApp()));
}
And how do I refer to Mobx inside the widget methods in that case, for example, if I want to call the action in the Mobx class in initState method? Now I do it in the following way. But when using Provider in initState there is no context.
#override
void initState() {
cbt.init();
super.initState();
}
Provider is used only for dependency injection with mobx. It is not used for state changes.
Now when you are using mobx you don't need a stateful widget in most cases because you are handling your state changes inside your mobx store and if there is any changes in the state we use Observer to change ui.
if want something to initialise with the screen than prefer using constructor of mobx class rather then initState.
for example,
class MyStore = _MyStore with _$MyStore;
abstract class _MyStore with Store {
_MyStore(){
getData();
}
}
Now don't use global providers for your all of store. Only Initialise a provider whenever you need it. So when you push a route wrap it with a provider so that Provider.of(context); can find it. Only use global store if it required globally.
You mentioned creating an instance of store to use it. When you initialise a store in stateless widget it, the data will get destroyed when you close the screen and when you reopen it everything will start all over again. It is useful when you don't need to maintain state after screen pops. It will based on your use case.
You should do what works best for your use case.
The reason why providers are useful is that they can be provided where needed. This could be in the application root, but also somewhere deeper in the widget tree.
Another advantage of the providers is that you can have a provider that notifies listeners. Widgets will rebuild automatically in this case, which can be useful if you have stored and need data to update everywhere in the application.
The initState does indeed not allow the use of providers directly. There are 3 solutions for this:
Don't have the provider listing (Provider.of(context, listen: false); This allows you to use the methods, but not listen to changes.
Use the provider in the build method, using the consumer.
I am by no means an expert on flutter, but this is just what I have experienced so far.

Flutter bloc with calling api on an interval

I'm working on an app that accesses two api's that contain location data that updates regularly (every 5 seconds or so). I want to utilize flutter and flutter_bloc to manage my state, but I'm not sure where the interval would come into play. I understand bloc and how the ui interacts with it with BlocBuilder and BlocProvider, as well as providing a repository that handles the api calls. What I'm not sure about is where to put the interval/timer. My idea was to do a normal bloc setup:
class DataBloc extends Bloc<DataEvent, DataState> {
//constructor
#override
Stream<DataState> mapEventToState(DataEvent event) async* {
if (event is FetchData) {
var data = repository.getData();
yield* _mapFetchDataToState(data);
}
}
}
In the ui:
class MainPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
final dataBloc = BlocProvider.of<DataBloc>(context);
Timer.periodic(fiveSeconds, (Timer t) => dataBloc.add(FetchData()));
return ui stuff
}
}
But I'm not sure if this is the right way to leverage BLoCs and/or timers. But I basically need to call the api on an interval and then have the data update in my ui without a full refresh of all the widgets. They're going to be icons on a google map, and I want the icons to update their locations when the data I pull is updated. How can I accomplish this? For reference, I've looked at the flutter_bloc docs for the timer app here and I thought it was a little overkill and that my idea above was simpler, just not sure if this is the right way. I'm pretty new to flutter so any help would be appreciated.
I'd recomend not to put initializing code inside build. Make it a StatefulWidget and add a dispose method to clear the timer. And inside initState setup the timer.
There's no right way in programming. There are best practices, so as long as it work for your use case is ok.
I would either put the timer into the bloc/cubit because its part of your logic and not of your ui.
Or if you use a repository I would probably use a stream listen to it in your bloc and periodically update the stream in your repo.
But any better suggestions are more than welcome, because I am also looking for some kind of 'the way to do'...

Finish geolocation in Flutter

How can I end the real-time geolocation service when I close the activity and move on to another one, for example MapReceiver to AppHome, since when I do so the GPS is still active and continues to update my database in Firebase realtime until I close the app completely
You can use dispose method when you leave the page or if you want to close the service. Note that dispose method is only supported inside Stateful widget only.
#override
void dispose(){
super.dispose();
// write code to close your streams,services, etc. here
}

Flutter: How to track app lifecycle without a widget

I'm involved with the FlutterSound project which is shipped as a package containing an api that, for the purposes of this question, doesn't contain a widget.
The api needs to handle events when the application changes its state (AppLifecycleState.pause/resume). (we need to stop/resume audio when the app is paused/resumed).
I can see how to do this in a widget using WidgetsBindingObserver but the api needs this same info without having to rely on a widget.
The SchedulerBinding class has a method handleAppLifecycleStateChanged which seems to provide the required info but its unclear how to implement this outside of a widget.
Below is a code sample that can listen to AppLifecycleState change events without directly involving a widget:
import 'package:flutter/material.dart';
class MyLibrary with WidgetsBindingObserver {
AppLifecycleState _state;
AppLifecycleState get state => _state;
MyLibrary() {
WidgetsBinding.instance.addObserver(this);
}
/// make sure the clients of this library invoke the dispose method
/// so that the observer can be unregistered
void dispose() {
WidgetsBinding.instance.removeObserver(this);
}
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
this._state = state;
}
void someFunctionality() {
// your library feature
}
}
Now you can instantiate the library in a flutter widget, for instance, from which point it will start listening to any change in AppLifecycleState.
Please note that, the above code doesn't take care of redundant bindings. For example, if the client of your library is meant to initialize the library more than once in different places, the didChangeAppLifecycleState() method will be triggered multiple times per state change (depending on the number of instances of the library that were created). Also I'm unsure whether the solution proposed conforms to flutter best practices. Nevertheless it solves the problem and hope it helps!

How to make a flutter app react to network changes?

I have this method, in one of my classes, that registers a listener to watch for connectivity changes:
static _watchNetworkChanges() {
Connectivity().onConnectivityChanged.listen((status){
if (status == ConnectivityResult.wifi) {
// Do something...
}
});
}
But apparently, the closure passed to the listen method is never called, no matter what I try (enable/disable wi-fi, mobile network, etc.)
The Connectivity class is part of this lib:
http://47.75.70.92/documentation/connectivity/0.4.1/
Does anyone know how to make this work?
EDIT:
I found out that this listener works if it is written inside an initState() method of a StatefulWidget. But that doesn't help me, because I'm writing a library and therefore I will have no widgets inside it.