Physics Simulations, Hero Transitions & CustomPainter
Explore advanced motion graphics in Flutter: realistic spring physics simulations, shared element Hero transitions across routes, and low-level 2D graphics rendering with CustomPainter.
1. Physics-Based Motion Simulations
Unlike time-based animations that run on fixed durations, physics simulations calculate motion dynamically based on physical forces (mass, stiffness, damping, velocity):
import 'package:flutter/physics.dart';
void launchSpringAnimation(AnimationController controller) {
// Define spring mechanics: mass, stiffness (k), damping ratio (zeta)
const spring = SpringDescription(
mass: 1.0,
stiffness: 100.0,
damping: 10.0,
);
// Create spring simulation moving from current value (0.0) to target (1.0) with initial velocity
final simulation = SpringSimulation(spring, 0.0, 1.0, 0.5);
// Run simulation directly on the controller
controller.animateWith(simulation);
}2. Shared Element Transitions: Hero
Hero animations create the visual illusion of a widget "flying" smoothly from one screen to another during route navigation:
// Screen A: Origin Thumbnail
class ProductThumbnail extends StatelessWidget {
final Product product;
const ProductThumbnail({required this.product, super.key});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => Navigator.push(context, MaterialPageRoute(builder: (_) => ProductDetail(product: product))),
child: Hero(
tag: 'product_image_${product.id}', // ⚡ Identical unique tag!
child: Image.network(product.imageUrl, width: 100, height: 100, fit: BoxFit.cover),
),
);
}
}
// Screen B: Destination Full Screen View
class ProductDetail extends StatelessWidget {
final Product product;
const ProductDetail({required this.product, super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(product.title)),
body: Hero(
tag: 'product_image_${product.id}', // ⚡ Matches origin tag!
child: Image.network(product.imageUrl, width: double.infinity, height: 350, fit: BoxFit.cover),
),
);
}
}3. Low-Level 2D Graphics: CustomPainter
When existing widgets cannot achieve a desired custom visual effect (e.g. wave shapes, circular audio visualizers, charts), CustomPainter grants direct access to the Skia/Impeller Canvas.
class DonutChartPainter extends CustomPainter {
final double progressPercent; // 0.0 to 1.0
final Color trackColor;
final Color progressColor;
const DonutChartPainter({
required this.progressPercent,
this.trackColor = const Color(0xFFE2E8F0),
this.progressColor = const Color(0xFF3B82F6),
});
@override
void paint(Canvas canvas, Size size) {
final center = Offset(size.width / 2, size.height / 2);
final radius = (size.width / 2) - 10.0;
final strokeWidth = 14.0;
// 1. Draw Background Ring
final trackPaint = Paint()
..color = trackColor
..style = PaintingStyle.stroke
..strokeWidth = strokeWidth;
canvas.drawCircle(center, radius, trackPaint);
// 2. Draw Active Progress Arc
final progressPaint = Paint()
..color = progressColor
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round // Smooth rounded ends
..strokeWidth = strokeWidth;
const startAngle = -3.14159 / 2; // Start from top (12 o'clock)
final sweepAngle = 2 * 3.14159 * progressPercent;
canvas.drawArc(
Rect.fromCircle(center: center, radius: radius),
startAngle,
sweepAngle,
false,
progressPaint,
);
}
// ⚡ CRITICAL: Avoid unnecessary repaints when inputs have not changed
@override
bool shouldRepaint(covariant DonutChartPainter oldDelegate) {
return oldDelegate.progressPercent != progressPercent ||
oldDelegate.progressColor != progressColor ||
oldDelegate.trackColor != trackColor;
}
}
// Widget integration:
class ProgressGauge extends StatelessWidget {
final double progress;
const ProgressGauge({required this.progress, super.key});
@override
Widget build(BuildContext context) {
return CustomPaint(
size: const Size(180, 180),
painter: DonutChartPainter(progressPercent: progress),
child: Center(
child: Text("${(progress * 100).toInt()}%", style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold)),
),
);
}
}4. CustomPainter Performance Best Practices
- Never Allocate Objects Inside
paint():- The
paint()method can execute 60–120 times per second during interactions. Allocating newPaint()orPath()objects insidepaint()triggers high garbage collection pressure. - Cache reusable paint objects in the painter instance.
- The
- Implement
shouldRepaint()Diligently:- Always compare fields between
oldDelegateandthis. Returningtrueunconditionally forces unnecessary CPU rasterization passes.
- Always compare fields between
- Wrap in
RepaintBoundary:- Isolate custom canvas drawing into a dedicated GPU layer.
❓ Knowledge Check
What is the significance of the tag property on a Hero widget?
Why should you never allocate new Paint or Path objects directly inside the paint() method of a CustomPainter?
Implicit vs. Explicit Animation Architecture
Master Flutter animations from automatic transitions to manual frame choreography: compare Implicit widgets, TweenAnimationBuilder, AnimationController, and AnimatedBuilder performance caching.
Key-Value & Secure Storage Architecture
Master local persistence in Flutter: compare in-memory cached SharedPreferences against hardware-encrypted Keychain/KeyStore storage, and navigate device file systems with path_provider.