1

I have created a custom StatefulWidget that takes a Widget as an argument.

import 'package:flutter/material.dart';

class CustomWidget extends StatefulWidget {
  
  final Widget myWidget;
  CustomWidget({this.myWidget});
  
  @override
  _CustomWidgetState createState() => _CustomWidgetState();
}

class _CustomWidgetState extends State<CustomWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: widget.myWidget(name: 'ASAD'), //I want to achieve this
    );
  }
}

I'm passing another StatefulWidget named Profile as that argument widget. Now I want to access that passed widget's constructor.

import 'package:flutter/material.dart';

class Profile extends StatefulWidget {

  final String name;

  Profile({this.name});
  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<Profile> {
  @override
  Widget build(BuildContext context) {
    return Center(child: Text('${widget.name}'),);
  }
}

I'm using the CustomWidget which takes a widget as argument here

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }

    class _MyHomePageState extends State<MyHomePage> {
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: CustomWidget(myWidget: Profile(),) //I don't want to pass the argument here
        );
      }
    }

I was able to accomplish something like this using typedef but it started throwing an error.

typedef CustomCallBack = Widget Function({String name});

class CustomWidget extends StatefulWidget {

  final CustomCallBack myWidget;
  CustomWidget({this.myWidget});

  @override
  _CustomWidgetState createState() => _CustomWidgetState();
}

class _CustomWidgetState extends State<CustomWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: widget.widget(name: 'ASAD'), //I'm able to access the constructor here but now I get an error.
    );
  }
}

Error

error: The argument type 'Profile' can't be assigned to the parameter type 'Widget Function({String name})'. (argument_type_not_assignable

Note: Based on the app that I'm working on but cannot share code, I have recreated this scenario and is same as far as the issue is concerned.

ASAD HAMEED
  • 2,296
  • 1
  • 20
  • 36

2 Answers2

0

Try the change this line in the CustomWidget widget,child: widget.widget(name: 'ASAD'), for the widget that you are passing only, without putting the (name: 'ASAD').

So when you pass the Profile Widget, that's when you must specify the value of the name parameter!

body: CustomWidget(myWidget: Profile(name: 'ASAD'),)
Yeikel200
  • 35
  • 1
  • 7
0

please try to use this :

import 'package:flutter/material.dart';

typedef CustomCallBack = Widget Function(String name);

class CustomWidget extends StatefulWidget {
  final CustomCallBack myWidget;

  CustomWidget({this.myWidget});

  @override
  _CustomWidgetState createState() => _CustomWidgetState();
}

class _CustomWidgetState extends State<CustomWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: widget.myWidget(
          'ASAD'), //I'm able to access the constructor here but now I get an error.
    );
  }
}

class Profile extends StatefulWidget {
  final String name;

  Profile({this.name});

  @override
  _ProfileState createState() => _ProfileState();
}

class _ProfileState extends State<Profile> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text('${widget.name}'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: CustomWidget(
      myWidget: (name) => Profile(
        name: name,
      ),
    ) //I don't want to pass the argument here
        );
  }
}

Taleb
  • 1,944
  • 2
  • 11
  • 36