1

I tried to add a RaisedButton above the ListView. I used a Colum but I got an error. Can anybody tell me where my mistake is? Thanks in advance!

 body: Column(
        children: <Widget>[
          RaisedButton(
            child: Text("Test"),
            onPressed: null,
          ),
          ListView.builder(
            itemCount: exercises.length,
            itemBuilder: (context, i) {
              return Container(
                child: Text(
                  exercises[i]["text"],
                ),
              );
            },
          ),
        ],
      ),
simi
  • 323
  • 4
  • 12

2 Answers2

1

You need to wrap your listview with Expanded widget.

     body: Column(
    children: <Widget>[
      RaisedButton(
        child: Text("Test"),
        onPressed: null,
      ),
      Expanded( // added widget
     child: ListView.builder(
        itemCount: exercises.length,
        itemBuilder: (context, i) {
          return Container(
            child: Text(
              exercises[i]["text"],
            ),
          );
        },
      ),
    ],
  ),
  )
Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61
1

Adding to @Viren answer, You should use Flexible instead of Expanded

body: Column(
        children: <Widget>[
          RaisedButton(
            child: Text("Test"),
            onPressed: null,
          ),
          Flexible(
            child: Container(
              child: ListView.builder(
                itemCount: 5,
                itemBuilder: (context, i) {
                  return Container(
                    child: Text(
                      'jitesh',
                    ),
                  );
                },
              ),
            ),
          )
        ],
      ),

Please find diff Flutter: Expanded vs Flexible

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147