Search code examples
flutterscaffold

How to add multiple Children to flutter scaffold body


I am trying to write multiple child to flutter scaffold body: tag.

I am new to flutter and I don't know how to do multiple children inside a body tag. his is what I have tried.

return new Scaffold(
      appBar: AppBar(
        leading: new Icon(Icons.live_tv),
        title: const Text('SnapNews'),
        backgroundColor: Colors.red,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.help),
            onPressed: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (_) => Help()));
            },
          ),
        ],
      ),
      body: new ListView.builder(
        itemCount: 10,
        itemBuilder: (context, index) {
          return new GestureDetector(
            onTap: () {
              print("tapped");
            },
            child: new Padding(
              padding: const EdgeInsets.all(8.0),
              child: new Container(
                color: Colors.grey,
                height: 100.0,
              ),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton.extended(
        elevation: 4.0,
        icon: const Icon(Icons.camera_enhance),
        label: const Text('Snap'),
        backgroundColor: Colors.red,
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (_) => CameraScreen()),
          );
        },
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: BottomAppBar(
        child: new Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            IconButton(
              icon: Icon(null),
              onPressed: () {},
            ),
            IconButton(
              icon: Icon(null),
              onPressed: () {},
            )
          ],
        ),
      ),
    );

And this produces a GUI as below:

enter image description here

But I want to add a Search Bar on the top of this ListView

How can I do this with adding another child/children to the body: tag

enter image description here

Thank you.


Solution

  • You could put the ListView.builder inside a ListView.

    Edit: To make the SearchBar() not scrollable, put it inside Column instead of ListView.

            body: Column(
              children: [
                SearchBar(),
                Expanded(
                  child: ListView.builder(
                    itemCount: 10,
                    itemBuilder: (context, index) {
                      return new GestureDetector(
                        onTap: () {
                          print("tapped");
                        },
                        child: new Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: new Container(
                            color: Colors.grey,
                            height: 100.0,
                          ),
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),