1- import 'package:flutter/widgets.dart' ;
2- import 'package:flutter_command/flutter_command.dart' ;
1+ part of flutter_command;
32
43class CommandBuilder <TParam , TResult > extends StatelessWidget {
54 final Command <TParam , TResult > command;
5+
6+ /// This builder will be called when the
7+ /// command is executed successfully, independent of the return value.
8+ final Widget Function (BuildContext context, TParam ? param)? onSuccess;
9+
10+ /// If your command has a return value, you can use this builder to build a widget
11+ /// when the command is executed successfully.
612 final Widget Function (BuildContext context, TResult data, TParam ? param)?
713 onData;
14+
15+ /// If the command has no return value or returns null, this builder will be called when the
16+ /// command is executed successfully.
817 final Widget Function (BuildContext context, TParam ? param)? onNullData;
918 final Widget Function (
1019 BuildContext context,
@@ -20,6 +29,7 @@ class CommandBuilder<TParam, TResult> extends StatelessWidget {
2029
2130 const CommandBuilder ({
2231 required this .command,
32+ this .onSuccess,
2333 this .onData,
2434 this .onNullData,
2535 this .whileExecuting,
@@ -29,10 +39,13 @@ class CommandBuilder<TParam, TResult> extends StatelessWidget {
2939
3040 @override
3141 Widget build (BuildContext context) {
42+ if (command._noReturnValue) {}
3243 return ValueListenableBuilder <CommandResult <TParam ?, TResult >>(
3344 valueListenable: command.results,
3445 builder: (context, result, _) {
3546 return result.toWidget (
47+ onSuccess: (paramData) =>
48+ onSuccess? .call (context, paramData) ?? const SizedBox (),
3649 onData: (data, paramData) =>
3750 onData? .call (context, data, paramData) ?? const SizedBox (),
3851 onNullData: (paramData) =>
@@ -59,19 +72,25 @@ class CommandBuilder<TParam, TResult> extends StatelessWidget {
5972extension ToWidgeCommandResult <TParam , TResult >
6073 on CommandResult <TParam , TResult > {
6174 Widget toWidget ({
62- required Widget Function (TResult result, TParam ? param) onData,
75+ Widget Function (TResult result, TParam ? param)? onData,
76+ Widget Function (TParam ? param)? onSuccess,
6377 Widget Function (TParam ? param)? onNullData,
6478 Widget Function (TResult ? lastResult, TParam ? param)? whileExecuting,
6579 Widget Function (Object error, TResult ? lastResult, TParam ? param)? onError,
6680 }) {
81+ assert (onData != null || onSuccess != null ,
82+ 'You have to provide at least a builder for onData or onSuccess' );
6783 if (error != null ) {
6884 return onError? .call (error! , data, paramData) ?? const SizedBox ();
6985 }
7086 if (isExecuting) {
7187 return whileExecuting? .call (data, paramData) ?? const SizedBox ();
7288 }
89+ if (onSuccess != null ) {
90+ return onSuccess.call (paramData);
91+ }
7392 if (data != null ) {
74- return onData (data as TResult , paramData);
93+ return onData? . call (data as TResult , paramData) ?? const SizedBox ( );
7594 } else {
7695 return onNullData? .call (paramData) ?? const SizedBox ();
7796 }
0 commit comments