android_global_scroll_fix.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import 'dart:math';
  2. import 'package:flutter/material.dart';
  3. import 'package:fis_common/env/env.dart';
  4. import 'package:flutter/rendering.dart';
  5. import 'package:vitalapp/architecture/app_parameters.dart';
  6. // 用于修正工作站 鼠标滚动速度过慢的问题
  7. class AndroidGlobalScrollFix extends StatefulWidget {
  8. const AndroidGlobalScrollFix({
  9. super.key,
  10. required this.child,
  11. this.scrollSpeed = 40.0,
  12. });
  13. final Widget child;
  14. final double scrollSpeed;
  15. @override
  16. State<AndroidGlobalScrollFix> createState() => _AndroidGlobalScrollFixState();
  17. }
  18. class _AndroidGlobalScrollFixState extends State<AndroidGlobalScrollFix> {
  19. FPlatformEnum get platform => FPlatform.current;
  20. @override
  21. void initState() {
  22. super.initState();
  23. }
  24. @override
  25. void dispose() {
  26. super.dispose();
  27. }
  28. // 监听全局滚动 通过 BuildContext 来访问到当前激活的ScrollController
  29. @override
  30. Widget build(BuildContext context) {
  31. // return widget.child;
  32. if (platform == FPlatformEnum.android &&
  33. AppParameters.data.isLocalStation) {
  34. // 如果是安卓,需要监听滚动事件,加速安卓上鼠标的滚动
  35. return NotificationListener<ScrollNotification>(
  36. onNotification: (ScrollNotification notification) {
  37. if (notification is ScrollUpdateNotification) {
  38. final detail = notification.dragDetails;
  39. if (detail != null) {
  40. return true;
  41. }
  42. if (notification.context != null) {
  43. ScrollController? scrollController =
  44. Scrollable.of(notification.context!).widget.controller;
  45. if (scrollController != null) {
  46. ScrollDirection scrollDirection =
  47. scrollController.position.userScrollDirection;
  48. if (scrollDirection != ScrollDirection.idle) {
  49. double scrollEnd = scrollController.offset +
  50. (scrollDirection == ScrollDirection.reverse
  51. ? widget.scrollSpeed
  52. : -widget.scrollSpeed);
  53. scrollEnd = min(
  54. scrollController.position.maxScrollExtent,
  55. max(scrollController.position.minScrollExtent,
  56. scrollEnd));
  57. scrollController.jumpTo(scrollEnd);
  58. }
  59. }
  60. }
  61. }
  62. return true;
  63. },
  64. child: widget.child,
  65. );
  66. } else {
  67. return widget.child;
  68. }
  69. }
  70. }