base_slider_widget.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import 'package:flutter/material.dart';
  2. /// annot_attribute_options_widget.dart
  3. ///
  4. /// Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  5. ///
  6. /// THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  7. /// AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  8. /// UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  9. /// This notice may not be removed from this file.
  10. typedef AnnotSliderValueOptionsCallback = void Function(double value);
  11. enum ValueShowType { sourceValue, percentage }
  12. class BaseSliderWidget extends StatefulWidget {
  13. final double sliderCurrentValue;
  14. final double sliderMaxValue;
  15. final double sliderMinValue;
  16. final IconData icon;
  17. final ValueShowType valueShowType;
  18. final String valueText;
  19. final AnnotSliderValueOptionsCallback sliderValueCallback;
  20. const BaseSliderWidget(
  21. {Key? key,
  22. required this.sliderCurrentValue,
  23. required this.sliderMinValue,
  24. required this.sliderMaxValue,
  25. required this.icon,
  26. this.valueText = "",
  27. this.valueShowType = ValueShowType.percentage,
  28. required this.sliderValueCallback})
  29. : super(key: key);
  30. @override
  31. State<BaseSliderWidget> createState() => _BaseSliderWidgetState();
  32. }
  33. class _BaseSliderWidgetState extends State<BaseSliderWidget> {
  34. late double currentSliderValue = 0.0;
  35. @override
  36. void initState() {
  37. super.initState();
  38. currentSliderValue = widget.sliderCurrentValue.toDouble();
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. return Padding(
  43. padding: const EdgeInsets.symmetric(horizontal: 8),
  44. child: Row(
  45. crossAxisAlignment: CrossAxisAlignment.center,
  46. children: [
  47. Icon(widget.icon),
  48. Expanded(
  49. child: Slider(
  50. min: widget.sliderMinValue,
  51. max: widget.sliderMaxValue,
  52. value: currentSliderValue,
  53. onChanged: (data) {
  54. setState(() {
  55. currentSliderValue = data;
  56. widget.sliderValueCallback(currentSliderValue.roundToDouble());
  57. });
  58. }
  59. )),
  60. if(widget.valueText.isEmpty) ...[
  61. Container(
  62. width: 50,
  63. child: Text(widget.valueShowType == ValueShowType.percentage
  64. ? '${((currentSliderValue / widget.sliderMaxValue) * 100).round()}%'
  65. : currentSliderValue.round().toString()),
  66. )
  67. ]else ...[
  68. Container(
  69. width: 50,
  70. child: Text(widget.valueText),
  71. )
  72. ]
  73. ],
  74. ));
  75. }
  76. }
  77. Widget colorAlphaWidget({int currentAlphaValue = 0, double sliderMaxValue = 255, required AnnotSliderValueOptionsCallback alphaOptionsCallback}){
  78. return BaseSliderWidget(sliderCurrentValue: currentAlphaValue.toDouble(),sliderMinValue: 0, sliderMaxValue: sliderMaxValue, icon: Icons.invert_colors, sliderValueCallback: alphaOptionsCallback);
  79. }