flowy_svg.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. final Color? color;
  31. /// If true a color filter is applied to the SVG, otherwise not applied.
  32. ///
  33. /// Defaults to true
  34. ///
  35. final BlendMode blendMode;
  36. @override
  37. Widget build(BuildContext context) {
  38. final iconColor = color ?? Theme.of(context).iconTheme.color;
  39. final child = SvgPicture.asset(
  40. _normalized(),
  41. colorFilter:
  42. iconColor != null ? ColorFilter.mode(iconColor, blendMode)
  43. : null,
  44. );
  45. if (size != null) {
  46. return SizedBox.fromSize(
  47. size: size,
  48. child: child,
  49. );
  50. }
  51. return child;
  52. }
  53. /// If the SVG's path does not start with `assets/`, it is
  54. /// normalized and directed to `assets/images/`
  55. ///
  56. /// If the SVG does not end with `.svg`, then we append the file extension
  57. ///
  58. String _normalized() {
  59. var path = svg.path;
  60. if (!path.toLowerCase().startsWith('assets/')) {
  61. path = 'assets/images/$path';
  62. }
  63. if (!path.toLowerCase().endsWith('.svg')) {
  64. path = '$path.svg';
  65. }
  66. return path;
  67. }
  68. }