Created with <3 with dartpad.dev.
Last active
November 17, 2022 14:22
-
-
Save Diaga/452cd526f10dc248348f9f3b6f108c54 to your computer and use it in GitHub Desktop.
use consumer widget for con con getter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() async { | |
final tableWidget = TableWidget(); | |
print(tableWidget.build()); | |
} | |
// Con Con DepController | |
class DepController<T> { | |
T instance; | |
DepController(T Function() builder) : instance = builder(); | |
T call() { | |
return instance; | |
} | |
} | |
// Con con Controller class | |
class Controller {} | |
// Custom UI controller | |
class TableController extends Controller { | |
void doStuff() {} | |
} | |
// Con con Consumer Widget class | |
class ConsumerWidget<T extends Controller> extends Widget { | |
final DepController? _depController; | |
ConsumerWidget({Key? key, DepController? depController}) : _depController = depController; | |
T get controller { | |
return _depController!.call(); | |
} | |
} | |
class TableWidget extends ConsumerWidget<TableController> { | |
TableWidget() | |
: super(depController: DepController(() { | |
return TableController(); | |
})); | |
Widget build() { | |
// Use controller | |
controller.doStuff(); | |
return ConsumerWidget(); | |
} | |
} | |
// Ignore | |
class Key {} | |
class Widget {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment