Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
> [!IMPORTANT]
> See the [Migration Guides](https://amoshuke.github.io/flutter_tilt_book/en/v4/docs/migration-guides/) for the details of breaking changes between versions.

## 4.0.2 (2026-06-05)

**Fixes**

- Fix light/shadow not reaching `maxIntensity` for the `LightDirection.around` and `LightDirection.all` directions. ([#42](https://github.com/fluttercandies/flutter_tilt/pull/42))
Now that it is fixed, if the effect looks too strong, set `maxIntensity` to the square of its previous value (e.g. `0.5` → `0.25`, `0.6` → `0.36`).

## 4.0.1 (2026-06-04)

**Improvements**
Expand Down
2 changes: 1 addition & 1 deletion lib/src/config/tilt_light_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LightConfig {
this.disable = false,
this.color = const Color(0xFFFFFFFF),
this.minIntensity = 0.0,
this.maxIntensity = 0.5,
this.maxIntensity = 0.25,
this.spreadFactor = 4.0,
this.direction = LightDirection.around,
this.enableReverse,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/config/tilt_shadow_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class ShadowConfig {
this.disable = false,
this.color = const Color(0xFF9E9E9E),
this.minIntensity = 0.0,
this.maxIntensity = 0.5,
this.maxIntensity = 0.25,
this.offsetInitial = Offset.zero,
this.offsetFactor = 0.1,
this.direction,
Expand Down
103 changes: 36 additions & 67 deletions lib/src/internal/mixin/tilt_decoration_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '../../enums.dart';
import '../../utils/utils.dart';

mixin TiltDecorationMixin {
/// 计算提供的方向进度
/// 计算提供的方向强度
///
/// 范围:0-1
///
Expand All @@ -14,169 +14,138 @@ mixin TiltDecorationMixin {
/// - [ShadowDirection] 阴影方向
///
/// 可选项
/// - [min] 最小进度限制 0-1
/// - [max] 最大进度限制 0-1
/// - [enableReverse] 开启反向
/// - [min] 最小强度限制 0-1
/// - [max] 最大强度限制 0-1
///
/// @return [double] 方向进度 0-1
double tiltDecorationDirectionProgress(
/// @return [double] 方向强度,映射到 [min] - [max] 区间
double tiltDecorationDirectionIntensity(
Offset areaProgress,
Direction direction, {
double min = 0.0,
double max = 1.0,
bool enableReverse = false,
}) {
assert(min <= max && min >= 0.0 && max <= 1.0);

/// 区域进度(取反)
final progress = -areaProgress;
final progressX = progress.dx, progressY = progress.dy;

/// 根据方向计算进度
var progressData = _calculateDirectionProgress(
/// 根据方向计算强度
var intensity = _calculateDirectionIntensity(
direction,
progressX,
progressY,
min,
max,
);

/// 强度调整
progressData *= max;
/// 将强度限制在 0-1
intensity = intensity.clamp(0.0, 1.0);

/// 反向处理
if (enableReverse) progressData = -progressData;
/// 按 min-max 区间映射:强度 0 对应 min,强度 1 对应 max
intensity = min + intensity * (max - min);

/// 避免超出范围
progressData = progressData.clamp(min, max);

return progressData;
return intensity;
}

/// 根据方向计算进度
/// 根据方向计算强度
///
/// - [direction] 光照方向
/// - [progressX] 当前水平进度
/// - [progressY] 当前垂直进度
/// - [min] 最小进度限制 0-1
/// - [max] 最大进度限制 0-1
///
/// @return 对应方向的进度
double _calculateDirectionProgress(
/// @return 对应方向的强度
double _calculateDirectionIntensity(
Direction direction,
double progressX,
double progressY,
double min,
double max,
) {
return switch (direction) {
LightDirection _ => _calculateLightDirectionProgress(
LightDirection _ => _calculateLightDirectionIntensity(
direction,
progressX,
progressY,
min,
max,
),
ShadowDirection _ => _calculateShadowDirectionProgress(
ShadowDirection _ => _calculateShadowDirectionIntensity(
direction,
progressX,
progressY,
min,
max,
),
};
}

/// 计算距离中心点的距离,并限制在最小和最大范围内
/// 计算距离中心点的距离,并限制在 0-1 范围内
///
/// - [progressX] 当前水平进度
/// - [progressY] 当前垂直进度
/// - [min] 最小进度限制 0-1
/// - [max] 最大进度限制 0-1
///
/// @return 对应方向的进度
/// @return 限制在 0-1 范围内的距离
double _constrainedDistance(
double progressX,
double progressY,
double min,
double max,
) {
final distance = Utils.p2pDistance(
Offset.zero,
Offset(progressX, progressY),
);
return distance.clamp(min, max);
return distance.clamp(0, 1);
}

/// 计算光照方向的进度
/// 计算光照方向的强度
///
/// - [direction] 光照方向
/// - [progressX] 当前水平进度
/// - [progressY] 当前垂直进度
/// - [min] 最小进度限制 0-1
/// - [max] 最大进度限制 0-1
///
/// @return 对应方向的进度
double _calculateLightDirectionProgress(
/// @return 对应方向的强度
double _calculateLightDirectionIntensity(
LightDirection direction,
double progressX,
double progressY,
double min,
double max,
) {
return switch (direction) {
LightDirection.none => 0.0,
LightDirection.around =>
_constrainedDistance(progressX, progressY, min, max),
LightDirection.all => max,
LightDirection.around => _constrainedDistance(progressX, progressY),
LightDirection.all => 1,
LightDirection.top => progressY,
LightDirection.bottom => -progressY,
LightDirection.left => progressX,
LightDirection.right => -progressX,
LightDirection.center =>
max - _constrainedDistance(progressX, progressY, min, max),
LightDirection.center => 1 - _constrainedDistance(progressX, progressY),
LightDirection.topLeft => progressX + progressY,
LightDirection.bottomRight => -(progressX + progressY),
LightDirection.topRight => -(progressX - progressY),
LightDirection.bottomLeft => progressX - progressY,
LightDirection.xCenter => max - progressY.abs(),
LightDirection.yCenter => max - progressX.abs(),
LightDirection.xCenter => 1 - progressY.abs(),
LightDirection.yCenter => 1 - progressX.abs(),
};
}

/// 计算阴影方向的进度
/// 计算阴影方向的强度
///
/// - [direction] 阴影方向
/// - [progressX] 当前水平进度
/// - [progressY] 当前垂直进度
/// - [min] 最小进度限制 0-1
/// - [max] 最大进度限制 0-1
///
/// @return 对应方向的进度
double _calculateShadowDirectionProgress(
/// @return 对应方向的强度
double _calculateShadowDirectionIntensity(
ShadowDirection direction,
double progressX,
double progressY,
double min,
double max,
) {
return switch (direction) {
ShadowDirection.none => 0.0,
ShadowDirection.around =>
_constrainedDistance(progressX, progressY, min, max),
ShadowDirection.all => max,
ShadowDirection.around => _constrainedDistance(progressX, progressY),
ShadowDirection.all => 1,
ShadowDirection.top => -progressY,
ShadowDirection.bottom => progressY,
ShadowDirection.left => -progressX,
ShadowDirection.right => progressX,
ShadowDirection.center =>
max - _constrainedDistance(progressX, progressY, min, max),
ShadowDirection.center => 1 - _constrainedDistance(progressX, progressY),
ShadowDirection.topLeft => -(progressX + progressY),
ShadowDirection.bottomRight => progressX + progressY,
ShadowDirection.topRight => progressX - progressY,
ShadowDirection.bottomLeft => -(progressX - progressY),
ShadowDirection.xCenter => max - progressY.abs(),
ShadowDirection.yCenter => max - progressX.abs(),
ShadowDirection.xCenter => 1 - progressY.abs(),
ShadowDirection.yCenter => 1 - progressX.abs(),
};
}
}
6 changes: 3 additions & 3 deletions lib/src/widgets/effects/tilt_light.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ class TiltLight extends StatelessWidget with TiltDecorationMixin {
/// 定位 y (从中心位置开始)
final positionY = p2cPosition.dy;

/// 光源方向进度
final showProgress = tiltDecorationDirectionProgress(
/// 光源方向强度
final intensity = tiltDecorationDirectionIntensity(
areaProgress,
lightConfig.direction,
min: lightConfig.minIntensity,
max: lightConfig.maxIntensity,
);

/// 光源颜色的 Alpha 进度
final alphaProgress = 255.0 * showProgress;
final alphaProgress = 255.0 * intensity;

return Positioned(
left: enableReverse ? positionX : null,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/widgets/effects/tilt_shadow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class TiltShadow<TShadowConfig extends ShadowConfig>
double get showShadow {
final direction =
(shadowConfig.direction ?? lightConfig.direction) as Direction;
return tiltDecorationDirectionProgress(
return tiltDecorationDirectionIntensity(
areaProgress,
direction,
min: shadowConfig.minIntensity,
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.html
# https://dart.dev/tools/pub/versioning#semantic-versions
# https://dart.dev/tools/pub/dependencies#version-constraints
version: 4.0.1
version: 4.0.2
homepage: https://amoshuke.github.io/flutter_tilt_book
documentation: https://amoshuke.github.io/flutter_tilt_book
repository: https://github.com/fluttercandies/flutter_tilt
Expand Down
Loading
Loading