skip to Main Content
class reusableCard extends StatelessWidget {
  reusableCard({required this.colour, this.cardChild});
  final Color colour;
  final Widget? cardChild;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: colour,
        borderRadius: BorderRadius.circular(20),
      ),
      margin: EdgeInsets.all(20),
    );
  }
}

i am calling a class named reuseabelCard with two parameter among which one is a of Widget datatype.
when calling i am passing a column containing icon and text ,but the the widget is not getting displayed at all.

Expanded(
  child: reusableCard(
    colour: ActiveColourCard,
    cardChild: Column(
      children: [
        Icon(FontAwesomeIcons.mars),
        SizedBox(
          height: 10.0,
        ),
        Text(
          'Male',
          style: TextStyle(
            color: Colors.white,
          ),
        ),
      ],
    ),
  ),
),

I am expecting for the icon and text to be displayed.

2

Answers


  1. you forgot to add cardChild on reusableCard build() method

        return Container(
          decoration: BoxDecoration(
            color: colour,
            borderRadius: BorderRadius.circular(20),
          ),
          margin: EdgeInsets.all(20),
          // add this line
          child: cardChild ?? null,
        );
    

    also, by dart convention, you should name classes with PascalCase (ReusableCard instead of reusableCard)

    Login or Signup to reply.
  2. You are not calling the cardChild in reusableCard widget.

    class reusableCard extends StatelessWidget {
      reusableCard({required this.colour, this.cardChild});
      final Color colour;
      final Widget? cardChild;
    
      @override
      Widget build(BuildContext context) {
        return Container(
          decoration: BoxDecoration(
            color: colour,
            borderRadius: BorderRadius.circular(20),
          ),
          margin: EdgeInsets.all(20),
          child: cardChild,
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search