Files
liteauthconfig/lib/pages/devicelist.dart
T

169 lines
5.0 KiB
Dart

import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:liteauthconfig/models/appconfig.dart';
import 'package:liteauthconfig/l10n/app_localizations.dart';
import 'package:liteauthconfig/pages/deviceinfo.dart';
import 'package:liteauthconfig/pages/registerpage.dart';
import 'package:nfc_manager/nfc_manager.dart';
import 'package:nfc_manager_ndef/nfc_manager_ndef.dart';
class DeviceListPage extends StatefulWidget {
const DeviceListPage({super.key});
@override
State<StatefulWidget> createState() => _DeviceListPageState();
}
class _DeviceListPageState extends State<DeviceListPage> {
List<Device> devices = [];
NfcAvailability? availability;
@override
void initState() {
super.initState();
refreshList();
checkNfc();
}
Future checkNfc() async {
if (!(Platform.isAndroid || Platform.isIOS)) return;
availability = await NfcManager.instance.checkAvailability();
setState(() {});
if (availability != NfcAvailability.enabled) return;
NfcManager.instance.startSession(
pollingOptions: {NfcPollingOption.iso14443},
onDiscovered: (NfcTag tag) async {
final Ndef? ndef = Ndef.from(tag);
if (ndef == null) {
print("This tag is not compatible with NDEF.");
return;
}
print(ndef);
},
);
}
void refreshList() {
setState(() {
devices = AppConfig().devices;
});
}
@override
Widget build(BuildContext context) {
var appLocal = AppLocalizations.of(context)!;
var appTheme = Theme.of(context);
return Scaffold(
appBar: AppBar(title: Text(appLocal.deviceList)),
body: SingleChildScrollView(
child: Column(
children: [
Padding(
padding: EdgeInsetsGeometry.all(8),
child: switch (availability) {
null => SizedBox(),
NfcAvailability.enabled => Card(
child: ListTile(
title: Text(appLocal.nfcReady),
subtitle: Text(appLocal.nfcReadyDescription),
),
),
NfcAvailability.disabled => Card(
child: ListTile(
title: Text(appLocal.nfcDisabled),
subtitle: Text(appLocal.nfcRequired),
),
),
NfcAvailability.unsupported => Card(
child: ListTile(
title: Text(appLocal.nfcUnsupported),
subtitle: Text(appLocal.nfcRequired),
),
),
},
),
Visibility(
visible: devices.isEmpty,
replacement: ListView.builder(
itemBuilder: (context, index) {
final dev = devices[index];
return ListTile(
title: Text(dev.name),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
DeviceInfoPage(deviceIndex: index),
),
).then((_) => refreshList());
},
);
},
itemCount: devices.length,
shrinkWrap: true,
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
appLocal.noDevices,
textAlign: TextAlign.center,
style: appTheme.textTheme.bodyLarge,
),
),
),
],
),
),
floatingActionButton: SpeedDial(
overlayColor: Colors.black,
spacing: 8,
children: [
SpeedDialChild(
child: const Icon(Icons.settings),
label: appLocal.setupNewDevice,
shape: CircleBorder(),
labelStyle: appTheme.textTheme.bodyMedium?.copyWith(
color: Colors.black,
),
onTap: () {
Navigator.of(context)
.push(
MaterialPageRoute(builder: (context) => RegisterPage(true)),
)
.then((_) => refreshList());
},
),
SpeedDialChild(
child: const Icon(Icons.add_box_outlined),
label: appLocal.addExistingDevice,
shape: CircleBorder(),
labelStyle: appTheme.textTheme.bodyMedium?.copyWith(
color: Colors.black,
),
onTap: () {
Navigator.of(context)
.push(
MaterialPageRoute(
builder: (context) => RegisterPage(false),
),
)
.then((_) => refreshList());
},
),
],
child: const Icon(Icons.add),
),
);
}
}