0

I want to have a layout with a header followed by two columns. I don't want to use the AppBar for the header as I want to add some other funtionality.

Of the two columns below the header, the left one should display a list and the right one should display details depending on the what is selected in the first.

My problem is that I am trying to use a ListView in the left column but Flutter doesn't let me do that.

The question is, what alternative layout options to I have? Or is there another way of building a list that will work with my layout?

3 Answers 3

1

This shouldn't be a problem if you use standard flutter layouts. All you need is a Row, a ListView and your View for the content.

Here is some sample code that gives you this enter image description here

Row(
  children: <Widget>[
    Expanded(
      flex: 1, //with this you can handle the width
      child: ListView(
        children: List.generate(
          5,
          (index) => Container(
            height: 200,
            color: Colors.red[100 + index * 100],
          ),
        ),
      ),
    ),
    Expanded(
      flex: 1,
      child: Container(
        color: Colors.green,
      ),
    )
  ],
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I need a full width header with two columns underneath. I have amanged this until I add the List builder, then flutter complains.
1

Use the class Row to divide the screen in two under your header vertically.

Multiple rows in body of flutter application

Also, take a look at expand widget: https://api.flutter.dev/flutter/widgets/Expanded-class.html

Very similar example to what you are trying to do: Divide screen into 4 different parts evenly

Pseudocode:

Row(
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: <Widget>[
    Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Expanded(
            child: Container(
              color: Colors.red,
            ),
          ),
          Expanded(
            child: Container(
              color: Colors.yellow,
            ),
          ),
        ],
      ),
    ),
  ],
);

1 Comment

I'm still struggling to create a header with a row and then two columns underneath with the left column displaying the list.
0

The issue I couldn't solve was having a ListView widget with more items than there was space on the page. This is no problem until you try to build this into some kind of layout, where you have to give a container a size. I want the page to be responsive so setting a fixed size is not an opption.

The solution was to find the size of the viewport using MediaQuery and using the result to set the height of a Container widget.

Here's my code:

  Widget build(BuildContext context) {
return Scaffold(
  body: SafeArea(
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        //Header Container
        Container(
          height: 80.0,
          padding: const EdgeInsets.all(8.0),
          color: Colors.blue,
          alignment: Alignment.center,
          child:
          TextField(
            decoration: InputDecoration(hintText: "Enter Text Here"),
          ),
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            //Body Container
            Expanded(
              flex: 2,
              child: SingleChildScrollView(
                padding: const EdgeInsets.all(10.0),
                child: Column(
                  children: <Widget>[
                    Container(
                      height: MediaQuery.of(context).size.height-100.0,
                      alignment: Alignment.center,
                      child: ListView.builder(
                        itemBuilder: (context, index){
                          return Card(
                            child: Padding(
                              padding: const EdgeInsets.all(5.0),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text(_students[index].firstName + ' ' + _students[index].lastName),
                                ],
                              ),
                            ),
                          );
                        },
                        itemCount: _students.length,
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              flex: 3,
              child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: Container(
                    color: Colors.green,
                    alignment: Alignment.topLeft,
                    child: Text('Right')
                ),
              ),
            ),
          ],
        ),
      ],
    ),
  ),
);

} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.