Controller class abstract

A Clean Architecture Controller. Should be aggregated within a ViewState or a View. However, it is preferable to be contained inside the View for readability and maintainability.

The Controller handles the events triggered by the View. For example, it handles the click events of buttons, lifecycle, data-sourcing, etc...

The Controller is also route-aware. However, in order to use it, it has to be initialized separately.

Usage of a Controller:

    // ***************** Controller *****************
    class CounterController extends Controller {
      int counter;
      final MyPresenter presenter;
      CounterController() : counter = 0, presenter = MyPresenter(), super();

      void increment() {
        counter++;
      }

      /// Shows a snackbar
      void showSnackBar() {
        ScaffoldState scaffoldState = getState(); // get the state, in this case, the scaffold
        scaffoldState.showSnackBar(SnackBar(content: Text('Hi')));
      }

      @override
      void initListeners() {
        // Initialize presenter listeners here
        // e.g. presenter.loginOnComplete = () => print('Login Successful);
        // see [initListeners]
      }
    }

    // ***************** View *****************
    class CounterPage extends View {
      @override
      // you can inject dependencies for the controller and the state in here
      State<StatefulWidget> createState() => CounterState(CounterController());
    }

    // ***************** ViewState *****************
    class CounterState extends ViewState<CounterPage, CounterController> {
      CounterState(CounterController controller) : super(controller);

      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          home: Scaffold(
            key: globalKey, // using the built-in global key of the `View` for the scaffold or any other
                            // widget provides the controller with a way to access them via getContext(), getState(), getStateKey()
            body: Column(
              children: <Widget>[
                Center(
                  // show the number of times the button has been clicked
                  child: Text(controller.counter.toString()),
                ),
                MaterialButton(onPressed: () => controller.increment()),
              ],
            ),
          ),
        );
      }
    }

Mixed in types

Constructors

Controller()

Properties

hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
logger ↔ Logger
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
didChangeAccessibilityFeatures() → void
Called when the system changes the set of currently active accessibility features.
inherited
didChangeAppLifecycleState(AppLifecycleState state) → void
Called when the system puts the app in the background or returns the app to the foreground.
override
didChangeLocales(List<Locale>? locales) → void
Called when the system tells the app that the user's locale has changed. For example, if the user changes the system language settings.
inherited
didChangeMetrics() → void
Called when the application's dimensions change. For example, when a phone is rotated.
inherited
didChangePlatformBrightness() → void
Called when the platform brightness changes.
inherited
didChangeTextScaleFactor() → void
Called when the platform's text scale factor changes.
inherited
didHaveMemoryPressure() → void
Called when the system is running low on memory.
inherited
didPop() → void
Called when the current route has been popped off.
inherited
didPopNext() → void
Called when the top route has been popped off, and the current route shows up.
inherited
didPopRoute() Future<bool>
Called when the system tells the app to pop the current route, such as after a system back button press or back gesture.
inherited
didPush() → void
Called when the current route has been pushed.
inherited
didPushNext() → void
Called when a new route has been pushed, and the current route is no longer visible.
inherited
didPushRoute(String route) Future<bool>
Called when the host tells the application to push a new route onto the navigator.
inherited
didPushRouteInformation(RouteInformation routeInformation) Future<bool>
Called when the host tells the application to push a new RouteInformation and a restoration state onto the router.
inherited
didRequestAppExit() Future<AppExitResponse>
Called when a request is received from the system to exit the application.
inherited
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
override
getContext() BuildContext
Retrieves the BuildContext associated with the View. Will throw an error if initController() was not called prior.
getState() State<StatefulWidget>
Retrieves the State<StatefulWidget> associated with the View
getStateKey() GlobalKey<State<StatefulWidget>>
Retrieves the GlobalKey<State<StatefulWidget>> associated with the View
initController(GlobalKey<State<StatefulWidget>> key) → void
Initializes optional Controller variables that can be used for _refreshing and error displaying. This method is called automatically by the mounted View. Do not call.
initListeners() → void
Initialize the listeners inside the the Controller's Presenter. This method is called automatically inside the Controller constructor and must be overridden. For example:
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
onDeactivated() → void
Called before the view is deactivated. When the view is in this context, it means that the view is about to be extracted from the widget tree, but it may be added again. Quoting the view deactivate docs from https://api.flutter.dev/flutter/widgets/State/deactivate.html:
onDetached() → void
Called before the application is detached. When the application is in this state, the engine is still running but not attached to any view.
onDidChangeDependencies() → void
Called before View.didChangeDependencies is called
onDisposed() → void
Unmounts the Controller from the View. Called by the View automatically. Any cleaning, disposing should go in here.
onInActive() → void
Called when the application is in an inactive state and is not receiving user input. On iOS, this state corresponds to an app or the Flutter host view running in the foreground inactive state. Apps transition to this state when in a phone call, responding to a TouchID request, when entering the app switcher or the control center, or when the UIViewController hosting the Flutter app is transitioning. On Android, this corresponds to an app or the Flutter host view running in the foreground inactive state. Apps transition to this state when another activity is focused, such as a split-screen app, a phone call, a picture-in-picture app, a system dialog, or another window.
onInitState() → void
Called before View.initState is called
onPaused() → void
Called when the application is not currently visible to the user, not responding to user input, and running in the background. When the application is in this state, the engine will not call the Window.onBeginFrame and Window.onDrawFrame callbacks. Android apps in this state should assume that they may enter the detached state at any time.
onReassembled() → void
Called before the view is reassembled. When this method is called on view life cycle on reassemble, and it guarantees that build lifecycle will be called. Quoting the docs:
onResumed() → void
Called when the application is visible and is responding to the user i.e. in the foreground and running.
refreshUI() → void
_refreshes the ControlledWidgets and the StatefulWidgets that depends on FlutterCleanArchitecture.getController of the View associated with the Controller if it is still mounted.
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited