Skip to content

Commit

Permalink
Merge pull request #53 from jeroen1602/add_info_dialog_for_example
Browse files Browse the repository at this point in the history
Added info dialog to display the app version.
  • Loading branch information
jeroen1602 authored Apr 2, 2023
2 parents 183de8d + ebf01b4 commit c3a3acb
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 29 deletions.
6 changes: 6 additions & 0 deletions example/lib/business/bluetooth_business.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ class BluetoothBusiness {
///
/// Create a stream that combines the device returned from advertisements and
/// from normal device requests.
///
/// Will return `null` if bluetooth web is not supported.
///
static Stream<Set<MainPageDevice>>? createDeviceStream() {
if (!Bluetooth.isBluetoothAPISupported()) {
return null;
}
final List<AdvertisementReceivedEvent<AdvertisementBluetoothDevice>>
advertisementDevices = [];

Expand Down
13 changes: 11 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "package:flutter_web_bluetooth_example/pages/device_services_page.dart";
import "package:flutter_web_bluetooth_example/web_helpers/web_helpers.dart";
import "package:flutter_web_bluetooth_example/widgets/bluetooth_device_widget.dart";
import "package:flutter_web_bluetooth_example/widgets/floating_action_buttons.dart";
import "package:flutter_web_bluetooth_example/widgets/info_dialog.dart";

const redirect = bool.fromEnvironment("redirectToHttps", defaultValue: false);

Expand Down Expand Up @@ -42,8 +43,16 @@ class _MyAppState extends State<MyApp> {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const SelectableText("Bluetooth web example app"),
),
title: const SelectableText("Bluetooth web example app"),
actions: [
Builder(builder: (final BuildContext context) {
return IconButton(
onPressed: () async {
await InfoDialog.showInfoDialog(context);
},
icon: const Icon(Icons.info));
}),
]),
body: MainPage(
isBluetoothAvailable: available,
),
Expand Down
111 changes: 111 additions & 0 deletions example/lib/widgets/info_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import "dart:async";

import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:package_info_plus/package_info_plus.dart";
import "package:yaml/yaml.dart";

class InfoDialog extends StatefulWidget {
const InfoDialog({super.key});

@override
State<StatefulWidget> createState() {
return _InfoDialogState();
}

static Future<void> showInfoDialog(final BuildContext context) async {
await showDialog(
context: context,
builder: (final BuildContext dialogContext) {
return const InfoDialog();
});
}
}

class _InfoDialogState extends State<InfoDialog> {
Future<String>? _libraryVersion;
Future<String>? _appVersion;

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

// ignore: discarded_futures
_libraryVersion = _getLibraryVersion();
// ignore: discarded_futures
_appVersion = _getExampleAppVersion();
}

@override
Widget build(final BuildContext context) {
return AlertDialog(
title: const Text("Flutter web bluetooth"),
content: SingleChildScrollView(
child: ListBody(
children: [
FutureBuilder<String>(
future: _appVersion,
builder: (final BuildContext context,
final AsyncSnapshot<String> snapshot) {
return Text(
"Example app version: ${snapshot.data ?? "loading"}");
}),
FutureBuilder<String>(
future: _libraryVersion,
builder: (final BuildContext context,
final AsyncSnapshot<String> snapshot) {
return Text(
"Using library version: ${snapshot.data ?? "Loading"}");
}),
],
),
),
actions: <Widget>[
TextButton(
onPressed: () {
showLicensePage(
context: context,
applicationName: "Flutter web bluetooth example");
},
child: const Text("Licenses")),
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text("Close"),
)
],
);
}

Future<String> _getLibraryVersion() async {
final data = await rootBundle.loadString("pubspec.lock");

try {
final parsed = loadYaml(data);
final packages = parsed["packages"];
if (packages == null) {
throw ArgumentError();
}
final library = packages["flutter_web_bluetooth"];
if (library == null) {
throw ArgumentError();
}
final version = library["version"];
if (version == null) {
throw ArgumentError();
}
return version.toString();
} on YamlException {
return "Could not be loaded";
} on ArgumentError {
return "Could not be loaded";
}
}

Future<String> _getExampleAppVersion() async {
final info = await PackageInfo.fromPlatform();

return info.version;
}
}
56 changes: 56 additions & 0 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
sha256: a38574032c5f1dd06c4aee541789906c12ccaab8ba01446e800d9c5b79c4a978
url: "https://pub.dev"
source: hosted
version: "2.0.1"
flutter:
dependency: "direct main"
description: flutter
Expand Down Expand Up @@ -119,6 +127,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.15.2"
http:
dependency: transitive
description:
name: http
sha256: "6aa2946395183537c8b880962d935877325d6a09a2867c3970c05c0fed6ac482"
url: "https://pub.dev"
source: hosted
version: "0.13.5"
http_parser:
dependency: transitive
description:
name: http_parser
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
url: "https://pub.dev"
source: hosted
version: "4.0.2"
js:
dependency: transitive
description:
Expand Down Expand Up @@ -175,6 +199,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.0"
package_info_plus:
dependency: "direct main"
description:
name: package_info_plus
sha256: "8df5ab0a481d7dc20c0e63809e90a588e496d276ba53358afc4c4443d0a00697"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
package_info_plus_platform_interface:
dependency: transitive
description:
name: package_info_plus_platform_interface
sha256: "9bc8ba46813a4cc42c66ab781470711781940780fd8beddd0c3da62506d3a6c6"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -356,6 +396,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.3"
win32:
dependency: transitive
description:
name: win32
sha256: "5cdbe09a75b5f4517adf213c68aaf53ffa162fadf54ba16f663f94f3d2664a56"
url: "https://pub.dev"
source: hosted
version: "4.1.1"
yaml:
dependency: "direct main"
description:
name: yaml
sha256: "23812a9b125b48d4007117254bca50abb6c712352927eece9e155207b1db2370"
url: "https://pub.dev"
source: hosted
version: "3.1.1"
sdks:
dart: ">=2.18.1 <3.0.0"
flutter: ">=3.3.0"
9 changes: 5 additions & 4 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Demonstrates how to use the flutter_web_bluetooth plugin.
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 0.0.1+1
version: 12.0.0+12

environment:
sdk: ">=2.18.1 <3.0.0"
Expand All @@ -22,9 +22,11 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
package_info_plus: ^3.0.3
rxdart: ^0.27.7
url_launcher: ^6.1.10
web_browser_detect: ^2.0.3
yaml: ^3.1.1

dev_dependencies:
flutter_lints: ^2.0.1
Expand All @@ -43,9 +45,8 @@ flutter:
uses-material-design: true

# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
assets:
- ./pubspec.lock

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
Expand Down
26 changes: 3 additions & 23 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import "package:flutter/material.dart";
import "package:flutter_test/flutter_test.dart";

import "package:flutter_web_bluetooth_example/main.dart";

void main() {
testWidgets("Verify Platform version", (final WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());

// Verify that platform version is retrieved.
expect(
find.byWidgetPredicate(
(final Widget widget) =>
widget is SelectableText &&
widget.data!.startsWith("Bluetooth web example app"),
),
findsOneWidget,
);
test("At least one", () {
expect(true, true,
reason: "At least one test is required for the GithubRunner");
});
}

0 comments on commit c3a3acb

Please sign in to comment.