demo.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'dart:collection';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vnote_device_plugin/consts/types.dart';
  6. import 'package:vnote_device_plugin_example/widgets/device_entity.dart';
  7. import 'package:vnote_device_plugin_example/widgets/device_manager.dart';
  8. import 'device_card.dart';
  9. class DevicesSettingState {
  10. final RxList<String> _accessTypes = RxList<String>();
  11. final RxList<DeviceEntity> _deviceList = RxList<DeviceEntity>();
  12. /// 已授权设备类型集合
  13. List<String> get accessTypes => UnmodifiableListView(_accessTypes);
  14. set accessTypes(List<String> val) => _accessTypes.value = val;
  15. /// 设备信息集合
  16. List<DeviceEntity> get deviceList => UnmodifiableListView(_deviceList);
  17. set deviceList(List<DeviceEntity> val) => _deviceList.value = val;
  18. }
  19. class DevicesSettingController extends GetxController {
  20. final state = DevicesSettingState();
  21. final manager = DeviceManager();
  22. @override
  23. void onReady() {
  24. _loadData();
  25. super.onReady();
  26. }
  27. DeviceEntity? getDataByType(String type) {
  28. final data = state.deviceList.firstWhereOrNull((e) => e.type == type);
  29. return data;
  30. }
  31. void _loadData() {
  32. state.accessTypes = manager.getAccessTypes();
  33. state.deviceList = [
  34. DeviceEntity(
  35. type: DeviceTypes.TEMP,
  36. productName: "鱼跃",
  37. bleName: "yuyue",
  38. model: "yuddd",
  39. mac: "xx:xx:xx:00:xx",
  40. ),
  41. ];
  42. }
  43. }
  44. class DevicesSettingPage extends GetView<DevicesSettingController> {
  45. const DevicesSettingPage({super.key});
  46. @override
  47. Widget build(BuildContext context) {
  48. return Scaffold(
  49. backgroundColor: Colors.grey.shade200,
  50. appBar: AppBar(
  51. elevation: 0,
  52. ),
  53. body: Row(
  54. crossAxisAlignment: CrossAxisAlignment.start,
  55. children: [
  56. Flexible(
  57. flex: 4,
  58. child: Container(
  59. color: Colors.green,
  60. ),
  61. ),
  62. Flexible(
  63. flex: 5,
  64. child: Container(
  65. padding: const EdgeInsets.all(12),
  66. child: _buildListView(),
  67. ),
  68. ),
  69. ],
  70. ),
  71. );
  72. }
  73. Widget _buildListView() {
  74. return Obx(() {
  75. final children = controller.state.accessTypes.map((e) {
  76. final data = controller.getDataByType(e);
  77. return DeviceCard(
  78. type: e,
  79. data: data,
  80. );
  81. }).toList();
  82. return StaggeredGrid.count(
  83. axisDirection: AxisDirection.down,
  84. crossAxisCount: 2,
  85. mainAxisSpacing: 16,
  86. crossAxisSpacing: 16,
  87. children: children,
  88. );
  89. });
  90. }
  91. }