webview_progress_bar.dart 1.8 KB

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