Tuesday, April 9, 2024
HomeiOS Developmentandroid - Displaying popup notifications to get location and disable battery optimization...

android – Displaying popup notifications to get location and disable battery optimization permission


**Please substitute this code.**


import 'package deal:flutter/materials.dart';
import 'package deal:app_settings/app_settings.dart';
import 'package deal:location/location.dart';

void major() {
 runApp(MyApp());
}

  class MyApp extends StatelessWidget {
  @override
  Widget construct(BuildContext context) {
  return MaterialApp(
   house: Residence(),
  );
 }
}

 class Residence extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
 }

 class _HomeState extends State<Residence> {
Location location = Location();

@override
void initState() {
tremendous.initState();
WidgetsBinding.occasion.addPostFrameCallback((_) {
  _showLocationAndBatteryDialog();
  });
}

  Future<void> _showLocationAndBatteryDialog() async {
   bool isLocationServiceEnabled = await location.serviceEnabled();
   if (!isLocationServiceEnabled) {
   bool shouldRequestLocation = await showDialog(
    context: context,
    builder: (context) => AlertDialog(
      title: Textual content('Allow Location Service'),
      content material: Textual content('Please allow location service to make use of 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();
  }
}

// Verify battery optimization
// Add your logic to examine battery optimization right here
// If battery optimization shouldn't be disabled, present a dialog 
 prompting the person to disable it
}

 @override
  Widget construct(BuildContext context) {
  return Scaffold(
  appBar: AppBar(
    title: Textual content("Flip On GPS in Flutter"),
    backgroundColor: Colours.redAccent,
  ),
  physique: Container(
    alignment: Alignment.middle,
    little one: Column(
      youngsters: [
        ElevatedButton(
          onPressed: () async {
            await _showLocationAndBatteryDialog();
          },
          child: Text("Show Location Dialog"),
          ),
        ],
      ),
    ),
  );
 }
} 



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments