flutter_internet_speed_test 1.0.1 flutter_internet_speed_test: ^1.0.1 copied to clipboard
A Flutter plugin to test internet download and upload speed.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_internet_speed_test/flutter_internet_speed_test.dart';
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> {
final internetSpeedTest = FlutterInternetSpeedTest();
double downloadRate = 0;
double uploadRate = 0;
String downloadProgress = '0';
String uploadProgress = '0';
String unitText = 'Mb/s';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text('Progress $downloadProgress%'),
Text('Download rate $downloadRate $unitText'),
],
),
ElevatedButton(
child: const Text('Start Testing'),
onPressed: () {
internetSpeedTest.startTesting(
onDone: (TestResult download, TestResult upload) {
if (kDebugMode) {
print(
'the transfer rate ${download.transferRate}, ${upload.transferRate}');
}
setState(() {
downloadRate = download.transferRate;
unitText =
download.unit == SpeedUnit.Kbps ? 'Kb/s' : 'Mb/s';
downloadProgress = '100';
});
setState(() {
uploadRate = upload.transferRate;
unitText =
upload.unit == SpeedUnit.Kbps ? 'Kb/s' : 'Mb/s';
uploadProgress = '100';
});
},
onProgress: (double percent, TestResult data) {
if (kDebugMode) {
print(
'the transfer rate $data.transferRate, the percent $percent');
}
setState(() {
unitText =
data.unit == SpeedUnit.Kbps ? 'Kb/s' : 'Mb/s';
if (data.type == TestType.DOWNLOAD) {
downloadRate = data.transferRate;
downloadProgress = percent.toStringAsFixed(2);
} else {
uploadRate = data.transferRate;
uploadProgress = percent.toStringAsFixed(2);
}
});
},
onError: (String errorMessage, String speedTestError) {
if (kDebugMode) {
print(
'the errorMessage $errorMessage, the speedTestError $speedTestError');
}
},
);
},
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text('Progress $uploadProgress%'),
Text('Upload rate $uploadRate Kb/s'),
],
),
],
),
),
),
);
}
}