Skip to content

Commit

Permalink
Merge branch 'features/gradients' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
TesteurManiak committed Oct 14, 2021
2 parents 4fea48e + 214bb21 commit 4c1c8fc
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 14 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Add decoration capabilities for the `Icon` widget with shadows, borders, gradien

* Add shadows to icons
* Add borders to icons
* Add gradients to icons

![](https://raw.githubusercontent.com/TesteurManiak/icon_decoration/main/test/goldens/shadow_base_widget.png)

Expand Down Expand Up @@ -38,7 +39,3 @@ DecoratedIcon(
),
)
```

## TODO

* Add support for gradients
12 changes: 11 additions & 1 deletion lib/src/decorated_icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ class DecoratedIcon extends StatelessWidget {
final iconSize = icon.size ?? iconTheme.size ?? 24.0;
final iconOpacity = iconTheme.opacity ?? 1.0;
final border = decoration?.border;
final gradient = decoration?.gradient;
Color iconColor = icon.color ?? iconTheme.color!;
if (iconOpacity != 1.0) {
iconColor = iconColor.withOpacity(iconColor.opacity * iconOpacity);
}

final iconStyle = TextStyle(
TextStyle iconStyle = TextStyle(
inherit: false,
color: iconColor,
fontSize: iconSize,
Expand All @@ -35,6 +36,15 @@ class DecoratedIcon extends StatelessWidget {
shadows: decoration?.shadows,
);

if (gradient != null) {
iconStyle = iconStyle.copyWith(
foreground: Paint()
..shader = gradient.createShader(
Rect.fromLTWH(0, 0, iconSize, iconSize),
),
);
}

Widget iconWidget = RichText(
overflow: TextOverflow.visible,
textDirection: textDirection,
Expand Down
9 changes: 7 additions & 2 deletions lib/src/icon_decoration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ class IconDecoration {
/// Multiple shadows are supported to replicate lighting from multiple light
/// sources.
final List<Shadow>? shadows;

/// A border to draw above the icon color or [gradient].
final IconBorder? border;
// final Gradient? gradient;

/// Apply a gradient to the icon. If this is specified the [Icon.color]
/// property has no effect.
final Gradient? gradient;

/// Creates a `DecoratedIcon` decoration.
const IconDecoration({
this.shadows,
this.border,
// this.gradient,
this.gradient,
});
}
3 changes: 0 additions & 3 deletions test/borders_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ void main() {
),
),
);
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
});

customGoldenTest('Border with colorized icon', (tester) async {
Expand All @@ -34,7 +33,6 @@ void main() {
),
),
);
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
});

customGoldenTest('Border with shadows', (tester) async {
Expand All @@ -58,7 +56,6 @@ void main() {
),
),
);
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
});
});
}
Binary file added test/goldens/base_gradient.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/gradient_with_border.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/gradient_with_border_and_shadow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/goldens/gradient_with_shadows.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 110 additions & 0 deletions test/gradient_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:icon_decoration/icon_decoration.dart';

import 'test_utils/base_widget.dart';
import 'test_utils/custom_golden.dart';

void main() {
group('Goldens Gradients', () {
const _rainbowGradient = LinearGradient(colors: [
Colors.red,
Colors.pink,
Colors.purple,
Colors.deepPurple,
Colors.deepPurple,
Colors.indigo,
Colors.blue,
Colors.lightBlue,
Colors.cyan,
Colors.teal,
Colors.green,
Colors.lightGreen,
Colors.lime,
Colors.yellow,
Colors.amber,
Colors.orange,
Colors.deepOrange,
]);

customGoldenTest('Base gradient', (tester) async {
tester.binding.window.physicalSizeTestValue = const Size.square(100);
await tester.pumpWidget(
generateBaseApp(
child: const Center(
child: DecoratedIcon(
icon: Icon(Icons.all_inbox),
decoration: IconDecoration(
gradient: _rainbowGradient,
),
),
),
),
);
});

customGoldenTest('Gradient with shadows', (tester) async {
tester.binding.window.physicalSizeTestValue = const Size.square(100);
await tester.pumpWidget(
generateBaseApp(
child: const Center(
child: DecoratedIcon(
icon: Icon(Icons.all_inbox),
decoration: IconDecoration(
gradient: _rainbowGradient,
shadows: [
Shadow(
color: Colors.red,
blurRadius: 3,
offset: Offset(0, 2),
)
],
),
),
),
),
);
});

customGoldenTest('Gradient with border', (tester) async {
tester.binding.window.physicalSizeTestValue = const Size.square(100);
await tester.pumpWidget(
generateBaseApp(
child: const Center(
child: DecoratedIcon(
icon: Icon(Icons.all_inbox),
decoration: IconDecoration(
gradient: _rainbowGradient,
border: IconBorder(),
),
),
),
),
);
});

customGoldenTest('Gradient with border and shadow', (tester) async {
tester.binding.window.physicalSizeTestValue = const Size.square(100);
await tester.pumpWidget(
generateBaseApp(
child: const Center(
child: DecoratedIcon(
icon: Icon(Icons.all_inbox),
decoration: IconDecoration(
gradient: _rainbowGradient,
border: IconBorder(),
shadows: [
Shadow(
color: Colors.red,
blurRadius: 3,
offset: Offset(0, 2),
)
],
),
),
),
),
);
});
});
}
4 changes: 0 additions & 4 deletions test/shadows_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ void main() {
child: baseWidget,
backgroundColor: Colors.black,
));
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
});

customGoldenTest('Default shadow', (tester) async {
Expand All @@ -28,7 +27,6 @@ void main() {
),
),
);
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
});

customGoldenTest('Shadow with Offset(0, 6)', (tester) async {
Expand All @@ -45,7 +43,6 @@ void main() {
),
),
);
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
});

customGoldenTest('Shadow with Offset(3, 0)', (tester) async {
Expand All @@ -62,7 +59,6 @@ void main() {
),
),
);
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
});
});
}
1 change: 1 addition & 0 deletions test/test_utils/custom_golden.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ void customGoldenTest(
.replaceAll(' ', '_')
.replaceAll(RegExp(r'(?:|[^\w\s])+'), ''),
);
addTearDown(tester.binding.window.clearPhysicalSizeTestValue);
});
}

0 comments on commit 4c1c8fc

Please sign in to comment.