Update test
This commit is contained in:
+109
-21
@@ -1,6 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_usb_serial/flutter_usb_serial.dart';
|
||||
|
||||
@@ -16,34 +16,67 @@ class MyApp extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
String _platformVersion = 'Unknown';
|
||||
final _flutterUsbSerialPlugin = FlutterUsbSerial();
|
||||
List<UsbSerialDevice> _devices = const [];
|
||||
String _status = 'Tap refresh to scan for attached USB serial devices.';
|
||||
bool _isLoading = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initPlatformState();
|
||||
unawaited(_refreshDevices());
|
||||
}
|
||||
|
||||
// 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.
|
||||
Future<void> _refreshDevices() async {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
platformVersion =
|
||||
await _flutterUsbSerialPlugin.getPlatformVersion() ?? 'Unknown platform version';
|
||||
} on PlatformException {
|
||||
platformVersion = 'Failed to get platform version.';
|
||||
final devices = await FlutterUsbSerial.listDevices();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_devices = devices;
|
||||
_status = devices.isEmpty
|
||||
? 'No USB serial devices detected.'
|
||||
: 'Found ${devices.length} USB serial device(s).';
|
||||
});
|
||||
} on MissingPluginException {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_devices = const [];
|
||||
_status = 'USB serial support is only available on Android.';
|
||||
});
|
||||
} on PlatformException catch (error) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_devices = const [];
|
||||
_status = error.message ?? 'Failed to list USB serial devices.';
|
||||
});
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _requestPermission(UsbSerialDevice device) async {
|
||||
String status;
|
||||
try {
|
||||
final granted = await FlutterUsbSerial.requestPermission(device);
|
||||
status = granted
|
||||
? 'Permission granted for device ${device.deviceId}.'
|
||||
: 'Permission denied for device ${device.deviceId}.';
|
||||
} on MissingPluginException {
|
||||
status = 'USB serial support is only available on Android.';
|
||||
} on PlatformException catch (error) {
|
||||
status = error.message ?? 'Failed to request USB permission.';
|
||||
}
|
||||
|
||||
// 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;
|
||||
_status = status;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -52,10 +85,65 @@ class _MyAppState extends State<MyApp> {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Plugin example app'),
|
||||
title: const Text('USB Serial Example'),
|
||||
),
|
||||
body: Center(
|
||||
child: Text('Running on: $_platformVersion\n'),
|
||||
body: SafeArea(
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
_status,
|
||||
key: const Key('status_text'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
FilledButton.icon(
|
||||
onPressed: _isLoading ? null : _refreshDevices,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('Refresh devices'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _devices.isEmpty
|
||||
? const Center(
|
||||
child: Text('Connect an Android USB serial device to see it here.'),
|
||||
)
|
||||
: ListView.separated(
|
||||
itemCount: _devices.length,
|
||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final device = _devices[index];
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.usb),
|
||||
title: Text(
|
||||
device.deviceName.isEmpty
|
||||
? 'USB device ${device.deviceId}'
|
||||
: device.deviceName,
|
||||
),
|
||||
subtitle: Text(
|
||||
'VID 0x${device.vendorId.toRadixString(16).padLeft(4, '0')} '
|
||||
'PID 0x${device.productId.toRadixString(16).padLeft(4, '0')}\n'
|
||||
'Manufacturer: ${device.manufacturerName.isEmpty ? 'Unknown' : device.manufacturerName}',
|
||||
),
|
||||
isThreeLine: true,
|
||||
trailing: OutlinedButton(
|
||||
onPressed: () => _requestPermission(device),
|
||||
child: const Text('Request permission'),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user