123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:vnote_device_plugin/devices/weight_height.dart';
- import 'package:vnote_device_plugin/models/exams/weight_height.dart';
- class WeightHeightCard extends StatefulWidget {
- final String mac;
- final String model;
- const WeightHeightCard({super.key, required this.mac, required this.model});
- @override
- State<WeightHeightCard> createState() => _WeightHeightCardState();
- }
- class _WeightHeightCardState extends State<WeightHeightCard> {
- late final WeightHeightDeviceWorker worker =
- WeightHeightDeviceWorker(mac: widget.mac, model: widget.model);
- WeightHeightExamData? value;
- bool _working = false;
- @override
- void initState() {
- worker.successEvent.addListener(_onSuccess);
- worker.errorEvent.addListener(_onError);
- super.initState();
- }
- void _onSuccess(_, WeightHeightExamData e) {
- setState(() {
- value = e;
- });
- }
- void _onError(_, String e) {
- Get.snackbar(
- "提示",
- "测量错误:$e",
- snackPosition: SnackPosition.TOP,
- );
- }
- @override
- void dispose() {
- worker.successEvent.removeListener(_onSuccess);
- worker.errorEvent.removeListener(_onError);
- worker.disconnect();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- return Card(
- elevation: 4,
- shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
- child: Container(
- alignment: Alignment.center,
- width: 360,
- height: 240,
- child: SizedBox(
- height: 100,
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Center(
- child: Column(
- children: [
- Row(
- children: [
- Text(
- "${value != null ? double.parse(value!.height.toStringAsFixed(1)) : ""}",
- style: const TextStyle(fontSize: 40),
- ),
- const SizedBox(
- width: 8,
- ),
- const Text(
- "cm",
- style: TextStyle(
- fontSize: 20,
- ),
- ),
- ],
- ),
- Row(
- children: [
- Text(
- "${value != null ? double.parse(value!.weight.toStringAsFixed(1)) : ""}",
- style: const TextStyle(fontSize: 40),
- ),
- const SizedBox(
- width: 8,
- ),
- const Text(
- "kg",
- style: TextStyle(
- fontSize: 20,
- ),
- ),
- ],
- ),
- ],
- ),
- ),
- const SizedBox(
- width: 24,
- ),
- SizedBox(
- width: 60,
- height: 60,
- child: _working
- ? OutlinedButton(
- style: ButtonStyle(
- backgroundColor: MaterialStatePropertyAll(
- Colors.red.withOpacity(.08),
- ),
- shape: MaterialStatePropertyAll(
- RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(30),
- ),
- ),
- ),
- onPressed: () async {
- await worker.disconnect();
- setState(() {
- _working = false;
- });
- },
- child: const Text(
- '停止',
- style: TextStyle(color: Colors.red),
- ))
- : OutlinedButton(
- style: ButtonStyle(
- backgroundColor: MaterialStatePropertyAll(
- Theme.of(context).primaryColor.withOpacity(.08),
- ),
- shape: MaterialStatePropertyAll(
- RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(30),
- ),
- ),
- ),
- onPressed: () async {
- await worker.connect();
- setState(() {
- _working = true;
- });
- },
- child: const Text("开始测量"),
- ),
- ),
- ],
- ),
- ),
- ),
- );
- }
- }
|