PageStateActions class

Actions for a page, addressed by the state name its NyPage listens on.

Build one from the page's path and keep it on the page class, so every call site reads the page it reaches:

class HomePage extends NyStatefulWidget<HomeController> {
  static RouteView path = ("/home", (_) => HomePage());
  static final actions = path.actions;

  HomePage({super.key}) : super(child: () => _HomePageState());
}

HomePage.actions.showToast("hello");
HomePage.actions.call("shake_the_logo", {"times": 3});

Every built-in method here is handled by NyBaseState.stateUpdated, so the same object drives a NyState widget when built from its state name: PageStateActions(Cart.state). A named action sent with call runs the handler registered under that name in the target's stateActions.

Typed actions

Subclass this and declare methods without a body. Each one is sent to the target as a typed action - the method's name as a Symbol, with the arguments it was called with - and the target registers its handler under that symbol:

class HomePageActions extends PageStateActions {
  HomePageActions(super.state);

  void shakeTheLogo({int times = 1}); // no body: sent by noSuchMethod
  void setStars(int stars);
}

class HomePage extends NyStatefulWidget<HomeController> {
  static RouteView path = ("/home", (_) => HomePage());
  static final actions = HomePageActions(path);
  ...
}

class _HomePageState extends NyPage<HomePage> {
  @override
  Map<Object, Function> get stateActions => {
    #shakeTheLogo: shakeTheLogo,
    #setStars: setStars,
  };

  void shakeTheLogo({int times = 1}) { ... }
  void setStars(int stars) { ... }
}

HomePage.actions.shakeTheLogo(times: 3);

Register the handlers with symbol literals (#shakeTheLogo), never with Symbol("shakeTheLogo"): under flutter build --obfuscate a literal is renamed together with the method it names, a symbol built from a string is not. A bodiless action returns void; its handler runs on the target once the event bus delivers it.

Inheritance

Constructors

PageStateActions(dynamic state, {String? id})
state is a page's path (a RouteView) or a state name. Pass id to reach one instance of a NyStateManaged widget, as stateAction does.

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
state ↔ String
getter/setter pairinherited

Methods

action(String action, {dynamic data}) → void
Call an action on a state. The action is a string that represents the action you want to perform on the state. The state is the state you want to perform the action on. The data is the data you want to pass to the action.
inherited
call(String name, [dynamic data]) → void
Call the name action on the state, with optional data.
inherited
changeLanguage(String language, {bool restartState = true}) → void
Update the language in the application
confirmAction(dynamic action(), {required String title, String dismissText = "nylo.confirm_action.cancel"}) → void
Ask the user to confirm action before it runs.
lockRelease(String name, {required Function perform, bool shouldSetState = true}) → void
Run perform and block it from running again until it has finished.
noSuchMethod(Invocation invocation) → dynamic
Sends a method a subclass declared without a body as a typed action.
override
pop({dynamic result, bool rootNavigator = false}) → void
Pop the page.
refreshPage({dynamic setState()?}) → void
Refresh the page, re-running its init. Pass setState to change the page's fields and rebuild it instead.
setState(dynamic setState()) → void
Run setState on the page, then rebuild it.
showToast(String description, {String? title, String id = 'success', Duration? duration, Map<String, dynamic>? data}) → void
Show a toast with description.
showToastCustom({String? title, String? description, String? id, Map<String, dynamic>? data}) → void
Display a custom Toast message. Use id to specify a custom toast style registered via Nylo.addToastNotifications.
showToastDanger({String? title, required String description}) → void
Displays a Toast message containing "Error" for the title, you only need to provide a description.
showToastInfo({String? title, required String description}) → void
Displays a Toast message containing "Info" for the title, you only need to provide a description.
showToastOops({String? title, required String description}) → void
Displays a Toast message containing "Oops" for the title, you only need to provide a description.
showToastSorry({String? title, required String description}) → void
Displays a Toast message containing "Sorry" for the title, you only need to provide a description.
showToastSuccess({String? title, required String description}) → void
Displays a Toast message containing "Success" for the title, you only need to provide a description.
showToastWarning({String? title, required String description}) → void
Displays a Toast message containing "Warning" for the title, you only need to provide a description.
toString() → String
A string representation of this object.
inherited
validate({required Map<String, dynamic> rules, Map<String, dynamic>? data, Map<String, dynamic>? messages, bool showAlert = true, Duration? alertDuration, String alertStyle = 'warning', dynamic onSuccess()?, dynamic onFailure(Exception exception)?, String? lockRelease}) → void
Validate data against rules on the page.

Operators

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