index.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:vitalapp/pages/home/controller.dart';
  4. import 'package:vitalapp/store/store.dart';
  5. /// 悬浮组件
  6. class HoveringPatientCard extends StatefulWidget {
  7. const HoveringPatientCard({
  8. Key? key,
  9. required this.child,
  10. required this.bgChild,
  11. this.alignment = AlignmentDirectional.topStart,
  12. this.textDirection,
  13. this.fit = StackFit.loose,
  14. this.clipBehavior = Clip.hardEdge,
  15. this.padding = EdgeInsets.zero,
  16. this.childSize = const Size(80, 80),
  17. }) : super(key: key);
  18. final AlignmentGeometry alignment;
  19. final TextDirection? textDirection;
  20. final StackFit fit;
  21. final Clip clipBehavior;
  22. /// 底部组件
  23. final Widget bgChild;
  24. /// 悬浮组件
  25. final Widget child;
  26. /// 悬浮组件宽高
  27. final Size childSize;
  28. /// 距离四周边界
  29. final EdgeInsets padding;
  30. @override
  31. _HoveringPatientCardState createState() => _HoveringPatientCardState();
  32. }
  33. class _HoveringPatientCardState extends State<HoveringPatientCard> {
  34. late ValueNotifier _topVN = ValueNotifier(0.0);
  35. late ValueNotifier _leftVN = ValueNotifier(0.0);
  36. late final ValueNotifier<bool> _panStart = ValueNotifier(false);
  37. @override
  38. Widget build(BuildContext context) {
  39. return Material(
  40. child: LayoutBuilder(builder: (context, constraints) {
  41. _topVN = ValueNotifier(186.0);
  42. _leftVN = ValueNotifier(constraints.maxWidth - widget.childSize.width);
  43. return Stack(
  44. alignment: widget.alignment,
  45. textDirection: widget.textDirection,
  46. fit: widget.fit,
  47. clipBehavior: widget.clipBehavior,
  48. children: [
  49. widget.bgChild,
  50. buildSuspension(
  51. child: widget.child,
  52. maxWidth: constraints.maxWidth,
  53. maxHeight: constraints.maxHeight,
  54. ),
  55. ],
  56. );
  57. }),
  58. );
  59. }
  60. @override
  61. void initState() {
  62. super.initState();
  63. }
  64. buildSuspension({
  65. required Widget child,
  66. required double maxWidth,
  67. required double maxHeight,
  68. }) {
  69. return AnimatedBuilder(
  70. animation: Listenable.merge([
  71. _topVN,
  72. _leftVN,
  73. ]),
  74. child: child,
  75. builder: (context, child) {
  76. return Positioned(
  77. top: _topVN.value,
  78. left: _leftVN.value,
  79. child: GestureDetector(
  80. onTap: () {
  81. // _onChangePatint();
  82. },
  83. onPanStart: (details) {
  84. _panStart.value = true;
  85. },
  86. onPanEnd: (details) {
  87. _panStart.value = false;
  88. _leftVN.value = maxWidth - widget.childSize.width;
  89. },
  90. onPanUpdate: (DragUpdateDetails detail) => _onPanUpdate(
  91. detail,
  92. maxHeight,
  93. maxWidth,
  94. ),
  95. child: AnimatedBuilder(
  96. animation: _panStart,
  97. child: child,
  98. builder: (BuildContext context, Widget? child) {
  99. if (_panStart.value) {
  100. return _buildHonver(child!);
  101. }
  102. return Container(
  103. child: child,
  104. );
  105. },
  106. ),
  107. ),
  108. );
  109. },
  110. );
  111. }
  112. Widget _buildHonver(Widget child) {
  113. return Stack(
  114. children: [
  115. SizedBox(
  116. height: 80,
  117. child: Row(
  118. children: [
  119. Column(
  120. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  121. children: const [
  122. Icon(
  123. Icons.keyboard_arrow_up_rounded,
  124. size: 30,
  125. ),
  126. Icon(
  127. Icons.keyboard_arrow_down_rounded,
  128. size: 30,
  129. ),
  130. ],
  131. ),
  132. SizedBox(
  133. height: 70,
  134. width: 70,
  135. child: child,
  136. ),
  137. ],
  138. ),
  139. ),
  140. ],
  141. );
  142. }
  143. _onPanUpdate(DragUpdateDetails detail, double maxHeight, double maxWidth) {
  144. //用户手指滑动时,更新偏移,重新构建
  145. //顶部
  146. if (_topVN.value < widget.padding.top && detail.delta.dy < 0) {
  147. return;
  148. }
  149. // 左边
  150. if (_leftVN.value < widget.padding.left && detail.delta.dx < 0) {
  151. return;
  152. }
  153. // 右边
  154. if (_topVN.value >
  155. (maxHeight - widget.childSize.height - widget.padding.bottom) &&
  156. detail.delta.dy > 0) {
  157. return;
  158. }
  159. // 下边
  160. if (_leftVN.value >
  161. (maxWidth - widget.childSize.width - widget.padding.right) &&
  162. detail.delta.dx > 0) {
  163. return;
  164. }
  165. _topVN.value += detail.delta.dy;
  166. print('update');
  167. /// 这边可以后面改成全局移动
  168. _leftVN.value += detail.delta.dx;
  169. // _leftVN.value = maxWidth - widget.childSize.width;
  170. // debugPrint("y:${_topVN.value}");
  171. }
  172. }