-1

Note: This widget is child of another Column


I need to use ListView.builder inside Column but to use it inside Column i need to wrap it with Container not only that i need to set the height,
here i used MediaQuery.of(context).size.height and it works,
but with one issue,it is showing that A RenderFlex overflowed by 226 pixels on the bottom.
How to avoid it ?

 Column(
                  children: <Widget>[
                    getTopWidget(),
                    Container(    
                      height: MediaQuery.of(context).size.height,
                      width: MediaQuery.of(context).size.width,
                      child: ListView.builder(
                          itemCount: shapShot.data.length,
                          itemBuilder: (context,index){
                        return  EventCard(
                          title: shapShot.data[index].title,
                          details: shapShot.data[index].details,
                          imageUrl: shapShot.data[index].imageUrl,
                        );
                      }),
                    ),
                  ],
                );


I tried with Flexible and it not worked
When in try with Expanded it makes below error
enter image description here
App screenshot
enter image description here

RenderBox was not laid out: RenderRepaintBoundary#76ded NEEDS-LAYOUT NEEDS-PAINT 'package:flutter/src/rendering/box.dart': Failed assertion: line 1695 pos 12: 'hasSize'


The parent class code

import 'dart:collection';

import 'package:devaayanam/DevaayanamApp/devaayanam_app_communities.dart';
import 'package:devaayanam/DevaayanamApp/devaayanam_app_home_page.dart';
import 'package:devaayanam/DevaayanamApp/devaayanam_app_temples_listing.dart';
import 'package:devaayanam/DevaayanamApp/devaayanam_app_videos.dart';
import 'package:flutter/material.dart';

import '../GradientAppBar.dart';
import 'temples_listing_with_tab_mode.dart';

class DevaayanamAppFramePage extends StatefulWidget {
  DevaayanamAppFramePage({Key key}) : super(key: key);

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

class _DevaayanamAppFramePageState extends State<DevaayanamAppFramePage> {
  int _selectedIndex = 0;
  ListQueue<Widget> page1,page2,page3,page4;
  _DevaayanamAppFramePageState(){
    page1=ListQueue();
    page1.add(DevaayanamAppHomePage());
    page2=ListQueue();
    page2.add(DevaayanamAppTemplesListing());
    page3=ListQueue();
    page3.add(DevaayanamAppCommunities());
    page4=ListQueue();
    page4.add(DevaayanamAppVideos());
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: null,

      bottomNavigationBar: BottomNavigationBar(
          onTap: _onItemTapped,
          unselectedItemColor: Colors.grey,
          selectedItemColor: Colors.grey,
          currentIndex: _selectedIndex,
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
                icon: Icon(
                  Icons.home,
                  color: Colors.grey,
                ),
                title: Text(
                  "Home",
                  style: TextStyle(color: Colors.grey),
                )),
            BottomNavigationBarItem(
                icon: Icon(Icons.assessment),
                title: Text(
                  "Temples",
                  style: TextStyle(color: Colors.grey),
                )),
            BottomNavigationBarItem(
                icon: Icon(Icons.comment),
                title: Text(
                  "Communities",
                  style: TextStyle(color: Colors.grey),
                )),
            BottomNavigationBarItem(
                icon: Icon(Icons.play_arrow),
                title: Text(
                  "Videos",
                  style: TextStyle(color: Colors.grey),
                )),
          ]),

      body: Container(
        child: Column(
          children: <Widget>[GradientAppBar(),getBody()],
        ),
      ),
    );
  }


  Widget getBody(){
    switch(_selectedIndex){
      case 0:
        return page1.last;
      case 1:
        return page2.last;
      case 2:
        return page3.last;
      case 3:
        return page4.last;
    }
  }
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }
}



Midhilaj
  • 4,905
  • 9
  • 45
  • 88

5 Answers5

11

Its happening because of the topWidget(), what you can do is wrap the inner container with Expanded and remove the height you are giving. Expanded widget will make the container take up the complete space that is available.

Column(
              children: <Widget>[
                getTopWidget(),
               //Wrap the container below with expanded
                Container(
                 // remove this height
                  height: MediaQuery.of(context).size.height,
                  width: MediaQuery.of(context).size.width,
                  child: ListView.builder(
                      itemCount: shapShot.data.length,
                      itemBuilder: (context,index){
                    return  EventCard(
                      title: shapShot.data[index].title,
                      details: shapShot.data[index].details,
                      imageUrl: shapShot.data[index].imageUrl,
                    );
                  }),
                ),
              ],
            );

edit: Please note that Expanded won't help unless the column is outer most column in the widget hierarchy. If you have column in a column, wrap upper column's child with expanded.

Yudhishthir Singh
  • 2,941
  • 2
  • 23
  • 42
1

You can use it like this:

Column(
                  children: <Widget>[
                    getTopWidget(),
                    Expanded(    
                      child: ListView.builder(
                          itemCount: shapShot.data.length,
                          itemBuilder: (context,index){
                        return  EventCard(
                          title: shapShot.data[index].title,
                          details: shapShot.data[index].details,
                          imageUrl: shapShot.data[index].imageUrl,
                        );
                      }),
                    ),
                  ],
                );

Because if you are using the MediaQuery.of(context).size.height it will overflow the view. As already showing on the app.

Sunny
  • 3,134
  • 1
  • 17
  • 31
1

First wrap the column with a container with set height:

Container(
      height: MediaQuery.of(context).size.height,
      child: Column(...),
),

Now in the column, wrap the child that you want to expand with Expanded widget so it would expand to the height of the column (with is limited by the outside container height).

Zvi Karp
  • 3,621
  • 3
  • 25
  • 40
0

You can use :

Container(
      height: MediaQuery.of(context).size.height,
      width:MediaQuery.of(context).size.width,
),

to make container as per your screen size, you can also set some percent of your screen for the container. For example to set container height to 60% of screen and width to 40% of screen use:

Container(
      height: MediaQuery.of(context).size.height * 60/100,
      width:MediaQuery.of(context).size.width * 40/100,
),

make an expanded as child of that container and a column or row as child to that expanded to fill the container with required widgets

Container(
      height: MediaQuery.of(context).size.height * 60/100,
      width:MediaQuery.of(context).size.width * 40/100,
     child:Expanded(
           child : Column(
...... 
           ),
      ),
),
Muhammad Usama
  • 321
  • 1
  • 4
  • 12
0

simple way is IntrinsicHeight( childe: ////your widget )

This class is useful, for example, when unlimited height is available and you would like a child that would otherwise attempt to expand infinitely to instead size itself to a more reasonable height.

m3h
  • 74
  • 2