Files
liteauthconfig/lib/pages/tokeninfo.dart
T

164 lines
5.3 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gap/gap.dart';
import 'package:liteauthconfig/l10n/app_localizations.dart';
import 'package:liteauthconfig/models/appconfig.dart';
import 'package:liteauthconfig/pages/qrscanner.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:qr_flutter/qr_flutter.dart';
class TokenInfo extends StatefulWidget {
final int deviceIndex;
final String token;
const TokenInfo(this.deviceIndex, this.token, {super.key});
@override
State<StatefulWidget> createState() => _TokenInfoState();
}
class _TokenInfoState extends State<TokenInfo> {
TextEditingController tokenController = TextEditingController();
bool showToken = false;
@override
void initState() {
tokenController.text = widget.token;
super.initState();
}
Future saveToken() async {
final appLocal = AppLocalizations.of(context)!;
var config = AppConfig();
config.devices[widget.deviceIndex].token =
tokenController.text;
await config.save();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(appLocal.saved)),
);
}
}
Future scanQrCode() async {
var code = await Navigator.push<BarcodeCapture>(
context,
MaterialPageRoute(builder: (ctx) => QRScanner()),
);
var value = code?.barcodes.first.rawValue;
if (code == null || value == null) return;
tokenController.text = value;
await saveToken();
}
@override
Widget build(BuildContext context) {
final appLocal = AppLocalizations.of(context)!;
return Scaffold(
appBar: AppBar(title: Text(appLocal.token)),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsetsGeometry.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(appLocal.viewTokenWarn),
Row(
children: [
Checkbox.adaptive(
semanticLabel: appLocal.showToken,
value: showToken,
onChanged: (newValue) {
setState(() {
showToken = newValue!;
});
},
),
Text(appLocal.showToken),
],
),
Visibility(
visible: showToken,
child: Center(
child: Column(
children: [
Visibility(
visible: tokenController.text.isNotEmpty,
replacement: Text(appLocal.tokenNotFound),
child: Column(
children: [
SizedBox.square(
dimension: 200,
child: QrImageView(
data: tokenController.text,
backgroundColor: Colors.white,
padding: EdgeInsets.all(16),
),
),
Gap(16),
OutlinedButton(
onPressed: () async {
await Clipboard.setData(
ClipboardData(text: tokenController.text),
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(appLocal.copied)),
);
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.copy),
Gap(8),
Text(appLocal.copyToken),
],
),
),
],
),
),
Gap(16),
TextField(
controller: tokenController,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: appLocal.token,
suffixIcon: IconButton(
onPressed: Platform.isLinux || Platform.isWindows
? null
: scanQrCode,
icon: const Icon(Icons.qr_code_scanner),
),
),
onChanged: (newValue) {
setState(() {});
},
),
Gap(16),
FilledButton(
onPressed: tokenController.text == widget.token
? null
: saveToken,
child: Text(appLocal.saveToken),
),
],
),
),
),
],
),
),
),
);
}
}