index.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. Future<void> _onChangePatint() async {
  144. try {
  145. final homeController = Get.find<HomeController>();
  146. homeController.switchNavByName('/patient/list');
  147. } catch (e) {
  148. print(e);
  149. }
  150. Store.user.isShowUserCard = true;
  151. }
  152. _onPanUpdate(DragUpdateDetails detail, double maxHeight, double maxWidth) {
  153. //用户手指滑动时,更新偏移,重新构建
  154. //顶部
  155. if (_topVN.value < widget.padding.top && detail.delta.dy < 0) {
  156. return;
  157. }
  158. // 左边
  159. if (_leftVN.value < widget.padding.left && detail.delta.dx < 0) {
  160. return;
  161. }
  162. // 右边
  163. if (_topVN.value >
  164. (maxHeight - widget.childSize.height - widget.padding.bottom) &&
  165. detail.delta.dy > 0) {
  166. return;
  167. }
  168. // 下边
  169. if (_leftVN.value >
  170. (maxWidth - widget.childSize.width - widget.padding.right) &&
  171. detail.delta.dx > 0) {
  172. return;
  173. }
  174. _topVN.value += detail.delta.dy;
  175. print('update');
  176. /// 这边可以后面改成全局移动
  177. _leftVN.value += detail.delta.dx;
  178. // _leftVN.value = maxWidth - widget.childSize.width;
  179. // debugPrint("y:${_topVN.value}");
  180. }
  181. }