add_device_vcn 0.0.3 copy "add_device_vcn: ^0.0.3" to clipboard
add_device_vcn: ^0.0.3 copied to clipboard

add_device_vcn

example/lib/main.dart

import 'package:add_device_vcn/add_device_vcn.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';

// import 'add_device_vcn.dart'; // Đường dẫn tới file chứa class AddDeviceVcn

void main() {
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  bool _isWifiEnabled = false;
  String _currentSsid = 'Unknown';
  final TextEditingController _ssidController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _ssidPrefixController = TextEditingController();
  bool _isConnecting = false;
  String _connectionStatus = '';
  String _ssid = 'Unknown';
  @override
  void initState() {
    super.initState();
    initPlatformState();
    initPlatformSSSID();
  }

  Future<void> initPlatformSSSID() async {
    String ssid;
    try {
      ssid = await AddDeviceVcn.ssid ?? '';
    } on PlatformException {
      ssid = 'Failed to get ssid';
    }
    if (!mounted) return;

    setState(() {
      _ssid = ssid;
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    bool isWifiEnabled = false;
    String currentSsid = 'Unknown';

    try {
      platformVersion =
          await AddDeviceVcn.platformVersion ?? 'Unknown platform version';
      isWifiEnabled = await AddDeviceVcn.isEnabled;
      currentSsid = await AddDeviceVcn.ssid ?? 'Unknown SSID';
    } catch (e) {
      platformVersion = 'Failed to get platform version: $e';
      isWifiEnabled = false;
      currentSsid = 'Failed to get SSID: $e';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
      _isWifiEnabled = isWifiEnabled;
      _currentSsid = currentSsid;
    });
  }

  Future<void> _toggleWifi() async {
    setState(() {
      _isConnecting = true;
      _connectionStatus = 'Toggling WiFi...';
    });

    try {
      if (_isWifiEnabled) {
        await AddDeviceVcn.deactivateWifi();
      } else {
        await AddDeviceVcn.activateWifi();
      }

      // Refresh WiFi status
      bool isEnabled = await AddDeviceVcn.isEnabled;

      setState(() {
        _isWifiEnabled = isEnabled;
        _connectionStatus = isEnabled ? 'WiFi enabled' : 'WiFi disabled';
      });
    } catch (e) {
      setState(() {
        _connectionStatus = 'Error toggling WiFi: $e';
      });
    } finally {
      setState(() {
        _isConnecting = false;
      });

      // Refresh SSID after toggling
      _refreshSsid();
    }
  }

  Future<void> _connectToNetwork() async {
    final ssid = _ssidController.text;
    final password = _passwordController.text;

    if (ssid.isEmpty) {
      setState(() {
        _connectionStatus = 'Please enter an SSID';
      });
      return;
    }

    setState(() {
      _isConnecting = true;
      _connectionStatus = 'Connecting to $ssid...';
    });

    try {
      bool? result;
      if (password.isEmpty) {
        // Connect to open network
        result = await AddDeviceVcn.connect(ssid, saveNetwork: true);
      } else {
        // Connect to secure network
        result = await AddDeviceVcn.connectToSecureNetwork(
          ssid,
          password,
          saveNetwork: true,
        );
      }

      setState(() {
        _connectionStatus = result == true
            ? 'Successfully connected to $ssid'
            : 'Failed to connect to $ssid';
      });
    } catch (e) {
      setState(() {
        _connectionStatus = 'Error connecting to network: $e';
      });
    } finally {
      setState(() {
        _isConnecting = false;
      });

      // Refresh SSID after connection attempt
      _refreshSsid();
    }
  }

  Future<void> _connectByPrefix() async {
    final prefix = _ssidPrefixController.text;
    final password = _passwordController.text;

    if (prefix.isEmpty) {
      setState(() {
        _connectionStatus = 'Please enter an SSID prefix';
      });
      return;
    }

    setState(() {
      _isConnecting = true;
      _connectionStatus = 'Connecting to network with prefix $prefix...';
    });

    try {
      bool? result;
      if (password.isEmpty) {
        // Connect to open network by prefix
        result = await AddDeviceVcn.connectByPrefix(prefix, saveNetwork: true);
      } else {
        // Connect to secure network by prefix
        result = await AddDeviceVcn.connectToSecureNetworkByPrefix(
          prefix,
          password,
          saveNetwork: true,
        );
      }

      setState(() {
        _connectionStatus = result == true
            ? 'Successfully connected to network with prefix $prefix'
            : 'Failed to connect to network with prefix $prefix';
      });
    } catch (e) {
      setState(() {
        _connectionStatus = 'Error connecting to network: $e';
      });
    } finally {
      setState(() {
        _isConnecting = false;
      });

      // Refresh SSID after connection attempt
      _refreshSsid();
    }
  }

  Future<void> _disconnectFromNetwork() async {
    setState(() {
      _isConnecting = true;
      _connectionStatus = 'Disconnecting...';
    });

    try {
      bool? result = await AddDeviceVcn.disconnect();

      setState(() {
        _connectionStatus = result == true
            ? 'Successfully disconnected'
            : 'Failed to disconnect';
      });
    } catch (e) {
      setState(() {
        _connectionStatus = 'Error disconnecting: $e';
      });
    } finally {
      setState(() {
        _isConnecting = false;
      });

      // Refresh SSID after disconnection
      _refreshSsid();
    }
  }

  Future<void> _refreshSsid() async {
    try {
      String? ssid = await AddDeviceVcn.ssid;
      bool isEnabled = await AddDeviceVcn.isEnabled;

      setState(() {
        _currentSsid = ssid ?? 'Unknown';
        _isWifiEnabled = isEnabled;
      });
    } catch (e) {
      setState(() {
        _currentSsid = 'Error getting SSID: $e';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Add Device VCN Demo'),
        ),
        body: SingleChildScrollView(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('Platform: $_platformVersion'),
                      const SizedBox(height: 8),
                      Text('WiFi Enabled: $_isWifiEnabled'),
                      const SizedBox(height: 8),
                      Text('Current SSID: $_ssid'),
                      const SizedBox(height: 16),
                      ElevatedButton(
                        onPressed: _isConnecting ? null : _toggleWifi,
                        child: Text(
                            _isWifiEnabled ? 'Disable WiFi' : 'Enable WiFi'),
                      ),
                      const SizedBox(height: 8),
                      ElevatedButton(
                        onPressed: _isConnecting ? null : _refreshSsid,
                        child: const Text('Refresh Status'),
                      ),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 16),
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      const Text('Connect to Network',
                          style: TextStyle(fontWeight: FontWeight.bold)),
                      const SizedBox(height: 16),
                      TextField(
                        controller: _ssidController,
                        decoration: const InputDecoration(
                          labelText: 'SSID',
                          border: OutlineInputBorder(),
                        ),
                      ),
                      const SizedBox(height: 8),
                      TextField(
                        controller: _passwordController,
                        decoration: const InputDecoration(
                          labelText: 'Password (leave empty for open networks)',
                          border: OutlineInputBorder(),
                        ),
                        obscureText: true,
                      ),
                      const SizedBox(height: 16),
                      ElevatedButton(
                        onPressed: _isConnecting ? null : _connectToNetwork,
                        child: const Text('Connect'),
                      ),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 16),
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      const Text('Connect by Prefix',
                          style: TextStyle(fontWeight: FontWeight.bold)),
                      const SizedBox(height: 16),
                      TextField(
                        controller: _ssidPrefixController,
                        decoration: const InputDecoration(
                          labelText: 'SSID Prefix',
                          border: OutlineInputBorder(),
                        ),
                      ),
                      const SizedBox(height: 16),
                      ElevatedButton(
                        onPressed: _isConnecting ? null : _connectByPrefix,
                        child: const Text('Connect by Prefix'),
                      ),
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: _isConnecting ? null : _disconnectFromNetwork,
                child: const Text('Disconnect'),
              ),
              const SizedBox(height: 16),
              if (_isConnecting)
                const Center(child: CircularProgressIndicator()),
              const SizedBox(height: 8),
              Text(
                _connectionStatus,
                style: const TextStyle(fontWeight: FontWeight.bold),
                textAlign: TextAlign.center,
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _ssidController.dispose();
    _passwordController.dispose();
    _ssidPrefixController.dispose();
    super.dispose();
  }
}
0
likes
140
points
15
downloads

Publisher

unverified uploader

Weekly Downloads

add_device_vcn

Repository

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on add_device_vcn

Packages that implement add_device_vcn