webview_progress_bar.dart 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import 'package:eitc_erm_dental_flutter/funcs.dart';
  2. import 'package:flutter/material.dart';
  3. ///Webview进度条
  4. class WebviewProgressBar extends StatefulWidget {
  5. final WebviewProgressBarController controller;
  6. final bool autoHide;
  7. final Color? backgroundColor;
  8. final Color? color;
  9. final Animation<Color?>? valueColor;
  10. final String? semanticsLabel;
  11. final String? semanticsValue;
  12. final BorderRadiusGeometry borderRadius;
  13. const WebviewProgressBar(
  14. {super.key,
  15. required this.controller,
  16. this.autoHide = true,
  17. this.backgroundColor,
  18. this.color,
  19. this.valueColor,
  20. this.semanticsLabel,
  21. this.semanticsValue,
  22. this.borderRadius = BorderRadius.zero});
  23. @override
  24. State<WebviewProgressBar> createState() => _WebviewProgressBarState();
  25. }
  26. class _WebviewProgressBarState extends State<WebviewProgressBar> {
  27. @override
  28. void initState() {
  29. super.initState();
  30. widget.controller.progress.addListener(_onUpdateProgress);
  31. }
  32. void _onUpdateProgress() {
  33. setState(() {});
  34. }
  35. @override
  36. void dispose() {
  37. super.dispose();
  38. widget.controller.progress.removeListener(_onUpdateProgress);
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. WebviewProgressBarController controller = widget.controller;
  43. if (controller.progress.value >= 100 && widget.autoHide) {
  44. return SizedBox();
  45. }
  46. return LinearProgressIndicator(
  47. value: controller.progress.value / 100,
  48. backgroundColor: widget.backgroundColor,
  49. color: widget.color,
  50. valueColor: widget.valueColor,
  51. semanticsLabel: widget.semanticsLabel,
  52. semanticsValue: widget.semanticsValue,
  53. borderRadius: widget.borderRadius,
  54. );
  55. }
  56. }
  57. ///Webview进度条控制器
  58. class WebviewProgressBarController {
  59. ///进度
  60. ValueNotifier<int>? _progress;
  61. ///进度
  62. ValueNotifier<int> get progress => _progress ??= ValueNotifier(0);
  63. ///设置进度
  64. void setProgress(int value) {
  65. progress.value = value;
  66. }
  67. void dispose() {
  68. _progress?.dispose();
  69. _progress = null;
  70. }
  71. }