I want to add different images each time, I reuses this Card.How to do that without copy pasting the whole Code

Issue

class OwnCard extends StatelessWidget {
  const OwnCard({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Column(
        children: [
          AspectRatio(
            aspectRatio: 18 / 11,
            child: Image.asset('assets/diamond.png'),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(16, 12, 16, 8),
            child: Column(
              children: [
                Text('Title'),
                SizedBox(
                  height: 12,
                ),
                Text('Secondary Text')
              ],
            ),
          )
        ],
      ),
    );

Solution

I prefer this way


class OwnCard extends StatelessWidget {
  final String imagePath;
  final String title;
  final String secondaryText;

  const OwnCard(
      {Key? key,
      required this.imagePath,
      required this.title,
      required this.secondaryText})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Column(
        children: [
          AspectRatio(
            aspectRatio: 18 / 11,
            child: Image.asset(imagePath),
          ),
          Padding(
            padding: const EdgeInsets.fromLTRB(16, 12, 16, 8),
            child: Column(
              children: [
                Text(title),
                const SizedBox(
                  height: 12,
                ),
                Text(secondaryText)
              ],
            ),
          )
        ],
      ),
    );
  }
}


Answered By – Yeasin Sheikh

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *