Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Listview in Cardview in flutter?

Tags:

flutter

The below code is about a stateful class with Scaffold that contains Listview in its body, and one of its children is about a stack that has some children itself, I want to use ListView as a child of card widget in order to scroll data table that is inside the card, but when I use list view inside the card view, all of the things in Scaffold get disappeared,but when there is no list view, everything came back,

  class DevicePageState extends State<DevicePage>{
    Widget bodyData()=>DataTable(
        columns:<DataColumn>[
          DataColumn(
            label: Text('وضعیت',style: TextStyle(color: Colors.deepPurple,fontWeight: FontWeight.bold,fontSize: 14.0),),
            numeric: false,
            onSort: (i,b){},
            tooltip: "to display first name of th e name"
          ),
          DataColumn(
              label: Text('عملکرد',style: TextStyle(color: Colors.deepPurple,fontWeight: FontWeight.bold,fontSize: 14.0),),
              numeric: false,
              onSort: (i,b){},
              tooltip: "to display Last name of th e name"
          ),
        ],
      rows: names.map((name)=>DataRow(
        cells: [
          DataCell(
          new Text(name.firstName, style: TextStyle(color: Colors.blueAccent,fontWeight: FontWeight.bold,fontSize: 12.0 ),
            ),
          showEditIcon: false,
          placeholder: false,
          ),
          DataCell(
            new Text(name.lastName,style: TextStyle(color: Colors.blueAccent,fontWeight: FontWeight.bold,fontSize: 12.0),),
            showEditIcon: false,
            placeholder: false,
          ),

        ],
      ),
      ).toList()
    ) ;


    @override
    Widget build (BuildContext){
      return new Scaffold(
      appBar:AppBar(
        title:  new Text('خانه هوشمند'),
      ),
      body:
          new ListView(
            children: <Widget>[
              new Container(
                padding: EdgeInsets.all(30.0),
                child: new Image.asset('images/acc.png'),
              ),
              new ListTile(
                title: new Text('نام دستگاه',textAlign: TextAlign.right,style: TextStyle(fontSize: 25.0),),
                subtitle:  new Text('کولر',textAlign: TextAlign.right,style: TextStyle(fontSize: 20.0),),
              ),
               new Stack(
                children: <Widget>[
                  new Container(
                    padding: EdgeInsets.all(300.0),
                    decoration:  new BoxDecoration(
                      image:DecorationImage(image: new AssetImage('images/c.PNG'),fit: BoxFit.cover),

                    ),
                  ),
                 new Card(
                   margin: EdgeInsets.only(left: 77.0,top: 128.0),
                   color: Color.fromRGBO(255, 255, 255, 0.85),
                   child:
                   new ListView(
                     children: <Widget>[
                       Container(
                         child: bodyData(),

                       ),
                     ],
                   ),
                 ),

                ],
              ),
            ],
          ),

      );

  }
  }
  class Name {
  String firstName;
  String lastName;

  Name({this.firstName,this.lastName});
}

var names = <Name>[
  Name(firstName: 'روشن',lastName: "پمپ آب"),
  Name(firstName: 'خاموش',lastName: "دور کند"),
  Name(firstName: 'روشن',lastName: "دور تند"),


];
like image 236
ElhamKeshavarz Avatar asked Sep 23 '18 07:09

ElhamKeshavarz


People also ask

How do you show list in card flutter?

Cards in ListView To use Card in Flutter, we will use the Card widget. The Card widget has a child prop that lays out a child widget, like this: Card(child: Text("A card.")) The Card widget renders a Text widget with the text “A card.”

How do you make a list dynamically in flutter?

We can use ListView. builder(..) in Stateless Widgets if we want to display dynamic (different) contents every time the widget is loaded within an application. With Stateful widgets, it can change the contents of the screen dynamically.

How do I display list of images in flutter?

and append the image. Step 4: Now in the body of the scaffold, Create a ListView widget, In ListView first, we will use the row to show the item count and a button. Now add one more child to the listview, which will show the list of images.


1 Answers

You must set shrinkWrap property of ListView to true.

like image 198
vipin agrahari Avatar answered Nov 01 '22 16:11

vipin agrahari