Skip to content

Commit

Permalink
Merge pull request #45 from splashbyte/44-indicator-gradient
Browse files Browse the repository at this point in the history
Adds indicatorGradient to ToggleStyle
  • Loading branch information
maeddin authored Oct 7, 2023
2 parents 10e058a + 47ad88f commit 5675799
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.8.0+1
- fixes changelog of version 0.8.0
## 0.8.1

- adds `indicatorGradient` to `ToggleStyle` ([#44](https://github.com/splashbyte/animated_toggle_switch/issues/44))
- fixes problems with `backgroundGradient` and `backgroundColor` ([#46](https://github.com/splashbyte/animated_toggle_switch/issues/46))

## 0.8.0 (2023-09-02)

Expand Down
4 changes: 4 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@ class _MyHomePageState extends State<MyHomePage> {
borderColor: Colors.transparent,
borderRadius: BorderRadius.circular(10.0),
),
styleBuilder: (i) => ToggleStyle(
indicatorGradient: LinearGradient(
colors: [colorBuilder(i), colorBuilder((i + 1) % 4)]),
),
height: 55,
spacing: 20.0,
loading: loading,
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.8.0"
version: "0.8.1"
async:
dependency: transitive
description:
Expand Down
18 changes: 16 additions & 2 deletions lib/src/style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class ToggleStyle {
/// Defaults to [ThemeData.colorScheme.secondary].
final Color? indicatorColor;

/// Gradient of the indicator. Overwrites [indicatorColor] if not [null].
final Gradient? indicatorGradient;

/// Background color of the switch.
///
/// Defaults to [ThemeData.colorScheme.surface].
Expand Down Expand Up @@ -41,6 +44,7 @@ class ToggleStyle {
/// Default constructor for [ToggleStyle].
const ToggleStyle({
this.indicatorColor,
this.indicatorGradient,
this.backgroundColor,
this.backgroundGradient,
this.borderColor,
Expand All @@ -52,8 +56,9 @@ class ToggleStyle {
});

/// Private constructor for setting all possible parameters.
ToggleStyle._({
const ToggleStyle._({
required this.indicatorColor,
required this.indicatorGradient,
required this.backgroundColor,
required this.backgroundGradient,
required this.borderColor,
Expand All @@ -72,9 +77,11 @@ class ToggleStyle {
? this
: ToggleStyle._(
indicatorColor: other.indicatorColor ?? indicatorColor,
indicatorGradient: other.indicatorGradient ??
(other.indicatorColor != null ? null : indicatorGradient),
backgroundColor: other.backgroundColor ?? backgroundColor,
backgroundGradient: other.backgroundGradient ??
(other.backgroundColor == null ? null : backgroundGradient),
(other.backgroundColor != null ? null : backgroundGradient),
borderColor: other.borderColor ?? borderColor,
borderRadius: other.borderRadius ?? borderRadius,
indicatorBorderRadius: other.indicatorBorderRadius ??
Expand All @@ -93,6 +100,11 @@ class ToggleStyle {
ToggleStyle._(
indicatorColor:
Color.lerp(style1.indicatorColor, style2.indicatorColor, t),
indicatorGradient: Gradient.lerp(
style1.indicatorGradient ?? style1.indicatorColor?.toGradient(),
style2.indicatorGradient ?? style2.indicatorColor?.toGradient(),
t,
),
backgroundColor:
Color.lerp(style1.backgroundColor, style2.backgroundColor, t),
backgroundGradient: Gradient.lerp(
Expand Down Expand Up @@ -136,6 +148,7 @@ class ToggleStyle {
other is ToggleStyle &&
runtimeType == other.runtimeType &&
indicatorColor == other.indicatorColor &&
indicatorGradient == other.indicatorGradient &&
backgroundColor == other.backgroundColor &&
backgroundGradient == other.backgroundGradient &&
borderColor == other.borderColor &&
Expand All @@ -148,6 +161,7 @@ class ToggleStyle {
@override
int get hashCode =>
indicatorColor.hashCode ^
indicatorGradient.hashCode ^
backgroundColor.hashCode ^
backgroundGradient.hashCode ^
borderColor.hashCode ^
Expand Down
9 changes: 7 additions & 2 deletions lib/src/widgets/animated_toggle_switch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,15 @@ class AnimatedToggleSwitch<T extends Object?>

/// The [AnimationType] for [styleBuilder].
///
/// The [AnimationType] for [ToggleStyle.indicatorColor], [ToggleStyle.indicatorBorderRadius],
/// The [AnimationType] for [ToggleStyle.indicatorColor],
/// [ToggleStyle.indicatorGradient], [ToggleStyle.indicatorBorderRadius],
/// [ToggleStyle.indicatorBorder] and [ToggleStyle.indicatorBoxShadow].
/// is managed separately with [indicatorAnimationType].
final AnimationType styleAnimationType;

/// The [AnimationType] for the [ToggleStyle.indicatorColor].
/// The [AnimationType] for [ToggleStyle.indicatorColor],
/// [ToggleStyle.indicatorGradient], [ToggleStyle.indicatorBorderRadius],
/// [ToggleStyle.indicatorBorder] and [ToggleStyle.indicatorBoxShadow]
///
/// For the other style parameters, please use [styleAnimationType].
final AnimationType indicatorAnimationType;
Expand Down Expand Up @@ -1020,6 +1023,7 @@ class AnimatedToggleSwitch<T extends Object?>
BorderRadius.circular(height / 2);
final style = ToggleStyle._(
indicatorColor: theme.colorScheme.secondary,
indicatorGradient: null,
backgroundColor: theme.colorScheme.surface,
backgroundGradient: null,
borderColor: theme.colorScheme.secondary,
Expand Down Expand Up @@ -1227,6 +1231,7 @@ class AnimatedToggleSwitch<T extends Object?>
return DecoratedBox(
decoration: BoxDecoration(
color: style.indicatorColor,
gradient: style.indicatorGradient,
borderRadius: style.indicatorBorderRadius,
border: style.indicatorBorder,
boxShadow: style.indicatorBoxShadow,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: animated_toggle_switch
description: Fully customizable, draggable and animated switch with multiple choices and smooth loading animation. It has prebuilt constructors for rolling and size animations.
version: 0.8.0
version: 0.8.1
repository: https://github.com/SplashByte/animated_toggle_switch
issue_tracker: https://github.com/SplashByte/animated_toggle_switch/issues

Expand Down

0 comments on commit 5675799

Please sign in to comment.