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
-
- Object
- StateActions
- PageStateActions
Constructors
- PageStateActions(dynamic state, {String? id})
-
stateis a page'spath(a RouteView) or a state name. Passidto reach one instance of aNyStateManagedwidget, as stateAction does.
Properties
Methods
-
action(
String action, {dynamic data}) → void -
Call an action on a state.
The
actionis 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. Thedatais the data you want to pass to the action.inherited -
call(
String name, [dynamic data]) → void -
Call the
nameaction on the state, with optionaldata.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
actionbefore it runs. -
lockRelease(
String name, {required Function perform, bool shouldSetState = true}) → void -
Run
performand 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, }) → void - Pop the page.
-
refreshPage(
{dynamic setState()?}) → void -
Refresh the page, re-running its
init. PasssetStateto change the page's fields and rebuild it instead. -
setState(
dynamic setState()) → void -
Run
setStateon 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
idto 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
dataagainstruleson the page.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited