Skip to content

Controllers

Controllers are custom classes that extend Controller from the xwidget package. They provide logic and state management for XML fragments. The generator scans your source files and creates a registration function for all discovered controllers.

Creating a Controller

Place controller files in lib/xwidget/controllers/:

// lib/xwidget/controllers/app_controller.dart
import 'package:xwidget/xwidget.dart';

class AppController extends Controller {
  // Your controller logic here
}

Auto-Discovery

By default, the controller builder scans lib/xwidget/controllers/**.dart for any class that extends Controller. No additional configuration is needed unless you want to change the scan path:

controllers:
  sources: [ "lib/xwidget/controllers/**.dart" ]

The ** glob pattern matches files in the directory and all subdirectories.

Generated Output

The generator creates a registerXWidgetControllers() function:

void registerXWidgetControllers() {
  XWidget.registerControllerFactoryForName(
    'AppController',
    () => AppController(),
  );
  XWidget.registerControllerFactoryForName(
    'LoginController',
    () => LoginController(),
  );
}

Controllers can then be referenced in XML fragments by class name:

<Controller for="home" name="AppController">
</Controller>