Where does a Fragment Shader execute in Flutter?
Fragment shaders run massively parallel programs on the GPU for every single pixel on screen.
Unlock cinematic visual effects in Flutter using GLSL fragment shaders and Impeller
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Flutter's rendering engine rewrite—Impeller—brings unprecedented graphics capabilities to mobile applications. With Impeller's first-class support for custom fragment shaders, developers can create cinematic visual effects: liquid glass reflections, animated mesh gradients, water ripples, and chromatic aberrations that run at a rock-solid 120 FPS.
In this lesson, you will learn what fragment shaders are, how to write GLSL code, and how to execute shaders in Flutter using FragmentProgram.
In 2D canvas drawing (CustomPainter), your CPU issues vector draw commands (draw lines, draw circles). The GPU rasterizes these shapes.
A Fragment Shader is a specialized mini-program written in GLSL (OpenGL Shading Language) that executes directly on the GPU cores for every single pixel on your canvas simultaneously:
fragColor for that exact pixel.wave.frag)Create a file named shaders/wave.frag in your project root:
1// shaders/wave.frag2#include <flutter/runtime_effect.glsl>34// Inputs passed from Flutter (Uniforms)5uniform vec2 uResolution; // Canvas width and height6uniform float uTime; // Elapsed animation time78out vec4 fragColor; // Color output for current pixel910void main() {11// Normalize pixel coordinates (0.0 to 1.0)12vec2 uv = FlutterFragCoord().xy / uResolution;1314// Create an animated sinusoidal wave pattern:15float wave = sin(uv.x * 10.0 + uTime * 3.0) * 0.1;16float dist = abs(uv.y - 0.5 - wave);1718// Color gradient based on wave distance:19vec3 color = vec3(0.1, 0.5, 0.9) * (0.05 / dist);2021fragColor = vec4(color, 1.0);22}
pubspec.yaml:flutter:
shaders:
- shaders/wave.frag
FragmentProgram.fromAsset() and bind it to a CustomPainter:1import 'dart:ui' as ui;2import 'package:flutter/material.dart';34class ShaderScreen extends StatefulWidget {5const ShaderScreen({super.key});67@override8State<ShaderScreen> createState() => _ShaderScreenState();9}1011class _ShaderScreenState extends State<ShaderScreen> with SingleTickerProviderStateMixin {12ui.FragmentShader? _shader;13late final AnimationController _controller;1415@override16void initState() {17super.initState();18_controller = AnimationController(vsync: this, duration: const Duration(seconds: 4))..repeat();19_loadShader();20}2122Future<void> _loadShader() async {23final program = await ui.FragmentProgram.fromAsset('shaders/wave.frag');24setState(() => _shader = program.fragmentShader());25}2627@override28void dispose() {29_controller.dispose();30_shader?.dispose();31super.dispose();32}3334@override35Widget build(BuildContext context) {36if (_shader == null) return const Center(child: CircularProgressIndicator());3738return AnimatedBuilder(39animation: _controller,40builder: (context, _) => CustomPaint(41size: Size.infinite,42painter: ShaderPainter(shader: _shader!, time: _controller.value * 6.28),43),44);45}46}4748class ShaderPainter extends CustomPainter {49final ui.FragmentShader shader;50final float time;5152ShaderPainter({required this.shader, required this.time});5354@override55void paint(Canvas canvas, Size size) {56// 1. Pass uniforms in exact order declared in GLSL:57shader.setFloat(0, size.width); // uResolution.x58shader.setFloat(1, size.height); // uResolution.y59shader.setFloat(2, time); // uTime6061// 2. Draw canvas with shader paint:62final paint = Paint()..shader = shader;63canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), paint);64}6566@override67bool shouldRepaint(covariant ShaderPainter oldDelegate) => true;68}
shader.setFloat(index, value) uses 0-based indices matching the exact sequence of uniform variables defined in the GLSL file. A vec2 consumes 2 slots (index 0 and 1).
Always dispose of FragmentShader instances inside your State.dispose() method. Leaving GPU shaders allocated leaks graphics memory buffers.
Unlock cinematic visual effects in Flutter using GLSL fragment shaders and Impeller. Animation gives feedback and personality to UI when it supports user intent clearly.
Operate with production and interview-level clarity.
Animate only what helps understanding, and keep transition timing consistent.
Good motion guides attention; bad motion distracts and can hurt performance.
Animation architecture should separate timing logic from reusable visual components.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
Fragment shaders run massively parallel programs on the GPU for every single pixel on screen.
Inputs in GLSL are called uniforms, and Flutter sets them sequentially using shader.setFloat(index, value).
flutter/runtime_effect.glsl provides necessary Flutter coordinate transformations like FlutterFragCoord().
Shaders are compiled at build time by Flutter and must be registered under flutter: shaders: in pubspec.yaml.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue