Flutter Wifi

Flutter Wifi:

This Flutter Wifi  plugin allows Flutter apps to get wifi ssid and its  list, connect wifi with ssid and password. After Connecting Wifi we can exchange data over the network like sending files, sms  message, using whatsapp etc. This plugin tested on Android and works better on it.

Use the below code to check the current Wifi  status of the device.

import 'package:wifi/wifi.dart';

String ssid = await Wifi.ssid;

String ip = await Wifi.ip;

var result = await Wifi.connection('ssid', 'password');

// only work on Android.
List<String> ssidList = await Wifi.list('key'); // this key is used to filter

For IOS:

To test this on IOS  connection on iOS ( use iOS 11 only) then only it will work.

‘build Phass’ -> ‘Link Binay With Libraries’ add ‘NetworkExtension.framework’

 

In IOS device  Open  ‘Capabilities’  and then open ‘Hotspot Configuration’  set it ON

 

Using WIFI Functionality In Flutter:

To use this Wifi Functionality we have to add its dependency package into the pubspec.yaml file:

use below code to add dependency package in pubspec.yaml file.

dependencies:

wifi: ^0.0.10

 

Install Wifi dependency package from terminal Editor:

You can install packages from the command line with Flutter by using below command:

$ flutter packages get

Bydefault, your Flutter terminal editor might support flutter packages get.

 

Importing wifi in flutter:

To use wifi in flutters Dart code we have to import into the dart code for coding. without importing wifi if we use it in coding then it shows error or exception that is package not found error.

In Dart code use below code to import the package.

import 'package:wifi/wifi.dart';

 

 

Below is the Example of  Flutter WIFI :

Create a new Flutter project  and the copy and paste below code into example/lib/main.dart file:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wifi/wifi.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Wifi',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
String _wifiName = 'click button to get wifi ssid.';
String _ip = 'click button to get ip.';
List ssidList = [];
String ssid = '', password = '';

@override
void initState() {
super.initState();
loadData();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Wifi'),
centerTitle: true,
),

body: SafeArea(
child: ListView.builder(
padding: EdgeInsets.all(8.0),
itemCount: ssidList.length + 1,
itemBuilder: (BuildContext context, int index) {
return itemSSID(index);
},
),
),
);
}

Widget itemSSID(index) {
if (index == 0) {
return Column(
children: [
Row(
children: <Widget>[
RaisedButton(
child: Text('ssid'),
onPressed: _getWifiName,
),
Text(_wifiName),
],
),
Row(
children: <Widget>[
RaisedButton(
child: Text('ip'),
onPressed: _getIP,
),
Text(_ip),
],
),
TextField(
decoration: InputDecoration(
border: UnderlineInputBorder(),
filled: true,
icon: Icon(Icons.wifi),
hintText: 'Your wifi ssid',
labelText: 'ssid',
),
keyboardType: TextInputType.text,
onChanged: (value) {
ssid = value;
},
),
TextField(
decoration: InputDecoration(
border: UnderlineInputBorder(),
filled: true,
icon: Icon(Icons.lock_outline),
hintText: 'Your wifi password',
labelText: 'password',
),
keyboardType: TextInputType.text,
onChanged: (value) {
password = value;
},
),
RaisedButton(
child: Text('connection'),
onPressed: connection,
),
],
);
} else {
return Column(children: <Widget>[
ListTile(
leading: Icon(Icons.wifi),
title: Text(
ssidList[index - 1],
style: TextStyle(
color: Colors.black87,
fontSize: 16.0,
),
),
dense: true,
),
Divider(),
]);
}
}

void loadData() {
Wifi.list('').then((list) {
setState(() {
ssidList = list;
});
});
}

Future<Null> _getWifiName() async {
String wifiName = await Wifi.ssid;
setState(() {
_wifiName = wifiName;
});
}

Future<Null> _getIP() async {
String ip = await Wifi.ip;
setState(() {
_ip = ip;
});
}

Future<Null> connection() async {
Wifi.connection(ssid, password).then((v) {
print(v);
});
}
}

Leave a Reply

Categories