I want to show some popups in a fluttter project to get location and disable battery optimazation whenever I open the app.
Is it possible to show popup from the first second without using any elevated button or something else? If it is possible, then please guide me how to do this.
I tried and showed this popup using ElevatedButton. But as I want to show this popup at the opening of the app, so this work using elevated button is not appropriate for me.
I used this code for showing popup using ElevatedButton:
import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
import 'package:location/location.dart';
Future<void> main() async {
runApp(MyApp());
}
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}
class Home extends StatefulWidget{
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Turn On GPS in Flutter"),
backgroundColor: Colors.redAccent,
),
body: Container(
alignment: Alignment.center,
child: Column(
children:[
ElevatedButton(
onPressed: () async {
Location location = new Location();
bool ison = await location.serviceEnabled();
if (!ison) { //if defvice is off
bool isturnedon = await location.requestService();
if (isturnedon) {
print("GPS device is turned ON");
}else{
print("GPS Device is still OFF");
}
}
},
child: Text("Turn On GPS | Location Package")
),
ElevatedButton(
onPressed: () async {
AppSettings.openLocationSettings();
},
child: Text("Turn On GPS | App Setting Package")
)
]
)
),
);
}
}
2
Answers