← Back to posts Cover image for Mastering Dynamic UIs: Building Generative UI Systems in Flutter

Mastering Dynamic UIs: Building Generative UI Systems in Flutter

· 5 min read
Weekly Digest

The Flutter news you actually need

No spam, ever. Unsubscribe in one click.

Chris
By Chris

From Data to Widgets: Building Generative UIs in Flutter

Have you ever needed to build an app where the layout, components, or even entire workflows are defined not in your Dart code, but on a server? Perhaps you’re creating a platform for user-generated forms, a CMS-driven mobile app, or a feature flag system that can ship new UI without an app store update. This is the realm of generative UI—dynamically constructing your interface from external data, most commonly JSON.

Building a system that translates a JSON payload into a live, interactive Flutter widget tree is a powerful pattern. It moves your app’s presentation logic from a hardcoded state to a flexible, data-driven model. Let’s break down how you can build one.

The Core Challenge: Mapping JSON to Widgets

At its heart, the problem is straightforward: you receive a data structure and need to turn it into a corresponding widget. The immediate temptation might be a massive switch statement or a set of if-else checks against a “type” field. While this works for trivial cases, it becomes unmaintainable quickly.

The real challenge lies in creating a system that is:

  • Extensible: Easy to add new widget types without rewriting core logic.
  • Nested: Capable of handling complex, hierarchical layouts.
  • Interactive: Able to incorporate stateful widgets, gestures, and callbacks.

Building a Registration System

Let’s design a system to render a dynamic registration form. Our JSON might look like this:

{
  "type": "Column",
  "children": [
    {
      "type": "TextInput",
      "id": "email_field",
      "label": "Email Address",
      "hint": "you@example.com"
    },
    {
      "type": "TextInput",
      "id": "password_field",
      "label": "Password",
      "obscureText": true
    },
    {
      "type": "Button",
      "id": "submit_btn",
      "text": "Sign Up",
      "action": "submit_form"
    }
  ]
}

Step 1: Define a Widget Registry

Instead of a hardcoded switch, we’ll use a map that acts as a registry, mapping string identifiers to widget builder functions.

typedef WidgetBuilder = Widget Function(Map<String, dynamic> config);

class WidgetRegistry {
  final Map<String, WidgetBuilder> _builders = {};

  void register(String type, WidgetBuilder builder) {
    _builders[type] = builder;
  }

  WidgetBuilder? getBuilder(String type) {
    return _builders[type];
  }
}

Step 2: Create Builder Functions

Now, we implement the builders for each widget type. Each builder is responsible for interpreting its specific configuration map.

class CoreWidgets {
  static void register(WidgetRegistry registry) {
    registry.register('Column', (config) {
      final childrenConfigs = config['children'] as List? ?? [];
      final children = _buildChildren(childrenConfigs, registry);
      return Column(children: children);
    });

    registry.register('TextInput', (config) {
      return TextField(
        decoration: InputDecoration(
          labelText: config['label'] as String?,
          hintText: config['hint'] as String?,
        ),
        obscureText: config['obscureText'] as bool? ?? false,
        onChanged: (value) {
          // Store value using the 'id' key, e.g., in a state manager
          print("Field ${config['id']}: $value");
        },
      );
    });

    registry.register('Button', (config) {
      return ElevatedButton(
        onPressed: () {
          // Trigger the action defined in the JSON
          print("Action: ${config['action']}");
        },
        child: Text(config['text'] as String? ?? 'Button'),
      );
    });
  }

  static List<Widget> _buildChildren(List<dynamic> childrenConfigs, WidgetRegistry registry) {
    return childrenConfigs.map<Widget>((childConfig) {
      return DynamicWidgetBuilder.build(childConfig, registry);
    }).toList();
  }
}

Step 3: The Recursive Builder

The magic happens in a recursive builder widget that traverses the JSON tree.

class DynamicWidgetBuilder {
  static Widget build(Map<String, dynamic> config, WidgetRegistry registry) {
    final type = config['type'] as String?;
    if (type == null) {
      return const SizedBox(); // Or a placeholder error widget
    }

    final builder = registry.getBuilder(type);
    if (builder == null) {
      return Text('Unknown widget type: $type'); // Error widget
    }

    return builder(config);
  }
}

Step 4: Bringing It All Together

Finally, we initialize the registry and use the builder.

class DynamicFormScreen extends StatelessWidget {
  final Map<String, dynamic> uiConfig;

  const DynamicFormScreen({super.key, required this.uiConfig});

  @override
  Widget build(BuildContext context) {
    // 1. Create and populate the registry
    final registry = WidgetRegistry();
    CoreWidgets.register(registry);
    // Later: CustomWidgets.register(registry);

    // 2. Build the UI from the root config
    return Scaffold(
      appBar: AppBar(title: const Text('Dynamic Form')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: SingleChildScrollView(
          child: DynamicWidgetBuilder.build(uiConfig, registry),
        ),
      ),
    );
  }
}

Common Pitfalls and Pro Tips

  1. State Management: The example uses onChanged and onPressed callbacks for demonstration. In a real app, you’ll need a way to collect the form data. Pair your generative UI system with a state management solution like Provider, Riverpod, or a simple ValueNotifier that maps widget ids to their current values.
  2. Error Handling: Always assume the JSON could be malformed. Use null-aware operators (as String?) and provide sensible defaults. Consider adding a debug-mode flag that visually outlines widgets with missing configurations.
  3. Performance: For deeply nested or large dynamic UIs, consider using ListView.builder patterns within your JSON configuration to lazily render long lists.
  4. Beyond Layout: You can extend this pattern to include styles (padding, colors, typography) defined in your JSON. A common approach is to have a "style" key that references a theme map, or to use a utility-class approach inspired by Tailwind CSS (e.g., "className": "p-4 bg-blue-100").

Building a generative UI system is an excellent exercise that deepens your understanding of Flutter’s compositional model. It forces you to think abstractly about widgets as data and reveals the framework’s flexibility. Start with a simple registry pattern, handle nesting recursively, and gradually add support for state and styling.

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

Related Posts

Cover image for Flutter's Hidden Gem: Building Powerful Linux Desktop Apps with Ease

Flutter's Hidden Gem: Building Powerful Linux Desktop Apps with Ease

Flutter is gaining traction as a robust framework for Linux desktop development, offering a smoother experience compared to traditional toolkits like GTK and Qt. This post will explore why Flutter excels for Linux apps, highlight its advantages, and provide practical insights for developers looking to leverage Flutter for their desktop projects.

Cover image for Simplifying Flutter Desktop Deployment: Signing and Distribution for Windows

Simplifying Flutter Desktop Deployment: Signing and Distribution for Windows

Deploying Flutter desktop apps on Windows can be challenging, especially regarding code signing and preventing Windows from blocking installations. This post will guide developers through the process of signing their Flutter desktop applications, exploring alternatives to the Microsoft Store like creating executable installers, and covering the necessary steps to ensure a smooth user experience.

Cover image for Mastering Flutter Tooling: Streamlining SDK Management and Installation on Windows

Mastering Flutter Tooling: Streamlining SDK Management and Installation on Windows

Developers frequently voice frustrations about Flutter SDK version management and installation processes, particularly on Windows. This post will explore best practices for managing multiple Flutter versions using tools like FVM, discuss integrating Flutter with package managers like Winget for a smoother Windows setup, and offer strategies to ensure a consistent development environment across projects and teams.