Skip to content

Commit ce773a6

Browse files
authored
Merge pull request #3 from JulienDev/main
add tint color support
2 parents 2ab7082 + 834fab9 commit ce773a6

File tree

8 files changed

+29
-23
lines changed

8 files changed

+29
-23
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ See the example folder for a complete example.
1515
```dart
1616
import 'package:progressive_blur/progressive_blur.dart';
1717
18-
// Simple gradient blur
18+
// Simple gradient blur with optional tint
1919
ProgressiveBlurWidget(
2020
sigma: 24.0,
2121
linearGradientBlur: const LinearGradientBlur(
@@ -24,6 +24,7 @@ ProgressiveBlurWidget(
2424
start: Alignment.topCenter,
2525
end: Alignment.bottomCenter,
2626
),
27+
tintColor: Colors.orange.withOpacity(0.3), //optional
2728
child: ...
2829
);
2930
@@ -33,6 +34,7 @@ ProgressiveBlurWidget(
3334
ProgressiveBlurWidget.custom(
3435
sigma: 24.0,
3536
blurTexture: [instance of ui.Image],
37+
tintColor: Colors.purple.withOpacity(0.4), // optional
3638
child: ...,
3739
)
3840
```

example/android/app/src/debug/AndroidManifest.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

example/android/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
3+
<uses-permission android:name="android.permission.INTERNET"/>
4+
25
<application
36
android:label="example"
47
android:name="${applicationName}"

example/android/app/src/profile/AndroidManifest.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

example/lib/main.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:convert';
2+
import 'dart:math' as math;
23

34
import 'package:example/albums.dart';
45
import 'package:figma_squircle/figma_squircle.dart';
@@ -76,8 +77,7 @@ class _MainPageState extends State<MainPage> {
7677
sliver: SliverLayoutBuilder(
7778
builder: (context, constraints) {
7879
const preferredItemSize = 160.0;
79-
final crossAxisCount =
80-
(constraints.crossAxisExtent / preferredItemSize).floor();
80+
final crossAxisCount = math.max(1, (constraints.crossAxisExtent / preferredItemSize).floor());
8181

8282
return SliverGrid.builder(
8383
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
@@ -192,6 +192,7 @@ class _AlbumCardState extends State<_AlbumCard>
192192
cornerSmoothing: 0.6,
193193
),
194194
child: ProgressiveBlurWidget(
195+
tintColor: Colors.black.withValues(alpha: 0.5),
195196
linearGradientBlur: const LinearGradientBlur(
196197
values: [0, 1],
197198
stops: [0.5, 0.8],

example/pubspec.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ packages:
241241
path: ".."
242242
relative: true
243243
source: path
244-
version: "0.0.0"
244+
version: "0.0.1"
245245
proj4dart:
246246
dependency: transitive
247247
description:
@@ -353,4 +353,4 @@ packages:
353353
version: "2.0.0"
354354
sdks:
355355
dart: ">=3.7.0-0 <4.0.0"
356-
flutter: ">=3.27.0"
356+
flutter: ">=3.30.0"

lib/progressive_blur.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ class ProgressiveBlurWidget extends StatefulWidget {
2727
required this.sigma,
2828
required this.child,
2929
this.blurTextureDimensions = 128,
30+
this.tintColor = Colors.transparent,
3031
}) : blurTexture = null;
3132

3233
const ProgressiveBlurWidget.custom({
3334
super.key,
3435
required this.blurTexture,
3536
required this.sigma,
3637
required this.child,
38+
this.tintColor = Colors.transparent,
3739
}) : linearGradientBlur = null,
3840
// Irrelevant in case of a custom blur texture
3941
blurTextureDimensions = -1;
@@ -63,6 +65,9 @@ class ProgressiveBlurWidget extends StatefulWidget {
6365
/// The standard deviation of the Gaussian blur.
6466
final double sigma;
6567

68+
/// Tint color to apply to the blurred area.
69+
final Color tintColor;
70+
6671
/// The widget to be blurred.
6772
final Widget child;
6873

@@ -148,6 +153,10 @@ class _ProgressiveBlurWidgetState extends State<ProgressiveBlurWidget> {
148153
shader.setFloat(1, scaledSize.height); // child_size.y
149154
shader.setFloat(2, widget.sigma); // blur_sigma
150155
shader.setFloat(3, 0.0); // blur_direction
156+
shader.setFloat(4, widget.tintColor.r); // tint.r
157+
shader.setFloat(5, widget.tintColor.g); // tint.g
158+
shader.setFloat(6, widget.tintColor.b); // tint.b
159+
shader.setFloat(7, widget.tintColor.a); // tint.a
151160

152161
// Draw the first pass
153162
final paint = Paint()..shader = shader;

lib/shaders/progressive_blur.frag

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ uniform sampler2D blur_texture;
1010
uniform float blur_sigma;
1111
uniform float blur_direction; // 0 for horizontal, 1 for vertical
1212

13+
uniform vec4 tint_color;
14+
1315
out vec4 frag_color;
1416

1517
void main() {
1618
vec2 uv = FlutterFragCoord().xy / child_size;
1719

1820
// Squaring the blur texture value makes it look more consistent?
19-
float sigma = blur_sigma * (pow(texture(blur_texture, uv).r, 2));
20-
vec2 dir = blur_direction == 0.0? vec2(1.0, 0.0) : vec2(0.0, 1.0);
21+
float blur_value = pow(texture(blur_texture, uv).r, 2.0);
22+
float sigma = blur_sigma * blur_value;
23+
vec2 dir = blur_direction == 0.0 ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
2124

2225
if (sigma < 1e-5) {
2326
frag_color = texture(child_texture, uv);
@@ -46,6 +49,8 @@ void main() {
4649

4750
color += texture(child_texture, uv + offset) * weight;
4851
}
49-
50-
frag_color = color / total_weight;
52+
53+
vec4 blurred = color / total_weight;
54+
float tint_strength = blur_value * tint_color.a;
55+
frag_color = mix(blurred, tint_color, tint_strength);
5156
}

0 commit comments

Comments
 (0)