'Add text window before asking user to share his location

I want to display custom window with text before an user allows location sharing, when user denies to share location, I want to log him out. However I am having problem in this method, after pressing deny it doesn't proceed to the Navigator push unless I log out and login again then it keeps pushing me all the time to the login screen whenever I try to log in. How can I set this up properly?

What I want to achieve: https://www.youtube.com/shorts/PnJi98a2LDI

User logs in, the dialog appears, press OK, gets asked for location sharing, if he presses deny -> log him out.
If he logs in back again, prompt the window again.
If user has accepted location sharing, let him inside app and not push to login screen.
 _serviceEnabled = await _location.serviceEnabled();
    if (!_serviceEnabled) {
      print("service disabled");
      _serviceEnabled = await _location.requestService();
      print("requesting service");

      if (!_serviceEnabled) {

      }
    }

    print(_serviceEnabled);

    _permissionGranted = await _location.hasPermission();
    if (_permissionGranted == PermissionStatus.denied) {

      _permissionGranted = await _location.requestPermission();
      if (_permissionGranted != PermissionStatus.granted) {
        print("not granted");
       
        Navigator.pushNamed(context, '/login');

      }
    }

   await _location.getLocation();
if(_permissionGranted == PermissionStatus.denied)
   AlertDialog(
        title: const Text('Disclaimer'),
        content: SingleChildScrollView(
          child: ListBody(
            children: const <Widget>[
              Text('This is app collects location data.'),
            ],
          ),
        ),
        actions: [
          TextButton(
            child: const Text('OK'),
            onPressed: () {
            },
          ),
        ],
      ),


Solution 1:[1]

With the given details I will guess what I understood.

The issue occurs because, whenever you login, it will check for the permission. And the permission is already denied (or not granted) ; So while clicking OK button it will logout; as per the code.

So change the alert dialog with YES or NO buttons with title= "Do you want to grant permission?". If the user clicks NO : it should logout, If user clicks YES : it should navigate to the permission page of your device.

Solution 2:[2]

import 'package:flutter/material.dart';
import 'package:location/location.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: Login());
  }
}

class Login extends StatelessWidget {
  const Login({Key? key}) : super(key: key);
  static final formKey = GlobalKey<FormState>();
  static final _usernameController = TextEditingController();
  static final _passwordController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Login'),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.symmetric(horizontal: 24),
        child: Form(
          key: formKey,
          child: Column(
            children: [
              const SizedBox(height: 154.0),
              TextFormField(
                controller: _usernameController,
                decoration: InputDecoration(
                  hintText: 'Enter Login',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8.0),
                  ),
                ),
              ),
              const SizedBox(height: 28.0),
              TextFormField(
                controller: _passwordController,
                obscureText: true,
                decoration: InputDecoration(
                  hintText: 'Enter Password',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8.0),
                  ),
                ),
              ),
              const SizedBox(height: 225.0),
              ElevatedButton(
                child: const Text('Log in'),
                onPressed: () async {
                  Location location = Location();

                  bool _serviceEnabled;
                  PermissionStatus _permissionGranted;
                  LocationData _locationData;

                  _serviceEnabled = await location.serviceEnabled();
                  if (!_serviceEnabled) {
                    _serviceEnabled = await location.requestService();
                    if (!_serviceEnabled) {
                      return;
                    }
                  }

                  _permissionGranted = await location.hasPermission();
                  if (_permissionGranted == PermissionStatus.denied) {
                    print(_permissionGranted);
                    _permissionGranted = await location.requestPermission();
                    if (_permissionGranted == PermissionStatus.denied ||
                        _permissionGranted == PermissionStatus.deniedForever) {
                      print(_permissionGranted == PermissionStatus.denied ||
                          _permissionGranted == PermissionStatus.deniedForever);
                      showDialog(
                        context: context,
                        builder: (_) => AlertDialog(
                          title: const Text('Disclaimer'),
                          content: SingleChildScrollView(
                            child: ListBody(
                              children: const <Widget>[
                                Text('This is app collects location data.'),
                              ],
                            ),
                          ),
                          actions: [
                            TextButton(
                              child: const Text('OK'),
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                            ),
                          ],
                        ),
                      );
                      return;
                    }
                  }
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (_) => const HomePage()),
                  );

                  _locationData = await location.getLocation();
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _HomePagestate();
}

class _HomePagestate extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Stateful Widget'),
      ),
      body: const Center(
        child: Text('Hello World'),
      ),
    );
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ananda Krishnan
Solution 2 DiyorbekDev