123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:vnote_device_plugin/devices/sugar.dart';
- import 'package:vnote_device_plugin/models/exams/sugar.dart';
- import 'package:vnoteapp/components/alert_dialog.dart';
- import 'package:vnoteapp/components/button.dart';
- import 'package:vnoteapp/managers/interfaces/permission.dart';
- import 'package:vnoteapp/pages/check/models/form.dart';
- import 'package:vnoteapp/pages/check/widgets/exam_configurable/exam_card.dart';
- class ExamBloodSugar extends StatefulWidget {
- const ExamBloodSugar({
- super.key,
- required this.currentFormObject,
- required this.currentInputValue,
- this.specialInput,
- });
- final FormObject currentFormObject;
- final String currentInputValue;
- final Function(String value)? specialInput;
- @override
- State<ExamBloodSugar> createState() => _ExamBloodSugarState();
- }
- class _ExamBloodSugarState extends State<ExamBloodSugar> {
- var permissionManager = Get.find<IPermissionManager>();
- @override
- void initState() {
- permissionManager.requestLocationPermission();
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- return ExamCard(
- title: widget.currentFormObject.label ?? '',
- clickCard: () {
- _buildTempertureInput(widget.currentFormObject);
- },
- content: Container(
- alignment: Alignment.bottomRight,
- padding: const EdgeInsets.only(
- bottom: 20,
- right: 30,
- left: 40,
- ),
- constraints: const BoxConstraints(minHeight: 150),
- child: FittedBox(
- child: Row(
- mainAxisAlignment: MainAxisAlignment.end,
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- RichText(
- text: TextSpan(
- text: widget.currentInputValue,
- style: const TextStyle(
- fontSize: 80,
- color: Colors.black,
- ),
- children: [
- TextSpan(
- text: widget.currentFormObject.append ?? '',
- style: const TextStyle(fontSize: 25),
- )
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- );
- }
- Future<void> _buildTempertureInput(FormObject currentFormObject) async {
- // Future.delayed(const Duration(milliseconds: 3000), () {
- // specialInputController.text = generateRandomNumber().toString();
- // widget.specialInput?.call(specialInputController.text);
- // setState(() {});
- // });
- final result = await Get.dialog(
- BloodSugar(
- currentFormObject: widget.currentFormObject,
- bloodSugar: widget.currentInputValue,
- ),
- barrierDismissible: false,
- );
- widget.specialInput?.call(result);
- print(result);
- }
- }
- class BloodSugar extends StatefulWidget {
- const BloodSugar({
- super.key,
- required this.currentFormObject,
- this.bloodSugar,
- });
- final FormObject currentFormObject;
- final String? bloodSugar;
- @override
- State<BloodSugar> createState() => _BloodSugarState();
- }
- class _BloodSugarState extends State<BloodSugar> {
- late final SugarDeviceWorker worker = SugarDeviceWorker(
- mac: '60:98:66:C4:0E:51',
- model: 'i-sens630',
- );
- late TextEditingController specialInputController =
- TextEditingController(text: widget.bloodSugar ?? '00.0');
- bool connectFailState = false;
- bool connectSuccessState = false;
- bool isConnect = false;
- @override
- void initState() {
- connect();
- worker.successEvent.addListener(_onSuccess);
- worker.connectErrorEvent.addListener(_onConnectFail);
- worker.connectedEvent.addListener(_onConnectSuccess);
- super.initState();
- }
- Future<void> connect() async {
- connectFailState = false;
- isConnect = true;
- connectSuccessState = false;
- setState(() {});
- await worker.connect();
- }
- Future<void> disconnect() async {
- worker.connectErrorEvent.removeListener(_onConnectFail);
- worker.connectedEvent.removeListener(_onConnectSuccess);
- await worker.disconnect();
- }
- @override
- void dispose() {
- super.dispose();
- }
- void _onSuccess(_, SugarExamData e) {
- setState(() {
- specialInputController.text = e.sugar.toString();
- // connectFailState = false;
- // connectSuccessState = false;
- isConnect = false;
- // disconnect();
- });
- }
- void _onConnectFail(sender, e) {
- print('连接设备失败');
- connectFailState = true;
- connectSuccessState = false;
- isConnect = false;
- disconnect();
- setState(() {});
- }
- void _onConnectSuccess(sender, e) {
- connectSuccessState = true;
- connectFailState = false;
- isConnect = false;
- setState(() {});
- }
- @override
- Widget build(BuildContext context) {
- return VAlertDialog(
- title: widget.currentFormObject.label ?? '',
- width: 600,
- contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
- content: buildMainWidget(),
- showCancel: true,
- onConfirm: () {
- disconnect();
- Get.back(result: specialInputController.text);
- },
- onCanceled: () {
- disconnect();
- },
- );
- }
- Widget buildInputField() {
- return Container(
- width: 350,
- padding: const EdgeInsets.only(left: 15),
- child: TextFormField(
- keyboardType: TextInputType.number,
- style: const TextStyle(
- fontSize: 100,
- ),
- showCursor: false,
- controller: specialInputController,
- decoration: const InputDecoration(
- labelStyle: TextStyle(
- fontSize: 100,
- ),
- ),
- ),
- );
- }
- Widget buildConnectFailText() {
- return const Text(
- '设备连接失败',
- style: TextStyle(
- color: Colors.red,
- fontSize: 25,
- ),
- textAlign: TextAlign.left,
- );
- }
- Widget buildConnectSuccessText() {
- return const Text(
- '设备连接成功',
- style: TextStyle(
- color: Colors.green,
- fontSize: 25,
- ),
- textAlign: TextAlign.left,
- );
- }
- Widget buildConnectingText() {
- return const Row(
- children: [
- Expanded(
- child: Text(
- '设备连接中',
- style: TextStyle(
- fontSize: 40,
- color: Colors.blue,
- ),
- ),
- ),
- CircularProgressIndicator(
- valueColor: AlwaysStoppedAnimation(
- Colors.blue,
- ),
- ),
- SizedBox(
- width: 20,
- ),
- ],
- );
- }
- Widget buildReconnectButton() {
- return Container(
- margin: const EdgeInsets.only(top: 4),
- width: 134,
- child: VButton(
- onTap: () async {
- /// TODO
- await connect();
- worker.connectErrorEvent.addListener(_onConnectFail);
- worker.connectedEvent.addListener(_onConnectSuccess);
- /// TODO 后面需要改,这边暂时演示用
- Future.delayed(const Duration(milliseconds: 8000), () {
- if (!connectSuccessState) {
- connectFailState = true;
- }
- });
- },
- child: const Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Icon(Icons.connected_tv_rounded, size: 24),
- SizedBox(
- width: 8,
- ),
- Text("重连", style: TextStyle(fontSize: 20)),
- ],
- ),
- ),
- );
- }
- Widget buildConnectStateWidgets() {
- return Column(
- children: [
- if (connectFailState) buildConnectFailText(),
- if (connectSuccessState) buildConnectSuccessText(),
- if (isConnect)
- buildConnectingText()
- else if (connectFailState)
- buildReconnectButton(),
- ],
- );
- }
- Widget buildMainWidget() {
- return SizedBox(
- height: 100,
- child: Row(
- children: [
- buildInputField(),
- Expanded(
- child: buildConnectStateWidgets(),
- ),
- ],
- ),
- );
- }
- }
|