← Back to posts Cover image for Unlocking Advanced UI: Building Custom Spatial and 3D Effects with Flutter Canvas

Unlocking Advanced UI: Building Custom Spatial and 3D Effects with Flutter Canvas

· 5 min read
Weekly Digest

The Flutter news you actually need

No spam, ever. Unsubscribe in one click.

Chris
By Chris

Beyond the Widget Tree: Creating Spatial and 3D Effects with Flutter Canvas

We often reach for Flutter’s rich widget library to build our interfaces. But when you need something truly unique—a UI that reacts to the tilt of a phone, a navigation bar that flows like liquid glass, or interface elements with a sense of depth—you have to go deeper. This is where Flutter’s CustomPainter and the raw power of the Canvas become your most potent tools.

The challenge? Many developers see CustomPainter as a way to draw simple shapes or charts, not as a gateway to immersive, spatial experiences. The leap from a static circle to a dynamic 3D object that responds to sensor data can feel daunting. How do you translate raw sensor streams into visual transformations without grinding your app to a halt?

Let’s break it down. The core idea is to use CustomPainter as your render engine. You’ll process data (like gyroscope inputs) to calculate a “view” of your scene, then use low-level Canvas operations (drawPath, drawVertices, transformations) to paint each frame.

Step 1: Laying the Foundation with a Reactive Canvas

First, create a CustomPainter that can be driven by external data. We’ll use a ValueNotifier to hold our “viewpoint” angles, which will later be controlled by sensors.

import 'dart:math';
import 'package:flutter/material.dart';

class SpatialPainter extends CustomPainter {
  final double rotationX; // Tilt up/down
  final double rotationY; // Pan left/right

  SpatialPainter({required this.rotationX, required this.rotationY});

  @override
  void paint(Canvas canvas, Size size) {
    // Move origin to center of canvas
    canvas.translate(size.width / 2, size.height / 2);

    // Apply spatial rotations. Order matters!
    canvas.rotate(rotationY);
    canvas.transform(
        Float64List.fromList([1, 0, 0, 0, 0, cos(rotationX), sin(rotationX), 0, 0, -sin(rotationX), cos(rotationX), 0, 0, 0, 0, 1]));

    // Draw a simple 3D cube as a starting point
    _drawCube(canvas, size);
  }

  void _drawCube(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = Colors.blue.withOpacity(0.7)
      ..style = PaintingStyle.fill;

    // Define cube vertices (simplified front-facing)
    var halfSide = size.width / 8;
    var frontFace = Path()
      ..addRect(Rect.fromLTRB(-halfSide, -halfSide, halfSide, halfSide));
    
    // Apply a simple perspective offset based on rotation
    var depthOffset = rotationY * 20;
    canvas.save();
    canvas.translate(depthOffset, 0);
    canvas.drawPath(frontFace, paint);
    canvas.restore();
  }

  @override
  bool shouldRepaint(SpatialPainter oldDelegate) {
    // Only repaint when the rotation values actually change
    return rotationX != oldDelegate.rotationX || rotationY != oldDelegate.rotationY;
  }
}

Step 2: Feeding the Canvas with Sensor Data

To make the UI spatial, we connect it to the device’s gyroscope. The critical part here is throttling. Sensor events fire far faster than your screen can refresh (often 100Hz+ vs 60/90Hz). Repainting on every event wastes CPU/GPU cycles.

We solve this by using a stream that’s synchronized to the frame rate.

import 'package:sensors_plus/sensors_plus.dart';
import 'package:flutter/scheduler.dart';

class SpatialView extends StatefulWidget {
  @override
  _SpatialViewState createState() => _SpatialViewState();
}

class _SpatialViewState extends State<SpatialView> with SingleTickerProviderStateMixin {
  late final Ticker _ticker;
  double _tiltX = 0.0;
  double _panY = 0.0;

  // We accumulate raw sensor data here between frames
  double _rawGyroY = 0.0;
  double _rawGyroX = 0.0;

  @override
  void initState() {
    super.initState();

    // Listen to the gyroscope at full speed, but only store the values.
    gyroscopeEvents.listen((GyroscopeEvent event) {
      // Integrate the rotation rate (event.y, event.x) over time.
      // In a real app, you'd apply a proper time delta. Here we use a small damping factor.
      _rawGyroY += event.y * 0.01;
      _rawGyroX += event.x * 0.01;
    });

    // This ties our repaint loop to the Flutter's frame pipeline.
    _ticker = createTicker((elapsed) {
      // Update the painter's values only once per frame.
      setState(() {
        _panY = _rawGyroY;
        _tiltX = _rawGyroX;
        // Optional: Apply smoothing or clamping here
        _tiltX = _tiltX.clamp(-0.5, 0.5);
      });
    });
    _ticker.start();
  }

  @override
  void dispose() {
    _ticker.stop(); // CRITICAL: Always stop the ticker!
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: SpatialPainter(rotationX: _tiltX, rotationY: _panY),
      size: MediaQuery.of(context).size,
    );
  }
}

Common Pitfalls and Optimization Tips

  1. Never Rebuild the Widget Tree on Every Sensor Event: The biggest performance killer is calling setState inside the raw gyroscopeEvents.listen. As shown, buffer the data and update it once per frame using a Ticker.
  2. Dispose Everything: The sensor stream and the ticker must be cleaned up in dispose(). Leaking these is a common source of memory issues and battery drain.
  3. Keep Paint Objects Reusable: Instantiate Paint, Path, and other drawing objects in your CustomPainter’s constructor or in a build method, not inside the paint() method itself, which is called every frame.
  4. Start Simple: Before attempting a complex “liquid glass” effect, master drawing and transforming basic 3D primitives (cubes, spheres made from points). The principles of perspective and lighting are the same.

By separating the high-frequency sensor data collection from the frame-locked rendering, you create a buttery-smooth spatial UI. The Canvas gives you the precision to turn mathematical concepts—3D vertex projections, Bézier curves for liquid effects, light calculations—into stunning, interactive visuals that live outside the standard widget library. The only limit is your understanding of the paint() method. Start experimenting with a single rotating square today, and you’ll be building immersive interfaces before you know it.

This blog is produced with the assistance of AI by a human editor. Learn more

Related Posts

Cover image for Flutter App Size Optimization: Strategies to Shrink Your APK/IPA Below 10MB

Flutter App Size Optimization: Strategies to Shrink Your APK/IPA Below 10MB

Flutter apps are often criticized for their larger-than-native binary sizes. This post will dive into practical, actionable strategies to significantly reduce your Flutter app's footprint, including build configurations, asset optimization, and dependency management, demonstrating how to achieve app sizes under 10MB.

Cover image for Mastering In-App Subscriptions in Flutter: A Guide to Avoiding Common Pitfalls

Mastering In-App Subscriptions in Flutter: A Guide to Avoiding Common Pitfalls

Implementing in-app subscriptions in Flutter can be surprisingly complex, especially when dealing with cross-platform nuances, receipt validation, and managing different store configurations. This post will delve into the common challenges developers face, compare solutions like RevenueCat with custom implementations, and provide best practices for setting up and maintaining a robust subscription system.

Cover image for Taming Android Emulator RAM: A Developer's Guide to Reducing Memory Usage for Flutter

Taming Android Emulator RAM: A Developer's Guide to Reducing Memory Usage for Flutter

Android emulators are notorious for hogging RAM, often pushing developer machines into swap and slowing down Flutter development. This post will explore the reasons behind high emulator memory consumption and provide practical, actionable strategies and tools (including a custom-built solution) to significantly reduce RAM usage, ensuring a smoother development experience without sacrificing functionality.