flowy_svg.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_svg/flutter_svg.dart';
  3. /// The class for FlowySvgData that the code generator will implement
  4. class FlowySvgData {
  5. /// The svg data
  6. const FlowySvgData(
  7. this.path,
  8. );
  9. /// The path to the svg data in appflowy assets/images
  10. final String path;
  11. }
  12. /// For icon that needs to change color when it is on hovered
  13. ///
  14. /// Get the hover color from ThemeData
  15. class FlowySvg extends StatelessWidget {
  16. /// Construct a FlowySvg Widget
  17. const FlowySvg(
  18. this.svg, {
  19. super.key,
  20. this.size,
  21. this.color,
  22. this.blendMode = BlendMode.srcIn,
  23. });
  24. /// The data for the flowy svg. Will be generated by the generator in this
  25. /// package within bin/flowy_svg.dart
  26. final FlowySvgData svg;
  27. /// The size of the svg
  28. final Size? size;
  29. /// The color of the svg.
  30. ///
  31. /// This property will not be applied to the underlying svg widget if the
  32. /// blend mode is null, but the blend mode defaults to [BlendMode.srcIn]
  33. /// if it is not explicitly set to null.
  34. final Color? color;
  35. /// The blend mode applied to the svg.
  36. ///
  37. /// If the blend mode is null then the icon color will not be applied.
  38. /// Set both the icon color and blendMode in order to apply color to the
  39. /// svg widget.
  40. final BlendMode? blendMode;
  41. @override
  42. Widget build(BuildContext context) {
  43. final iconColor = color ?? Theme.of(context).iconTheme.color;
  44. return SvgPicture.asset(
  45. _normalized(),
  46. width: size?.width,
  47. height: size?.height,
  48. colorFilter: iconColor != null && blendMode != null
  49. ? ColorFilter.mode(
  50. iconColor,
  51. blendMode!,
  52. )
  53. : null,
  54. );
  55. }
  56. /// If the SVG's path does not start with `assets/`, it is
  57. /// normalized and directed to `assets/images/`
  58. ///
  59. /// If the SVG does not end with `.svg`, then we append the file extension
  60. ///
  61. String _normalized() {
  62. var path = svg.path;
  63. if (!path.toLowerCase().startsWith('assets/')) {
  64. path = 'assets/images/$path';
  65. }
  66. if (!path.toLowerCase().endsWith('.svg')) {
  67. path = '$path.svg';
  68. }
  69. return path;
  70. }
  71. }