我想在 flutter 项目中显示一些弹出窗口,以便在每次打开应用程序时获取位置并禁用电池优化。
是否可以在不使用任何提升按钮或其他东西的情况下从第一秒开始显示弹出窗口?如果可以的话请指导我如何做到这一点。
我尝试使用 ElevatedButton 来显示此弹出窗口。但由于我想在应用程序打开时显示此弹出窗口,因此使用提升按钮的这项工作不适合我。
我使用此代码使用 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")
)
]
)
),
);
}
}
**Please replace this code.**
import 'package:flutter/material.dart';
import 'package:app_settings/app_settings.dart';
import 'package:location/location.dart';
void main() {
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> {
Location location = Location();
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_showLocationAndBatteryDialog();
});
}
Future<void> _showLocationAndBatteryDialog() async {
bool isLocationServiceEnabled = await location.serviceEnabled();
if (!isLocationServiceEnabled) {
bool shouldRequestLocation = await showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Enable Location Service'),
content: Text('Please enable location service to use this app.'),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context, true);
},
child: Text('Enable'),
),
TextButton(
onPressed: () {
Navigator.pop(context, false);
},
child: Text('Cancel'),
),
],
),
);
if (shouldRequestLocation) {
await location.requestService();
}
}
// Check battery optimization
// Add your logic to check battery optimization here
// If battery optimization is not disabled, show a dialog prompting the user to disable it
}
@覆盖 小部件构建(BuildContext上下文){ 返回脚手架( 应用栏:应用栏( title: Text("在 Flutter 中打开 GPS"), 背景颜色:Colors.redAccent, ), 主体:容器( 对齐方式:Alignment.center, 子项:列( 孩子们: [ 升高的按钮( onPressed: () 异步 { 等待_showLocationAndBatteryDialog(); }, child: Text("显示位置对话框"), ), ], ), ), ); } }