Skip to content

Commit

Permalink
Feat: Config copyWith()
Browse files Browse the repository at this point in the history
  • Loading branch information
AmosHuKe committed Sep 15, 2023
1 parent d793629 commit 104f321
Show file tree
Hide file tree
Showing 8 changed files with 367 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.

## 2.0.2

### Improvements

- Config copyWith().

## 2.0.1

### Fixes
Expand Down
47 changes: 47 additions & 0 deletions lib/src/type/tilt_light_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,51 @@ class LightConfig {
/// * 阴影反向 [ShadowConfig.enableReverse]
/// {@endtemplate}
final bool? enableReverse;

LightConfig copyWith({
bool? disable,
Color? color,
double? minIntensity,
double? maxIntensity,
LightDirection? direction,
bool? enableReverse,
}) {
return LightConfig(
disable: disable ?? this.disable,
color: color ?? this.color,
minIntensity: minIntensity ?? this.minIntensity,
maxIntensity: maxIntensity ?? this.maxIntensity,
direction: direction ?? this.direction,
enableReverse: enableReverse ?? this.enableReverse,
);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is LightConfig &&
other.disable == disable &&
other.color == color &&
other.minIntensity == minIntensity &&
other.maxIntensity == maxIntensity &&
other.direction == direction &&
other.enableReverse == enableReverse;
}

@override
int get hashCode {
return Object.hash(
disable,
color,
minIntensity,
maxIntensity,
direction,
enableReverse,
);
}
}
71 changes: 71 additions & 0 deletions lib/src/type/tilt_shadow_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,75 @@ class ShadowConfig {
/// * 失效:光源反向 [LightConfig.enableReverse]
/// {@endtemplate}
final bool? enableReverse;

ShadowConfig copyWith({
bool? disable,
Color? color,
double? minIntensity,
double? maxIntensity,
Offset? offsetInitial,
double? offsetFactor,
double? spreadInitial,
double? spreadFactor,
double? minBlurRadius,
double? maxBlurRadius,
ShadowDirection? direction,
bool? enableReverse,
}) {
return ShadowConfig(
disable: disable ?? this.disable,
color: color ?? this.color,
minIntensity: minIntensity ?? this.minIntensity,
maxIntensity: maxIntensity ?? this.maxIntensity,
offsetInitial: offsetInitial ?? this.offsetInitial,
offsetFactor: offsetFactor ?? this.offsetFactor,
spreadInitial: spreadInitial ?? this.spreadInitial,
spreadFactor: spreadFactor ?? this.spreadFactor,
minBlurRadius: minBlurRadius ?? this.minBlurRadius,
maxBlurRadius: maxBlurRadius ?? this.maxBlurRadius,
direction: direction ?? this.direction,
enableReverse: enableReverse ?? this.enableReverse,
);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is ShadowConfig &&
other.disable == disable &&
other.color == color &&
other.minIntensity == minIntensity &&
other.maxIntensity == maxIntensity &&
other.offsetInitial == offsetInitial &&
other.offsetFactor == offsetFactor &&
other.spreadInitial == spreadInitial &&
other.spreadFactor == spreadFactor &&
other.minBlurRadius == minBlurRadius &&
other.maxBlurRadius == maxBlurRadius &&
other.direction == direction &&
other.enableReverse == enableReverse;
}

@override
int get hashCode {
return Object.hash(
disable,
color,
minIntensity,
maxIntensity,
offsetInitial,
offsetFactor,
spreadInitial,
spreadFactor,
minBlurRadius,
maxBlurRadius,
direction,
enableReverse,
);
}
}
117 changes: 115 additions & 2 deletions lib/src/type/tilt_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:flutter/widgets.dart';

import 'package:flutter_tilt/src/enums.dart';
import 'package:flutter_tilt/src/model/tilt_model.dart';
import 'package:flutter_tilt/src/type/tilt_light_type.dart';
import 'package:flutter_tilt/src/type/tilt_shadow_type.dart';

/// 倾斜回调
typedef TiltCallback = void Function(
Expand Down Expand Up @@ -159,6 +157,107 @@ class TiltConfig {
///
/// `仅对手势 Touch Hover 有效`
final Curve leaveCurve;

TiltConfig copyWith({
bool? disable,
Offset? initial,
double? angle,
List<TiltDirection>? direction,
bool? enableReverse,
FilterQuality? filterQuality,
bool? enableGestureSensors,
double? sensorFactor,
bool? enableSensorRevert,
double? sensorRevertFactor,
Duration? sensorMoveDuration,
bool? enableGestureHover,
bool? enableGestureTouch,
bool? enableRevert,
bool? enableOutsideAreaMove,
Duration? moveDuration,
Duration? leaveDuration,
Curve? moveCurve,
Curve? leaveCurve,
}) {
return TiltConfig(
disable: disable ?? this.disable,
initial: initial ?? this.initial,
angle: angle ?? this.angle,
direction: direction ?? this.direction,
enableReverse: enableReverse ?? this.enableReverse,
filterQuality: filterQuality ?? this.filterQuality,
enableGestureSensors: enableGestureSensors ?? this.enableGestureSensors,
sensorFactor: sensorFactor ?? this.sensorFactor,
enableSensorRevert: enableSensorRevert ?? this.enableSensorRevert,
sensorRevertFactor: sensorRevertFactor ?? this.sensorRevertFactor,
sensorMoveDuration: sensorMoveDuration ?? this.sensorMoveDuration,
enableGestureHover: enableGestureHover ?? this.enableGestureHover,
enableGestureTouch: enableGestureTouch ?? this.enableGestureTouch,
enableRevert: enableRevert ?? this.enableRevert,
enableOutsideAreaMove:
enableOutsideAreaMove ?? this.enableOutsideAreaMove,
moveDuration: moveDuration ?? this.moveDuration,
leaveDuration: leaveDuration ?? this.leaveDuration,
moveCurve: moveCurve ?? this.moveCurve,
leaveCurve: leaveCurve ?? this.leaveCurve,
);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is TiltConfig &&
other.disable == disable &&
other.initial == initial &&
other.angle == angle &&
Object.hashAll(other.direction ?? []) ==
Object.hashAll(direction ?? []) &&
other.enableReverse == enableReverse &&
other.filterQuality == filterQuality &&
other.enableGestureSensors == enableGestureSensors &&
other.sensorFactor == sensorFactor &&
other.enableSensorRevert == enableSensorRevert &&
other.sensorRevertFactor == sensorRevertFactor &&
other.sensorMoveDuration == sensorMoveDuration &&
other.enableGestureHover == enableGestureHover &&
other.enableGestureTouch == enableGestureTouch &&
other.enableRevert == enableRevert &&
other.enableOutsideAreaMove == enableOutsideAreaMove &&
other.moveDuration == moveDuration &&
other.leaveDuration == leaveDuration &&
other.moveCurve == moveCurve &&
other.leaveCurve == leaveCurve;
}

@override
int get hashCode {
return Object.hash(
disable,
initial,
angle,
Object.hashAll(direction ?? []),
enableReverse,
filterQuality,
enableGestureSensors,
sensorFactor,
enableSensorRevert,
sensorRevertFactor,
sensorMoveDuration,
enableGestureHover,
enableGestureTouch,
enableRevert,
enableOutsideAreaMove,
moveDuration,
leaveDuration,
moveCurve,
leaveCurve,
);
}
}

/// child 其他布局
Expand Down Expand Up @@ -298,6 +397,20 @@ class TiltDirection {
TiltDirection operator ~/(double operand) =>
TiltDirection((dx ~/ operand).toDouble(), (dy ~/ operand).toDouble());

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is TiltDirection && other.dx == dx && other.dy == dy;
}

@override
int get hashCode => Object.hash(dx, dy);

@override
String toString() =>
'TiltDirection(${dx.toStringAsFixed(1)}, ${dy.toStringAsFixed(1)})';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: Easily apply tilt parallax hover effects for Flutter, which support
# https://semver.org/spec/v2.0.0-rc.1.html
# https://dart.dev/tools/pub/versioning#semantic-versions
# https://dart.dev/tools/pub/dependencies#version-constraints
version: 2.0.1
version: 2.0.2
homepage: https://amoshuke.github.io/flutter_tilt_book
repository: https://github.com/AmosHuKe/flutter_tilt
issue_tracker: https://github.com/AmosHuKe/flutter_tilt/issues
Expand Down
31 changes: 31 additions & 0 deletions test/type/tilt_light_type_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_tilt/src/enums.dart';
import 'package:flutter_tilt/src/type/tilt_light_type.dart';

void main() {
group('LightConfig', () {
test('copyWith', () {
const LightConfig lightConfig = LightConfig();
const LightConfig lightConfigExpect = LightConfig(
disable: true,
color: Color(0xFFFFFFF0),
minIntensity: 0.0,
maxIntensity: 0.5,
direction: LightDirection.around,
enableReverse: true,
);
final LightConfig lightConfigCopyWith = lightConfig.copyWith(
disable: true,
color: const Color(0xFFFFFFF0),
minIntensity: 0.0,
maxIntensity: 0.5,
direction: LightDirection.around,
enableReverse: true,
);
expect(lightConfig, lightConfig.copyWith());
expect(lightConfigCopyWith, lightConfigExpect);
expect(lightConfigCopyWith.hashCode, lightConfigExpect.hashCode);
});
});
}
43 changes: 43 additions & 0 deletions test/type/tilt_shadow_type_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_tilt/src/enums.dart';
import 'package:flutter_tilt/src/type/tilt_shadow_type.dart';

void main() {
group('ShadowConfig', () {
test('copyWith', () {
const ShadowConfig shadowConfig = ShadowConfig();
const ShadowConfig shadowConfigExpect = ShadowConfig(
disable: true,
color: Color(0xFF9E9E90),
minIntensity: 1.0,
maxIntensity: 1.0,
offsetInitial: Offset(3.0, 3.0),
offsetFactor: 4.0,
spreadInitial: 5.0,
spreadFactor: 6.0,
minBlurRadius: 7.0,
maxBlurRadius: 8.0,
direction: ShadowDirection.all,
enableReverse: true,
);
final ShadowConfig shadowConfigCopyWith = shadowConfig.copyWith(
disable: true,
color: const Color(0xFF9E9E90),
minIntensity: 1.0,
maxIntensity: 1.0,
offsetInitial: const Offset(3.0, 3.0),
offsetFactor: 4.0,
spreadInitial: 5.0,
spreadFactor: 6.0,
minBlurRadius: 7.0,
maxBlurRadius: 8.0,
direction: ShadowDirection.all,
enableReverse: true,
);
expect(shadowConfig, shadowConfig.copyWith());
expect(shadowConfigCopyWith, shadowConfigExpect);
expect(shadowConfigCopyWith.hashCode, shadowConfigExpect.hashCode);
});
});
}
Loading

0 comments on commit 104f321

Please sign in to comment.