90 lines
2.9 KiB
Dart
90 lines
2.9 KiB
Dart
|
|
import 'package:flutter/material.dart';
|
||
|
|
import 'package:flutter_sdk/starcloud_flutter_sdk.dart';
|
||
|
|
import '../l10n.dart';
|
||
|
|
import '../global.dart';
|
||
|
|
import '../widgets/common_widgets.dart';
|
||
|
|
|
||
|
|
class LockControlTab extends StatefulWidget {
|
||
|
|
const LockControlTab({super.key});
|
||
|
|
|
||
|
|
@override
|
||
|
|
State<LockControlTab> createState() => _LockControlTabState();
|
||
|
|
}
|
||
|
|
|
||
|
|
class _LockControlTabState extends State<LockControlTab> {
|
||
|
|
final List<String> _log = [];
|
||
|
|
bool _loading = false;
|
||
|
|
|
||
|
|
void _log_(String msg) {
|
||
|
|
final ts = DateTime.now().toIso8601String().substring(11, 19);
|
||
|
|
setState(() => _log.add('[$ts] $msg'));
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> _run(String desc, Future<void> Function() fn) async {
|
||
|
|
if (pairedLockId.value == null) return;
|
||
|
|
_log_('$desc...');
|
||
|
|
setState(() => _loading = true);
|
||
|
|
try {
|
||
|
|
await fn();
|
||
|
|
_log_('${S.isEn ? "Success" : "成功"}');
|
||
|
|
} catch (e) {
|
||
|
|
_log_('${S.isEn ? "Failed" : "失败"}: $e');
|
||
|
|
} finally {
|
||
|
|
setState(() => _loading = false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
// 监听语言切换通知
|
||
|
|
return ValueListenableBuilder<bool>(
|
||
|
|
valueListenable: S.langNotifier,
|
||
|
|
builder: (context, isEn, _) {
|
||
|
|
return Padding(
|
||
|
|
padding: const EdgeInsets.all(16),
|
||
|
|
child: ValueListenableBuilder<int?>(
|
||
|
|
valueListenable: pairedLockId,
|
||
|
|
builder: (context, id, _) {
|
||
|
|
return Column(
|
||
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||
|
|
children: [
|
||
|
|
Text('${S.lockIdLabel}: ${id ?? S.notPaired}',
|
||
|
|
style: const TextStyle(fontWeight: FontWeight.bold)),
|
||
|
|
const SizedBox(height: 16),
|
||
|
|
Row(
|
||
|
|
children: [
|
||
|
|
Expanded(
|
||
|
|
child: ActionButton(
|
||
|
|
label: S.btnOpen,
|
||
|
|
icon: Icons.lock_open,
|
||
|
|
color: Colors.green,
|
||
|
|
loading: _loading || id == null,
|
||
|
|
onTap: () => _run(S.btnOpen,
|
||
|
|
() => StarCloudClient.instance.device.openLock(lockId: id!)),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
const SizedBox(width: 8),
|
||
|
|
Expanded(
|
||
|
|
child: ActionButton(
|
||
|
|
label: S.btnClose,
|
||
|
|
icon: Icons.lock,
|
||
|
|
color: Colors.orange,
|
||
|
|
loading: _loading || id == null,
|
||
|
|
onTap: () => _run(S.btnClose,
|
||
|
|
() => StarCloudClient.instance.device.closeLock(lockId: id!)),
|
||
|
|
),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
const SizedBox(height: 16),
|
||
|
|
LogArea(log: _log),
|
||
|
|
],
|
||
|
|
);
|
||
|
|
},
|
||
|
|
),
|
||
|
|
);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|