Flutter listen to overlay entry insertion - flutter

How can I listen to an overlay that is inserted in flutter?
I have a main screen which consists of a scroll view and a button which opens an overlay.
When the overlay is opened I want to disable scroll on the below scroll view.
Ive tried using RouteAware but it is only executed when pushing another PageRoute.
The listeners are only executed when I push a new route using the main navigator but ive found no solution on how to listen when an overlay is pushed ontop of the current route.
Code:
class MyWidget extends StatelessWidget {
#override
_MyWidget State createState() => _MyWidget State();
}
class _MyWidgetState extends StatefulWidget<MyWidget> with RouteAware {
bool _scrollDisabled = false;
#override
didChangeDependencies() {
super.didChangeDependencies();
getIt<RouteObserver>().subscribe(this, ModalRoute.of(context) as PageRoute);
}
#override
dispose() {
getIt<RouteObserver>().unsubscribe(this);
super.dispose();
}
didPushNext() {
print("didPushNext");
setState(() {
_scrollDisabled = true;
});
}
didPush() {
print("didPush");
}
}

Related

How to dispose a list of text editing controllers in Flutter

I want to know what is the right way to dispose all text editing controllers in a list in flutter?
List<TextEditingController> controllers = [];
I tried this, but it's not working. What should be the correct approach for it?
dispose(){
for(TextEditingController controller in controllers){
controller.dispose();
}
}
You are trying to call dispose on your List<TextEditingController>and not on your TextEditingController. You need to change it inside your for loop body.
dispose() {
for(final TextEditingController controller in controllers){
controller.dispose();
}
}
You are calling on wrong variable:
#override
void dispose() {
for (TextEditingController controller in controllers) {
controller.dispose(); //<-- change this
}
super.dispose();
}

How can I make widgets inside a gridview change size while scrolling?

I have a gridview (specifically StaggeredGridView) and I want to make the children widgets change size while scrolling to get the effect that the widgets in the middle of the screen are the largest and as the user scrolls and the widget gets out of the screen it shrinks.
Is there a way to implement it or does such a layout already exist?
You need to use ScrollController.
In the code below, the value of ScrollPoint will change as you scroll. You can use its value to change the size.
class HomeScreen extends StatefulWidget {
HomeScreen({Key? key}) : super(key: key);
#override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final scrollController = ScrollController();
final dataKey = GlobalKey();
#override
void initState() {
scrollController.addListener(onScroll);
super.initState();
}
onScroll() {
setState(() {
scrollPoint = scrollController.offset;
});
print(scrollPoint);
}
#override
dispose() {
scrollController.removeListener(onScroll);
super.dispose();
}
double scrollPoint = 0.0;
#override
Widget build(BuildContext context) {
print(scrollPoint);
return Scaffold() }}

How to show alert when flutter app is paused?

How to show alert on screen when app is in background in flutter
or how to change state of flutter app from pause to resume
hi the answer is to use WidgetsBindingObserver.
in your app just add
class _MyScreenState extends State<MyScreen> with WidgetsBindingObserver{}
add the following inside our initState:
WidgetsBinding.instance.addObserver(this);
didChangeAppLifecycleState will be:
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.paused) {
// went to Background
}
if (state == AppLifecycleState.resumed) {
// came back to Foreground
}
}
here is the link of the full article link
I hope it will help you.

How to detect a Long press in a game with flutter/flame

I have a rect appearing in the game and I am able to detect taps like this
in main.dart
TapGestureRecognizer tapper = TapGestureRecognizer();
tapper.onTapDown = game.onTapDown;
flameUtil.addGestureRecognizer(tapper);
in game-controller.dart
void onTapDown(TapDownDetails d) {
if (mainMenu.tutorialBtnRect.contains(d.globalPosition)) {
mainMenu.tutorialTapped();
}
}
I can't seem to be able to get it to work with any of the LongPressGestures
Something about PrimaryButton and velocity I am not able to set
Please help
Thanks
It is easier to not use the GestureRecognizers and just implement it directly on your game.
class MyGame extends BaseGame with LongPressDetector {
MyGame();
#override
void onLongPressEnd(LongPressEndDetails details) {
if (mainMenu.tutorialBtnRect.contains(details.globalPosition)) {
mainMenu.tutorialTapped();
}
}
}

How to detect when flutter app comes back from background?

I would like to detect when flutter app comes back from background.
In other SDKs for crossApp development, there is usually a listener when app changes this state.
Is there anything similar in flutter?
class _AppState extends State<App> with WidgetsBindingObserver {
#override
void initState() {
super.initState();
//add an observer to monitor the widget lyfecycle changes
WidgetsBinding.instance!.addObserver(this);
}
#override
void dispose() {
//don't forget to dispose of it when not needed anymore
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
late AppLifecycleState _lastState;
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.resumed && _lastState == AppLifecycleState.paused) {
//now you know that your app went to the background and is back to the foreground
}
_lastState = state; //register the last state. When you get "paused" it means the app went to the background.
}
}