|
| 1 | +import "dart:async"; |
| 2 | + |
| 3 | +import "package:flutter/material.dart"; |
| 4 | +import "package:flutter/services.dart"; |
| 5 | +import "package:package_info_plus/package_info_plus.dart"; |
| 6 | +import "package:yaml/yaml.dart"; |
| 7 | + |
| 8 | +class InfoDialog extends StatefulWidget { |
| 9 | + const InfoDialog({super.key}); |
| 10 | + |
| 11 | + @override |
| 12 | + State<StatefulWidget> createState() { |
| 13 | + return _InfoDialogState(); |
| 14 | + } |
| 15 | + |
| 16 | + static Future<void> showInfoDialog(final BuildContext context) async { |
| 17 | + await showDialog( |
| 18 | + context: context, |
| 19 | + builder: (final BuildContext dialogContext) { |
| 20 | + return const InfoDialog(); |
| 21 | + }); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +class _InfoDialogState extends State<InfoDialog> { |
| 26 | + Future<String>? _libraryVersion; |
| 27 | + Future<String>? _appVersion; |
| 28 | + |
| 29 | + @override |
| 30 | + void initState() { |
| 31 | + super.initState(); |
| 32 | + |
| 33 | + // ignore: discarded_futures |
| 34 | + _libraryVersion = _getLibraryVersion(); |
| 35 | + // ignore: discarded_futures |
| 36 | + _appVersion = _getExampleAppVersion(); |
| 37 | + } |
| 38 | + |
| 39 | + @override |
| 40 | + Widget build(final BuildContext context) { |
| 41 | + return AlertDialog( |
| 42 | + title: const Text("Flutter web bluetooth"), |
| 43 | + content: SingleChildScrollView( |
| 44 | + child: ListBody( |
| 45 | + children: [ |
| 46 | + FutureBuilder<String>( |
| 47 | + future: _appVersion, |
| 48 | + builder: (final BuildContext context, |
| 49 | + final AsyncSnapshot<String> snapshot) { |
| 50 | + return Text( |
| 51 | + "Example app version: ${snapshot.data ?? "loading"}"); |
| 52 | + }), |
| 53 | + FutureBuilder<String>( |
| 54 | + future: _libraryVersion, |
| 55 | + builder: (final BuildContext context, |
| 56 | + final AsyncSnapshot<String> snapshot) { |
| 57 | + return Text( |
| 58 | + "Using library version: ${snapshot.data ?? "Loading"}"); |
| 59 | + }), |
| 60 | + ], |
| 61 | + ), |
| 62 | + ), |
| 63 | + actions: <Widget>[ |
| 64 | + TextButton( |
| 65 | + onPressed: () { |
| 66 | + showLicensePage( |
| 67 | + context: context, |
| 68 | + applicationName: "Flutter web bluetooth example"); |
| 69 | + }, |
| 70 | + child: const Text("Licenses")), |
| 71 | + TextButton( |
| 72 | + onPressed: () { |
| 73 | + Navigator.pop(context); |
| 74 | + }, |
| 75 | + child: const Text("Close"), |
| 76 | + ) |
| 77 | + ], |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + Future<String> _getLibraryVersion() async { |
| 82 | + final data = await rootBundle.loadString("pubspec.lock"); |
| 83 | + |
| 84 | + try { |
| 85 | + final parsed = loadYaml(data); |
| 86 | + final packages = parsed["packages"]; |
| 87 | + if (packages == null) { |
| 88 | + throw ArgumentError(); |
| 89 | + } |
| 90 | + final library = packages["flutter_web_bluetooth"]; |
| 91 | + if (library == null) { |
| 92 | + throw ArgumentError(); |
| 93 | + } |
| 94 | + final version = library["version"]; |
| 95 | + if (version == null) { |
| 96 | + throw ArgumentError(); |
| 97 | + } |
| 98 | + return version.toString(); |
| 99 | + } on YamlException { |
| 100 | + return "Could not be loaded"; |
| 101 | + } on ArgumentError { |
| 102 | + return "Could not be loaded"; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + Future<String> _getExampleAppVersion() async { |
| 107 | + final info = await PackageInfo.fromPlatform(); |
| 108 | + |
| 109 | + return info.version; |
| 110 | + } |
| 111 | +} |
0 commit comments