Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter (Dart): Exceptions caused by rendering / A RenderFlex overflowed

I have a problem with Flutter (Dart) RenderFlex overflowed pixels. An exception of rendering library.

How can I manage or apply scrolling ability to my app page view and avoid Flutter's rendering exceptions with messages like:

A RenderFlex overflowed by 28 pixels on the bottom.

if you by any chance need the full log to help me is here:

enter image description here

on the hot-reload it comes up with the yellow/black stripes at the bottom as per the message.

Is this something I can manage with a scrollable widget? Or I can declare otherwise my widgets in order to control it?

full code if needed (i changed the Text data but assume the texts appearing are longer than the screen size, and thus the error comes up):

   @override   Widget build(BuildContext context) {     return new DefaultTabController(         length: 3,         child: new Scaffold(           appBar: new AppBar(             bottom: new TabBar(               tabs: [                 new Tab(text: "xxx",),                 new Tab(text: "xxx",),                 new Tab(text: "xxx",),               ],             ),             title: new Text(data["xxx"]),           ),           body: new TabBarView(             children: [               new Column(                 children: <Widget>[                   new Text(data["xxx"],                         style: new TextStyle(                               fontStyle: FontStyle.italic,                               color: Colors.blue,                               fontSize: 16.0                         ),),                   new Text(data["xxx"],                         style: new TextStyle(                               fontStyle: FontStyle.italic,                               color: Colors.blue,                               fontSize: 10.0                         ),),                   new Text(data["xxx"],                         style: new TextStyle(                               fontStyle: FontStyle.italic,                               color: Colors.blue,                               fontSize: 16.0                         ),),                   new Text(data["xxx"],                         style: new TextStyle(                               fontStyle: FontStyle.italic,                               color: Colors.blue,                               fontSize: 8.0                         ),                       ),                   new Text(data["xxx"],                         style: new TextStyle(                               fontStyle: FontStyle.italic,                               color: Colors.blue,                               fontSize: 8.0                         ),),                    new Row(                     children: <Widget>[                       new Expanded(                         child: new Text("xxx"),                       ),                       new Expanded(                         child: new Icon(Icons.file_download, color: Colors.green, size: 30.0,),                       ),                     ],                   ),                    new Divider(),                   new Text("xxx",                              style: new TextStyle(                               fontStyle: FontStyle.italic,                               color: Colors.red,                               fontSize: 16.0                       ),                   ),                 ],               ),                new ListView.builder(                 itemBuilder: (BuildContext context, int index) => new EntryItem(_lstTiles[index]),                 itemCount: _lstTiles.length,               ),                new Column(                 children: <Widget>[                   new Text(data["xxx"],                              style: new TextStyle(                               fontStyle: FontStyle.italic,                               color: Colors.green[900],                               fontSize: 16.0                       ),                   ),                   new Text(data["xxx"],                              style: new TextStyle(                               fontStyle: FontStyle.italic,                               color: Colors.green[900],                               fontSize: 16.0                       ),),                   new Text(data["xxx"]),                   new ListTile(title: new Text("xxx")),                   new Text(data["xxx"]),                   new ListTile(title: new Text("xxx")),                   new Divider(),                   new Text("xxx",                              style: new TextStyle(                               fontStyle: FontStyle.italic,                               color: Colors.red,                               fontSize: 16.0                       ),                   ),                 ],               ),             ],           ),         ),       );   } 
like image 349
oetoni Avatar asked Mar 25 '18 19:03

oetoni


People also ask

How do I fix RenderFlex overflowed in flutter?

To fix A RenderFlex overflowed by pixels you just have to Wrap Image in flex widget Expanded , height available is calculated then shared among Expanded (as constraints) and Image is resized to fit inside Expanded constraints.

How do I fix the overflow error in flutter?

Solution : The solution to resolve this overflow error is to make your entire widget or in our case the Column scrollable. We can do that by wrapping our Column inside a SingleChildScrollView. Also, wrap the SingleChildScrollView with Center so that the entire UI is centered.

When should I use SingleChildScrollView?

The SingleChildScrollView is quite useful when we have a single widget, which should be entirely visible. So we can vertically scroll down. The code is quite simple. Now, we can refactor our new column widget.


2 Answers

This is a pretty common issue to run into, especially when you start testing your app on multiple devices and orientations. Flutter's Widget gallery has a section covering the various scrolling widgets:

https://flutter.io/widgets/scrolling/

I'd recommend either wrapping your entire content in a SingleChildScrollView, or using a scrolling ListView.

EDIT: This question and answer have gotten some notice, so I'd like to provide a little more help for those who land here.

The Flutter SDK team puts a lot of effort into good documentation within the SDK code itself. One of the best resources for understanding the algorithm that Flex widgets (Row and Column are both subclasses of Flex) use to lay out their children is the DartDoc that accompanies the class itself:

https://github.com/flutter/flutter/blob/e3005e6962cfefbc12e7aac56576597177cc966f/packages/flutter/lib/src/widgets/basic.dart#L3724

The Flutter website also contains a tutorial on building layouts and an interactive codelab about how to use Row and Column widgets.

like image 71
RedBrogdon Avatar answered Oct 25 '22 17:10

RedBrogdon


Let's say you have a List of 100 Text widgets like this:

final children = List<Widget>.generate(100, (i) => Text('Item $i')).toList(); 

Depending on the device screen, these widgets can overflow, there are few solutions to handle it.

  1. Use Column wrapped in SingleChildScrollView

    SingleChildScrollView(   child: Column(children: children), ) 
  2. Use ListView

    ListView(   children: children ) 
  3. Use combination of both Column and ListView(you should use Expanded/Flexible, or give a fixed height to the ListView when doing so).

    Column(   children: [     ...children.take(2).toList(), // show first 2 children in Column     Expanded(       child: ListView(         children: children.getRange(3, children.length).toList(),       ), // And rest of them in ListView     ),   ], ) 
like image 31
CopsOnRoad Avatar answered Oct 25 '22 17:10

CopsOnRoad