Need to pass variables as properties to reusable widget

flutter pass data to statefulwidget
flutter pass data between widgets
flutter pass data between routes
flutter pass data to parent widget
flutter reusable widgets
flutter pass function to widget
flutter pass data to child widget
flutter widgets

This is my code for a card widget. This will go in as a child of a ListView.builder widget

class CardHome extends StatefulWidget {
  String cardHead;
  String cardDesc;
  String cardTime;
  String cardDate;

  CardHome(
      {this.cardHead, this.cardDesc, this.cardTime, this.cardDate});

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

class _CardHomeState extends State<CardHome> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(globals.blockSizeHorizontal * 6,
          globals.blockSizeVertical * 3, globals.blockSizeHorizontal * 6, 0),
      width: globals.blockSizeHorizontal * 87,
      child: Card(
        shape: RoundedRectangleBorder(
            borderRadius:
                BorderRadius.circular(SizeConfig.blockSizeHorizontal * 4.5)),
        color: Color(globals.secColor),
        elevation: 0,
        child: Container(
          padding: EdgeInsets.symmetric(
              vertical: globals.blockSizeVertical * 2.7,
              horizontal: globals.blockSizeHorizontal * 6.5),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Container(
                    width: globals.blockSizeHorizontal * 56,
                    child: Text(
                      'Buy Plane Tickets',
                      overflow: TextOverflow.ellipsis,
                      maxLines: 2,
                      style: TextStyle(
                          height: globals.blockSizeVertical * 0.14,
                          fontFamily: 'Gilroy',
                          fontWeight: FontWeight.bold,
                          fontSize: globals.blockSizeHorizontal * 3.5,
                          letterSpacing: globals.blockSizeHorizontal * 0.1,
                          color: Color(0xff171b20)),
                    ),
                  ),
                  Text(
                    '8:20',
                    style: TextStyle(
                        fontFamily: 'Arboria',
                        fontWeight: FontWeight.w400,
                        fontSize: globals.blockSizeHorizontal * 3.8,
                        color: globals.inactHeadTextColor),
                  ),
                ],
              ),
              SizedBox(
                height: globals.blockSizeVertical * 2,
              ),
              Container(
                width: globals.blockSizeHorizontal * 70,
                child: Text(
                  'Buy Tickets for departure on 14th May, 2020 at 2:15pm',
                  overflow: TextOverflow.ellipsis,
                  maxLines: 3,
                  style: TextStyle(
                      fontFamily: 'Gilroy',
                      fontWeight: FontWeight.w600,
                      fontSize: globals.blockSizeHorizontal * 3.5,
                      letterSpacing: globals.blockSizeHorizontal * 0.02,
                      color: Color(0xff171b20)),
                ),
              ),
              SizedBox(height: globals.blockSizeVertical * 2),
              Text(
                '23 January',
                style: TextStyle(
                    fontFamily: 'Arboria',
                    fontWeight: FontWeight.w400,
                    fontSize: globals.blockSizeHorizontal * 3.3,
                    color: globals.inactHeadTextColor),
              )
            ],
          ),
        ),
      ),
    );
  }
}

I need to pass this widget to a ListView.builder so cardHead, cardDesc, cardTime, cardDate variables need to go in place of the Strings which I had manually typed (eg. Buy Plane Tickets, 23 January, etc) while passing the widget. For example the cardHead works as a property which I can use in the listview builder like this:

ListView.builder(
  itemCount: headers.length,
  itemBuilder: (context, index) {
    return CardHome(
      cardHead: Text('${headers.[index]}'),
      cardDesc: Text('${descriptions.[index]}'),
    );
  },
);

Is there any way I can achieve this ?

I don't think you need an StatefulWidget here.

class CardHome extends StatelessWidget {
  final String cardHead;
  final String cardDesc;
  final String cardTime;
  final String cardDate;

  const CardHome({
    Key key,
    this.cardHead,
    this.cardDesc,
    this.cardTime,
    this.cardDate,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(
        globals.blockSizeHorizontal * 6,
        globals.blockSizeVertical * 3,
        globals.blockSizeHorizontal * 6,
        0,
      ),
      width: globals.blockSizeHorizontal * 87,
      child: Card(
        shape: RoundedRectangleBorder(
          borderRadius:
              BorderRadius.circular(SizeConfig.blockSizeHorizontal * 4.5),
        ),
        color: Color(globals.secColor),
        elevation: 0,
        child: Container(
          padding: EdgeInsets.symmetric(
            vertical: globals.blockSizeVertical * 2.7,
            horizontal: globals.blockSizeHorizontal * 6.5,
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Container(
                    width: globals.blockSizeHorizontal * 56,
                    child: Text(
                      cardHead,
                      overflow: TextOverflow.ellipsis,
                      maxLines: 2,
                      style: TextStyle(
                        height: globals.blockSizeVertical * 0.14,
                        fontFamily: 'Gilroy',
                        fontWeight: FontWeight.bold,
                        fontSize: globals.blockSizeHorizontal * 3.5,
                        letterSpacing: globals.blockSizeHorizontal * 0.1,
                        color: Color(0xff171b20),
                      ),
                    ),
                  ),
                  Text(
                    cardTime,
                    style: TextStyle(
                      fontFamily: 'Arboria',
                      fontWeight: FontWeight.w400,
                      fontSize: globals.blockSizeHorizontal * 3.8,
                      color: globals.inactHeadTextColor,
                    ),
                  ),
                ],
              ),
              SizedBox(
                height: globals.blockSizeVertical * 2,
              ),
              Container(
                width: globals.blockSizeHorizontal * 70,
                child: Text(
                  cardDesc,
                  overflow: TextOverflow.ellipsis,
                  maxLines: 3,
                  style: TextStyle(
                    fontFamily: 'Gilroy',
                    fontWeight: FontWeight.w600,
                    fontSize: globals.blockSizeHorizontal * 3.5,
                    letterSpacing: globals.blockSizeHorizontal * 0.02,
                    color: Color(0xff171b20),
                  ),
                ),
              ),
              SizedBox(height: globals.blockSizeVertical * 2),
              Text(
                cardDate,
                style: TextStyle(
                  fontFamily: 'Arboria',
                  fontWeight: FontWeight.w400,
                  fontSize: globals.blockSizeHorizontal * 3.3,
                  color: globals.inactHeadTextColor,
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Usage:

CardHome(
  cardHead: "Buy Plane Tickets",
  cardTime: "8:20",
  cardDesc: "Buy Tickets for departure on 14th May, 2020 at 2:15pm",
  cardDate: "23 January",
)

or

CardHome(
  cardHead: '${headers[index]}',
  cardTime: '${times[index}',
  cardDesc: '${descriptions[index]}',
  cardDate: '${dates[index]}',
)

Simple ways to pass to and share data with widgets/pages, As first thing we define a class with some properties that are going to be The simplest way to send data from a widget to another is by its constructor. Let's say, for example, we need to pass an instance of the above Data� You just need to pass the data and you can reuse these functions everywhere. So, it’s the end of the duplicating the code. We can also make other widgets like Dialogs, switch, etc reusable.

Please modify your CardHome widget as above.

class CardHome extends StatefulWidget {
  Widget cardHead;
  Widget cardDesc;
  Widget cardTime;
  Widget cardDate;

  CardHome(
      {this.cardHead, this.cardDesc, this.cardTime, this.cardDate});

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

class _CardHomeState extends State<CardHome> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(globals.blockSizeHorizontal * 6,
          globals.blockSizeVertical * 3, globals.blockSizeHorizontal * 6, 0),
      width: globals.blockSizeHorizontal * 87,
      child: Card(
        shape: RoundedRectangleBorder(
            borderRadius:
            BorderRadius.circular(SizeConfig.blockSizeHorizontal * 4.5)),
        color: Color(globals.secColor),
        elevation: 0,
        child: Container(
          padding: EdgeInsets.symmetric(
              vertical: globals.blockSizeVertical * 2.7,
              horizontal: globals.blockSizeHorizontal * 6.5),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Container(
                    width: globals.blockSizeHorizontal * 56,
                    child: widget.cardHead
                  ),
                  widget.cardTime
                ],
              ),
              SizedBox(
                height: globals.blockSizeVertical * 2,
              ),
              Container(
                width: globals.blockSizeHorizontal * 70,
                child: widget.cardDesc
              ),
              SizedBox(height: globals.blockSizeVertical * 2),
              widget.cardDate
            ],
          ),
        ),
      ),
    );
  }
}

As you can see here, We have exchanged

  String cardHead;
  String cardDesc;
  String cardTime;
  String cardDate;

With

  Widget cardHead;
  Widget cardDesc;
  Widget cardTime;
  Widget cardDate;

So now you can use it as

CardHome(
      cardHead: Text('${headers.[index]}'),
      cardDesc: Text('${descriptions.[index]}'),
    );

Flutter: The power of small and reusable widgets, In part 1 we have seen how load the airports data and set up all the non-UI code Creating widgets that are small and reusable favours composition, and We use a flightDetails variable to configure the values inside our AirportWidget and This works because Expanded has a flex property with a default� Use composition and custom widgets to write reusable and concise UI code. Use CupertinoSegmentedControl from the Cupertino widget set. As we will see, the key message of this article is that: Creating widgets that are small and reusable favours composition, and leads to code that is more concise and easier to reason about. But first of all

you can replace string of text with widget.cardHead solve your problem

Text(
    widget.cardHead,
  ),

Building Your First Reusable Widget with Flutter, In this guide, I will show you how to build a reusable widget with Flutter and Dart. All of our properties are final because we only want to pass the data once on Now we can pass our custom arguments, see image below. The simplest way to send data from a widget to another is by its constructor. Let’s say, for example, we need to pass an instance of the above Data class from a screen (PageOne) to another

Flutter: The power of small and reusable widgets, In part 1 we have seen how load the airports data and set up all the non-UI Use composition and custom widgets to write reusable and concise UI code. This works because Expanded has a flex property with a default value of 1 SegmentedControl<FlightType>( // parameters ), SizedBox(height: 16.0),� In the lifetime of my widget, do I need to consider a variable that will change and when changed, will force the widget to be rebuilt? If the answer to the question is yes, then you need a Stateful widget, otherwise, you need a Stateless widget. Some examples: a widget to display a list of checkboxes.

ChangeNotifierProvider class - provider library, Depending on wether you want to create or reuse a ChangeNotifier, you will want to use If you want to pass variables to your ChangeNotifier, consider using� In part 1 we have seen how load the airports data and set up all the non-UI code that will power the app. Creating widgets that are small and reusable favours composition, and leads to code that is…

provider, By using provider instead of manually writing InheritedWidget, you get: If you want to pass variables that can change over time to your object, consider using DON'T reuse an existing ChangeNotifier using the default constructor debugFillProperties(properties); // list all the properties of your class here. To develop widgets, you need ServiceNow API experience to: . Run record queries on the server. Create and update records. You need AngularJS experience to: Bind variables to client controllers.