DEV Community

Electrify-Services
Electrify-Services

Posted on

flutter open wifi setting

import 'package:app_settings/app_settings.dart';

I am using this plugin to open the wifi setting but was not able to open so anyone help how to open the Wi-Fi setting in the Flutter program

Future _checkAndRequestPermission(Permission permission) async {
var status = await permission.status;
print('Permission status: $status');
if (status.isDenied) {
print('Requesting permission...');
await permission.request();
}
}

Future _openAppSettings() async {
AppSettings.openWIFISettings();
}

Future _checkAndRequestPermissions() async {
// Check and request Wi-Fi permissions
await _checkAndRequestPermission(Permission.location);
await _checkAndRequestPermission(Permission.locationWhenInUse);

// Check if Wi-Fi is connected, if not, prompt user to turn on Wi-Fi
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.wifi) {
  // Wi-Fi is connected, proceed to scan for networks
  _scanWifiNetworks();
} else {
  // Wi-Fi is not connected, prompt user to turn on Wi-Fi
  bool shouldOpenWifiSettings = await showDialog(
    // Wi-Fi is not connected, prompt user to turn on Wi-F
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: const Text('Turn on Wi-Fi'),
        content:
            const Text('Please turn on Wi-Fi to connect to the network.'),
        actions: <Widget>[
          TextButton(
            child: const Text('Cancel'),
            onPressed: () {
              Navigator.of(context).pop(false);
            },
          ),
          TextButton(
            child: const Text('Open Settings'),
            onPressed: () {
              Navigator.of(context).pop(true);
            },
          ),
        ],
      );
    },
  );

  if (shouldOpenWifiSettings == true) {
    // Open Wi-Fi settings
    await _openAppSettings();
  }
}
Enter fullscreen mode Exit fullscreen mode

}

this function i am using

Top comments (0)