Skip to content

Commit 9d88c30

Browse files
committed
feat: add aspect ratio support to ShaderInfo and update ShaderAnimationView for responsive layout
1 parent c1a0ff0 commit 9d88c30

File tree

2 files changed

+69
-52
lines changed

2 files changed

+69
-52
lines changed

lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ShaderInfo {
3535
final String path;
3636
final EdgeInsetsGeometry? padding;
3737
final Color? backgroundColor;
38+
final double? aspectRatio;
3839

3940
const ShaderInfo({
4041
required this.name,
@@ -47,6 +48,7 @@ class ShaderInfo {
4748
required this.path,
4849
this.padding,
4950
this.backgroundColor,
51+
this.aspectRatio,
5052
});
5153

5254
ShaderMetadata get metadata => ShaderMetadata(
@@ -108,6 +110,7 @@ final shaders = [
108110
dateAdded: DateTime(2025, 7, 29),
109111
builder: const RingsShaderBuilder(),
110112
path: 'rings-shader',
113+
aspectRatio: 1,
111114
),
112115
ShaderInfo(
113116
name: 'Clearly a Bug',
@@ -130,6 +133,7 @@ final shaders = [
130133
path: 'ai-assistant',
131134
padding: EdgeInsets.all(32),
132135
backgroundColor: Colors.black,
136+
aspectRatio: 1,
133137
),
134138
];
135139

lib/widgets/shader_animation_view.dart

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -52,62 +52,75 @@ class _ShaderAnimationViewState extends State<ShaderAnimationView> with SingleTi
5252

5353
@override
5454
Widget build(BuildContext context) {
55-
return ColoredBox(
56-
color: widget.shaderInfo.backgroundColor ?? Colors.transparent,
57-
child: Padding(
58-
padding: widget.shaderInfo.padding ?? EdgeInsets.zero,
59-
child: LayoutBuilder(
60-
builder: (context, size) {
61-
return AnimatedBuilder(
62-
animation: _controller,
63-
builder: (context, child) {
64-
return ShaderBuilder(
65-
assetKey: widget.shaderInfo.assetKey,
66-
(context, shader, _) {
67-
final duration = widget.shaderInfo.builder.animationDuration;
68-
double timeValue;
55+
Widget child = Padding(
56+
padding: widget.shaderInfo.padding ?? EdgeInsets.zero,
57+
child: LayoutBuilder(
58+
builder: (context, size) {
59+
return AnimatedBuilder(
60+
animation: _controller,
61+
builder: (context, child) {
62+
return ShaderBuilder(
63+
assetKey: widget.shaderInfo.assetKey,
64+
(context, shader, _) {
65+
final duration = widget.shaderInfo.builder.animationDuration;
66+
double timeValue;
6967

70-
if (duration != null) {
71-
// Bounded animation - use animated value between 0-1
72-
final animation = TweenSequence<double>([
73-
TweenSequenceItem(
74-
tween: Tween<double>(
75-
begin: 0.0,
76-
end: 1.0,
77-
).chain(CurveTween(curve: Curves.easeInOut)),
78-
weight: 50.0,
79-
),
80-
TweenSequenceItem(
81-
tween: Tween<double>(
82-
begin: 1.0,
83-
end: 0.0,
84-
).chain(CurveTween(curve: Curves.easeInOut)),
85-
weight: 50.0,
86-
),
87-
]).animate(_controller);
88-
timeValue = animation.value;
89-
} else {
90-
// Unbounded animation - use controller value as continuous time
91-
// Scale the 0-1 controller value to actual time in seconds
92-
timeValue = _controller.value * _controller.duration!.inSeconds;
93-
}
68+
if (duration != null) {
69+
// Bounded animation - use animated value between 0-1
70+
final animation = TweenSequence<double>([
71+
TweenSequenceItem(
72+
tween: Tween<double>(
73+
begin: 0.0,
74+
end: 1.0,
75+
).chain(CurveTween(curve: Curves.easeInOut)),
76+
weight: 50.0,
77+
),
78+
TweenSequenceItem(
79+
tween: Tween<double>(
80+
begin: 1.0,
81+
end: 0.0,
82+
).chain(CurveTween(curve: Curves.easeInOut)),
83+
weight: 50.0,
84+
),
85+
]).animate(_controller);
86+
timeValue = animation.value;
87+
} else {
88+
// Unbounded animation - use controller value as continuous time
89+
// Scale the 0-1 controller value to actual time in seconds
90+
timeValue = _controller.value * _controller.duration!.inSeconds;
91+
}
9492

95-
widget.shaderInfo.builder.setUniforms(shader, size.biggest, timeValue);
93+
widget.shaderInfo.builder.setUniforms(shader, size.biggest, timeValue);
9694

97-
return widget.shaderInfo.builder.buildShader(
98-
widget.shaderInfo.metadata,
99-
shader,
100-
size.biggest,
101-
timeValue,
102-
widget.shaderInfo.builder.childBuilder(context),
103-
);
104-
},
105-
);
106-
},
107-
);
108-
},
109-
),
95+
return widget.shaderInfo.builder.buildShader(
96+
widget.shaderInfo.metadata,
97+
shader,
98+
size.biggest,
99+
timeValue,
100+
widget.shaderInfo.builder.childBuilder(context),
101+
);
102+
},
103+
);
104+
},
105+
);
106+
},
110107
),
111108
);
109+
110+
if (widget.shaderInfo.aspectRatio != null) {
111+
child = AspectRatio(
112+
aspectRatio: widget.shaderInfo.aspectRatio!,
113+
child: child,
114+
);
115+
}
116+
117+
if (widget.shaderInfo.backgroundColor != null) {
118+
child = ColoredBox(
119+
color: widget.shaderInfo.backgroundColor!,
120+
child: Center(child: child),
121+
);
122+
}
123+
124+
return child;
112125
}
113126
}

0 commit comments

Comments
 (0)