skip to Main Content

I am creating a full screen loader where i want to set default circularprogressindicator if user dont pass any widget to this function..

but its showing error

whats my mistake

static void showFullScreenDialog({Widget widget=Text('hello')//showing an error} ){
    showDialog(
        context: Get.overlayContext!,
        barrierDismissible: false,
        builder: (_) => PopScope(
            canPop: false,
            child: Container(
          height: double.infinity,
          width: double.infinity,
          color: Colors.transparent,
          child: Center(child: widget,),
        )),
        );
  }

2

Answers


  1. You haven’t shared the error message, but I can guess the source of the problem. When creating a widget as a default value in function parameters, this widget needs to be created at build time. However, Text('hello') is created at runtime.

    static void showFullScreenDialog({
      Widget Function()? widgetBuilder,
    }) {
      showDialog(
        context: Get.overlayContext!,
        barrierDismissible: false,
        builder: (_) => PopScope(
          canPop: false,
          child: Container(
            height: double.infinity,
            width: double.infinity,
            color: Colors.transparent,
            child: Center(
              // Use widgetBuilder if provided, otherwise show default CircularProgressIndicator
              child: widgetBuilder != null
                  ? widgetBuilder()
                  : const CircularProgressIndicator(),
            ),
          ),
        ),
      );
    }
    
    Login or Signup to reply.
  2. call this function as below

    className.showFullScreenDialog()// willshow circularProgressIndicator
    
    or
    
    className.showFullScreenDialog(loader:Text('hello))// will show Text('hello')
    
    
    static void showFullScreenDialog({Widget? loader}) {
        showDialog(
          context: Get.overlayContext!,
          barrierDismissible: false,
          builder: (_) => PopScope(
            canPop: false,
            child: Container(
              height: double.infinity,
              width: double.infinity,
              color: Colors.transparent,
              child: Center(
                child: loader != null ? loader : CircularProgressIndicator(),//add this line
              ),
            ),
          ),
        );
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search