view.dart 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
  3. import 'package:get/get.dart';
  4. import 'package:vitalapp/components/alert_dialog.dart';
  5. import 'controller.dart';
  6. import 'widgets/device_card.dart';
  7. import 'widgets/search_dialog.dart';
  8. class DevicesSettingPage extends GetView<DevicesSettingController> {
  9. const DevicesSettingPage({super.key});
  10. @override
  11. Widget build(BuildContext context) {
  12. return Container(
  13. // color: Colors.grey.shade200,
  14. alignment: Alignment.topCenter,
  15. padding: const EdgeInsets.all(12),
  16. child: _buildListView(),
  17. );
  18. }
  19. Widget _buildListView() {
  20. return Obx(() {
  21. final deviceList = controller.state.deviceList;
  22. List<String> types = List.from(controller.state.accessTypes);
  23. types.sort((a, b) {
  24. return deviceList.any((e) => e.type == b) ? 1 : 0;
  25. });
  26. final children = types.map((e) {
  27. final data = controller.getDataByType(e);
  28. return DeviceCard(
  29. type: e,
  30. data: data,
  31. onTapChangeBind: (value) {
  32. if (value) {
  33. _onDeviceBind(e);
  34. } else {
  35. _onDeviceUnbind(e);
  36. }
  37. },
  38. );
  39. }).toList();
  40. return SingleChildScrollView(
  41. child: StaggeredGrid.count(
  42. axisDirection: AxisDirection.down,
  43. crossAxisCount: 2,
  44. mainAxisSpacing: 16,
  45. crossAxisSpacing: 16,
  46. children: children,
  47. ),
  48. );
  49. });
  50. }
  51. void _onDeviceBind(String type) async {
  52. // await controller.initPermission();
  53. final result = await DeviceSearchDialog.dialog(type);
  54. if (result != null) {
  55. controller.bindDevice(result);
  56. }
  57. }
  58. void _onDeviceUnbind(String type) {
  59. // TODO: 用confirm组件替换
  60. Get.dialog(
  61. VAlertDialog(
  62. title: "提示",
  63. width: 260,
  64. content: Container(
  65. height: 32,
  66. padding: const EdgeInsets.symmetric(horizontal: 24),
  67. alignment: Alignment.center,
  68. child: const Text(
  69. "确定解除此设备的绑定?",
  70. style: TextStyle(fontSize: 20),
  71. ),
  72. ),
  73. onConfirm: () {
  74. Get.back();
  75. controller.unbindDevice(type);
  76. },
  77. ),
  78. barrierDismissible: false,
  79. barrierColor: Colors.black.withOpacity(.4),
  80. );
  81. }
  82. }