Search code examples
androidflutterlistviewwidgetandroid-cardview

Multiple cards inside a list widget in flutter


I am a beginner in Flutter and i am creating an demo application but in that application I want to add another card in the list widget(code is mentioned below) but somehow it shows an error.

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


class Intro extends StatefulWidget {
  const Intro({Key? key}) : super(key: key);

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

class _IntroState extends State<Intro> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
            children:

    List<Widget>[
      Container(
        child: Padding(
          padding: const EdgeInsets.all(36.0),
          child: Center(
            child: Card(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const ListTile(
                    title: Text('Goal'),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      TextButton(
                        child: const Text('>'),
                        onPressed: () {
                          showAlertDialog(context);
                        },
                      ),
                      const SizedBox(width: 8),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
      Container(
        child: Padding(
          padding: const EdgeInsets.all(36.0),
          child: Center(
            child: Card(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const ListTile(
                    title: Text('objective'),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      TextButton(
                        child: const Text('>'),
                        onPressed: () {
                          showAlertDialog1(context);
                        },
                      ),
                      const SizedBox(width: 8),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    ]
    )
    );
  }
}

showAlertDialog(BuildContext context) {
  // Create button
  Widget okButton = FlatButton(
    child: Text("OK"),
    onPressed: () {
      Navigator.of(context).pop(MaterialPageRoute(builder: (context) => Intro()));
    },
  );

  // Create AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text("Simple Alert"),
    content: Text("This is an alert message."),
    actions: [
      okButton,
    ],
  );

  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

showAlertDialog1(BuildContext context) {
  // Create button
  Widget okButton = FlatButton(
    child: Text("OK"),
    onPressed: () {
      Navigator.of(context).pop(MaterialPageRoute(builder: (context) => Intro()));
    },
  );

  // Create AlertDialog
  AlertDialog alert = AlertDialog(
    title: Text("Simple Alert"),
    content: Text("This is an alert message."),
    actions: [
      okButton,
    ],
  );

  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

Also , I tried by creating a loop of widget still it shows an error in Widget build(BuildContext context)!

So show me a way to run this code successfully!


Solution

  • Remove List<widget> after Column( children:

    Try this code:

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    
    class Intro extends StatefulWidget {
     const Intro({Key? key}) : super(key: key);
    
     @override
      _IntroState createState() => _IntroState();
     }
    
    class _IntroState extends State<Intro> {
    
    @override
    Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
            children:[
      Container(
        child: Padding(
          padding: const EdgeInsets.all(36.0),
          child: Center(
            child: Card(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const ListTile(
                    title: Text('Goal'),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      TextButton(
                        child: const Text('>'),
                        onPressed: () {
                          showAlertDialog(context);
                        },
                      ),
                      const SizedBox(width: 8),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
      Container(
        child: Padding(
          padding: const EdgeInsets.all(36.0),
          child: Center(
            child: Card(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  const ListTile(
                    title: Text('objective'),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      TextButton(
                        child: const Text('>'),
                        onPressed: () {
                          showAlertDialog1(context);
                        },
                      ),
                      const SizedBox(width: 8),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    ]
    )
    );
    }}}