Skip to content

Commit

Permalink
- [wifi_basic] class renamed to WiFiBasic, basic methods added
Browse files Browse the repository at this point in the history
- [wifi_basic,test] "basic test" added with mocked MethodCallHandler
- [wifi_basic,example] basic methods added to example app
  • Loading branch information
daadu committed Nov 28, 2021
1 parent 15dadec commit 05ccf12
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 41 deletions.
69 changes: 36 additions & 33 deletions packages/wifi_basic/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:wifi_basic/wifi_basic.dart';

void main() {
Expand All @@ -16,35 +14,12 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';

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

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion =
await WifiBasic.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}

// 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;
});
}
void showSnackBar(BuildContext context, String message) =>
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(message),
),
);

@override
Widget build(BuildContext context) {
Expand All @@ -54,7 +29,35 @@ class _MyAppState extends State<MyApp> {
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
child: Column(
children: [
TextButton(
child: const Text("WiFiBasic.hasCapability"),
onPressed: () async => showSnackBar(context,
"hasCapability: ${await WiFiBasic.hasCapability()}"),
),
TextButton(
child: const Text("WiFiBasic.isEnabled"),
onPressed: () async => showSnackBar(
context, "isEnabled: ${await WiFiBasic.isEnabled()}"),
),
Row(
children: [
const Text("WiFiBasic.setEnabled: "),
TextButton(
child: const Text("enable"),
onPressed: () async => await WiFiBasic.setEnabled(true,
shouldOpenSettings: true),
),
TextButton(
child: const Text("disable"),
onPressed: () async => await WiFiBasic.setEnabled(false,
shouldOpenSettings: true),
),
],
)
],
),
),
),
);
Expand Down
18 changes: 13 additions & 5 deletions packages/wifi_basic/lib/wifi_basic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import 'dart:async';

import 'package:flutter/services.dart';

class WifiBasic {
class WiFiBasic {
static const MethodChannel _channel = MethodChannel('wifi_basic');

static Future<String?> get platformVersion async {
final String? version = await _channel.invokeMethod('getPlatformVersion');
return version;
}
static Future<bool> hasCapability() async =>
await _channel.invokeMethod('hasCapability');

static Future<bool> isEnabled() async =>
await _channel.invokeMethod('isEnabled');

static Future<void> setEnabled(bool state,
{bool shouldOpenSettings = false}) async =>
await _channel.invokeMethod('setEnabled', {
"state": state,
"shouldOpenSettings": shouldOpenSettings,
});
}
20 changes: 17 additions & 3 deletions packages/wifi_basic/test/wifi_basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,30 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized();

setUp(() {
bool isEnabled = true;
channel.setMockMethodCallHandler((MethodCall methodCall) async {
return '42';
switch(methodCall.method) {
case "hasCapability":
return true;
case "isEnabled":
return isEnabled;
case "setEnabled":
isEnabled = methodCall.arguments["state"];
break;
default:
throw MissingPluginException();
}
});
});

tearDown(() {
channel.setMockMethodCallHandler(null);
});

test('getPlatformVersion', () async {
expect(await WifiBasic.platformVersion, '42');
test('basic test', () async {
expect(await WiFiBasic.hasCapability(), true);
expect(await WiFiBasic.isEnabled(), true);
await WiFiBasic.setEnabled(false);
expect(await WiFiBasic.isEnabled(), false);
});
}

0 comments on commit 05ccf12

Please sign in to comment.