data_strength 0.0.2 data_strength: ^0.0.2 copied to clipboard
To Execution on the Data strength for the wifi, mobile data and wifi speed.
import 'package:data_strength/data_strength.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int? _mobileSignal;
int? _wifiSignal;
int? _wifiSpeed;
String? _networkType;
String? _version;
final _internetSignal = DataStrength();
@override
void initState() {
super.initState();
_getPlatformVersion();
}
Future<void> _getPlatformVersion() async {
try {
_version = await _internetSignal.getPlatformVersion();
} on PlatformException {
if (kDebugMode) print('Error get Android version.');
_version = null;
}
setState(() {});
}
Future<void> _getInternetSignal() async {
int? mobile;
int? wifi;
int? wifiSpeed;
String? networkType;
try {
mobile = await _internetSignal.getMobileSignalStrength();
wifi = await _internetSignal.getWifiSignalStrength();
wifiSpeed = await _internetSignal.getWifiLinkSpeed();
networkType = await _internetSignal.getNetworkType();
} on PlatformException {
if (kDebugMode) print('Error get internet signal.');
}
setState(() {
_mobileSignal = mobile;
_wifiSignal = wifi;
_wifiSpeed = wifiSpeed;
_networkType = networkType;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Internet Signal Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('On Version: $_version \n'),
Text(
'Mobile signal: ${_mobileSignal ?? '--'} dBm ::${DataStrength.mobRangeName(_mobileSignal)}\n'),
Text(
'Wifi signal: ${_wifiSignal ?? '--'} dBm :: ${DataStrength.rangeName(_wifiSignal)}\n'),
Text('Wifi speed: ${_wifiSpeed ?? '--'} Mbps\n'),
Text('Network Type: ${_networkType ?? '--'} Mbps\n'),
ElevatedButton(
onPressed: _getInternetSignal,
child: const Text('Get internet signal'),
)
],
),
),
),
);
}
}