buildWithChild method

  1. @override
Widget buildWithChild(
  1. BuildContext context,
  2. Widget? child
)
override

A build method that receives an extra child parameter.

This method may be called with a child different from the parameter passed to the constructor of SingleChildStatelessWidget. It may also be called again with a different child, without this widget being recreated.

Implementation

@override
Widget buildWithChild(BuildContext context, Widget? child) {
  return ChangeNotifierProvider<VM>(
    create: (_) => create(context),
    child: child,
    builder: (context, child) {
      return ViewModelBinding<VM, VM>(
        selector: (context, vm) => vm,
        child: child,
        builder: (context, value, isBinding, child) {
          final buildWidget = () {
            if (isBinding) bindViewModel?.call(context, value);
            return builder?.call(context, value, child) ??
                child ??
                const SizedBox();
          };
          if (initViewModel != null ||
              initFrame != null ||
              disposeViewModel != null) {
            return LifecycleBuilder<VM>(
              create: (context) => value,
              initState: (_) => initViewModel?.call(context, value),
              initFrame: (_, __) => initFrame?.call(context, value),
              dispose: (_, value) => disposeViewModel?.call(context, value),
              child: child,
              builder: (context, setState, value, child) => buildWidget(),
            );
          }
          return buildWidget();
        },
      );
    },
  );
}