'Flutter RenderFlex constraint exception
I have a problem. I am trying to put my custom button at the bottom of my screen by using the following code:
import 'package:test/models/choice_chip_data.dart';
import 'package:test/page/restaurant_reservation_table_selection.dart';
import 'package:test/styles/button_style.dart';
import 'package:test/widgets/reservation_person_selection.dart';
import 'package:test/widgets/reservation_time_selection.dart';
import 'package:test/widgets/restaurant_rating.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
final DateFormat formatter = DateFormat('dd-MM-yyyy');
class RestaurantReservationDetailsPage extends StatefulWidget {
@override
_RestaurantReservationDetailsPageState createState() =>
_RestaurantReservationDetailsPageState();
}
class _RestaurantReservationDetailsPageState
extends State<RestaurantReservationDetailsPage> {
DateTime? reservationDateTime;
@override
void dispose() {
super.dispose();
}
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.black,
),
iconSize: 25,
onPressed: () {
Navigator.pop(context);
},
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Senso Sushi & Grill",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
RestaurantRating()
],
)
],
)
],
),
Container(
height: 180,
width: 180,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(90),
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(229, 229, 229, 1),
Color.fromRGBO(253, 253, 253, 1),
],
)),
padding: EdgeInsets.all(20),
margin: EdgeInsets.only(top: 50, bottom: 50),
child: Image.asset(
"assets/restaurant_logo_sample.png",
),
),
Column(
children: [
Container(
padding: EdgeInsets.only(left: 30, right: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text("Aantal personen",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
CustomNumberInput(),
SizedBox(height: 15),
Text("Datum",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
MainButton(
btnText: (reservationDateTime == null
? "Datum kiezen"
: formatter.format(reservationDateTime!)),
btnAction: () => {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime.now()
.add(Duration(days: 14)))
.then((date) => {
setState(() {
reservationDateTime = date;
})
})
}),
Spacer(flex: 1),
MainButton(
btnText: "Volgende",
btnAction: () => {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
RestaurantReservationTableSelectionPage()),
)
}),
],
)),
],
)
],
)));
}
}
I am trying to build the following screen:
Now all the elements of that page are on the screen, but the last thing I am trying to do is putting the button to the bottom of the screen. To do that I added the Spacer(flex: 1) on line: 128. Unfortunately, when I run this code, it crashes on a build-in exception in the flex.dart file:

I have tried setting all the mainAxisSize: MainAxisSize.min of all the Column() widgets, but that still doesn't work...
Here is the full error output:
RenderFlex children have non-zero flex but incoming height constraints are unbounded.
When a column is in a parent that does not provide a finite height constraint, for example if it is in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining space in the vertical direction.
These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child cannot simultaneously expand to fit its parent.
Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible children (using Flexible rather than Expanded). This will allow the flexible children to size themselves to less than the infinite remaining space they would otherwise be forced to take, and then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum constraints provided by the parent.
If this message did not help you determine the problem, consider using debugDumpRenderTree():
https://flutter.dev/debugging/#rendering-layer
http://api.flutter.dev/flutter/rendering/debugDumpRenderTree.html
The affected RenderFlex is:
RenderFlex#595d0 relayoutBoundary=up5 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE(creator: Column ← Padding ← Container ← Column ← Column ← MediaQuery ← Padding ← SafeArea ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← ⋯, parentData: offset=Offset(0.0, 0.0) (can use size), constraints: BoxConstraints(0.0<=w<=324.0, 0.0<=h<=Infinity), size: MISSING, direction: vertical, mainAxisAlignment: start, mainAxisSize: min, crossAxisAlignment: start, textDirection: ltr, verticalDirection: down)
The creator information is set to:
Column ← Padding ← Container ← Column ← Column ← MediaQuery ← Padding ← SafeArea ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← ⋯
The nearest ancestor providing an unbounded width constraint is: RenderFlex#7c6dc relayoutBoundary=up2 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE:
creator: Column ← MediaQuery ← Padding ← SafeArea ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#4ac50 ink renderer] ← ⋯
parentData: offset=Offset(0.0, 0.0) (can use size)
constraints: BoxConstraints(0.0<=w<=384.0, 0.0<=h<=800.4)
size: MISSING
direction: vertical
mainAxisAlignment: start
mainAxisSize: min
crossAxisAlignment: center
verticalDirection: down
See also: https://flutter.dev/layout/
If none of the above helps enough to fix this problem, please don't hesitate to file a bug:
https://github.com/flutter/flutter/issues/new?template=2_bug.md
What I know so far is that the exception is caused by the Spacer(flex: 1) line. I added that line to add space between the other components and the button, so the button will be moved to the bottom. This is not the case...
Please let me know how I can fix this!?
Solution 1:[1]
You may try this:
Widget build(BuildContext context) {
return new Scaffold(
body: SafeArea(
child: Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.black,
),
iconSize: 25,
onPressed: () {
Navigator.pop(context);
},
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Senso Sushi & Grill",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
RestaurantRating(),
],
)
],
),
Container(
height: 180,
width: 180,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(90),
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(229, 229, 229, 1),
Color.fromRGBO(253, 253, 253, 1),
],
),
),
padding: EdgeInsets.all(20),
margin: EdgeInsets.only(top: 50, bottom: 50),
child: Image.asset(
"assets/restaurant_logo_sample.png",
),
),
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
Container(
padding: EdgeInsets.only(left: 30, right: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Aantal personen", style: TextStyle(color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
CustomNumberInput(),
SizedBox(height: 15),
Text("Datum", style: TextStyle(color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
MainButton(
btnText: (reservationDateTime == null ? "Datum kiezen" : formatter.format(reservationDateTime!)),
btnAction: () => {
showDatePicker(context: context, initialDate: DateTime.now(), firstDate: DateTime.now(), lastDate: DateTime.now().add(Duration(days: 14))).then(
(date) => {
setState(
() {
reservationDateTime = date;
},
)
},
),
},
),
SizedBox(height: 15),
Text("Tijd", style: TextStyle(color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
Expanded(child: buildTimeChoiceChips()),
Align(
alignment: FractionalOffset.bottomCenter,
child: MaterialButton(
onPressed: () => {},
child: MainButton(
btnText: "Volgende",
btnAction: () => {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => RestaurantReservationTableSelectionPage()),
),
},
),
),
)
],
),
),
],
),
),
),
],
),
),
);
}
Solution 2:[2]
Try this:
Widget build(BuildContext context) {
return new Scaffold(
body: SafeArea(
child: Column(
children: [
Row(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.black,
),
iconSize: 25,
onPressed: () {
Navigator.pop(context);
},
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Senso Sushi & Grill",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
RestaurantRating()
],
)
],
)
],
),
Container(
height: 180,
width: 180,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(90),
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(229, 229, 229, 1),
Color.fromRGBO(253, 253, 253, 1),
],
)),
padding: EdgeInsets.all(20),
margin: EdgeInsets.only(top: 50, bottom: 50),
child: Image.asset(
"assets/restaurant_logo_sample.png",
),
),
Expanded(
child: Column(
children: [
Container(
padding: EdgeInsets.only(left: 30, right: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Aantal personen",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
CustomNumberInput(),
SizedBox(height: 15),
Text("Datum",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
MainButton(
btnText: (reservationDateTime == null
? "Datum kiezen"
: formatter.format(reservationDateTime!)),
btnAction: () => {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime.now()
.add(Duration(days: 14)))
.then((date) => {
setState(() {
reservationDateTime = date;
})
})
}),
SizedBox(height: 15),
Text("Tijd",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
Expanded(child: buildTimeChoiceChips()),
Align(
alignment: FractionalOffset.bottomCenter,
child: MaterialButton(
onPressed: () => {},
child: MainButton(
btnText: "Volgende",
btnAction: () => {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
RestaurantReservationTableSelectionPage()),
)
}),
),
)
],
)),
],
),
)
],
)));
}
Solution 3:[3]
You have a Column inside a Column.
The inner column needs to know how big it can be to layout properly.
As there is only one child in the Column, it would make sense to remove the Column entirely, or wrap it in a Flex or Expanded.
(Code marked near the bottom)
import 'package:test/models/choice_chip_data.dart';
import 'package:test/page/restaurant_reservation_table_selection.dart';
import 'package:test/styles/button_style.dart';
import 'package:test/widgets/reservation_person_selection.dart';
import 'package:test/widgets/reservation_time_selection.dart';
import 'package:test/widgets/restaurant_rating.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
final DateFormat formatter = DateFormat('dd-MM-yyyy');
class RestaurantReservationDetailsPage extends StatefulWidget {
@override
_RestaurantReservationDetailsPageState createState() =>
_RestaurantReservationDetailsPageState();
}
class _RestaurantReservationDetailsPageState
extends State<RestaurantReservationDetailsPage> {
DateTime? reservationDateTime;
@override
void dispose() {
super.dispose();
}
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconButton(
icon: Icon(
Icons.arrow_back_ios,
color: Colors.black,
),
iconSize: 25,
onPressed: () {
Navigator.pop(context);
},
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Senso Sushi & Grill",
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
RestaurantRating()
],
)
],
)
],
),
Container(
height: 180,
width: 180,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(90),
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color.fromRGBO(229, 229, 229, 1),
Color.fromRGBO(253, 253, 253, 1),
],
)),
padding: EdgeInsets.all(20),
margin: EdgeInsets.only(top: 50, bottom: 50),
child: Image.asset(
"assets/restaurant_logo_sample.png",
),
),
////////////////////
Column( <<<<<< HERE (Remove?)
////////////////////
children: [
Container(
padding: EdgeInsets.only(left: 30, right: 30),
////////////////////////////////////////////
child: Column( <<<<<< COLUMN INSIDE A COLUMN
////////////////////////////////////////////
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text("Aantal personen",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
CustomNumberInput(),
SizedBox(height: 15),
Text("Datum",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
MainButton(
btnText: (reservationDateTime == null
? "Datum kiezen"
: formatter.format(reservationDateTime!)),
btnAction: () => {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime.now()
.add(Duration(days: 14)))
.then((date) => {
setState(() {
reservationDateTime = date;
})
})
}),
Spacer(flex: 1),
MainButton(
btnText: "Volgende",
btnAction: () => {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
RestaurantReservationTableSelectionPage()),
)
}),
],
)),
],
)
],
)));
}
}
Solution 4:[4]
Try this if any query please comment:-
Output:-

Code:-
import 'package:flutter/material.dart';
class DemoScreen extends StatelessWidget {
DemoScreen({Key? key}) : super(key: key);
int aantalPersonen = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
top: true,
child: Column(
children: [
Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconButton(
icon: const Icon(
Icons.arrow_back_ios,
color: Colors.black,
),
iconSize: 25,
onPressed: () {
Navigator.pop(context);
},
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text(
"Senso Sushi & Grill",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 18),
),
Text("--- Add Rating bar ---"),
],
),
],
),
Container(
height: 180,
width: 180,
padding: const EdgeInsets.all(20),
margin: const EdgeInsets.only(top: 50, bottom: 50),
decoration: const BoxDecoration(
shape: BoxShape.circle, color: Colors.red),
),
Container(
alignment: Alignment.topLeft,
padding: const EdgeInsets.only(left: 30, right: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
const Text("Aantal personen",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3),
fontSize: 16)),
const SizedBox(height: 5),
Container(
color: Colors.green,
padding: const EdgeInsets.symmetric(
horizontal: 8, vertical: 10),
child: const Text("Add CustomNumberInput"),
),
const SizedBox(height: 15),
const Text("Datum",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3),
fontSize: 16)),
const SizedBox(height: 5),
Container(
color: Colors.green,
padding: const EdgeInsets.symmetric(
horizontal: 8, vertical: 10),
child: const Text("Add CustomNumberInput"),
),
],
),
),
],
),
Expanded(child: Container()),
GradientButtonFb4(
onPressed: () {},
text: "Volgende",
)
],
),
),
);
}
}
class GradientButtonFb4 extends StatelessWidget {
final String text;
final Function() onPressed;
const GradientButtonFb4(
{required this.text, required this.onPressed, Key? key})
: super(key: key);
final double borderRadius = 25;
@override
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(borderRadius),
gradient: const LinearGradient(
colors: [Color(0xff53E88B), Color(0xff15BE77)])),
child: ElevatedButton(
style: ButtonStyle(
elevation: MaterialStateProperty.all(0),
alignment: Alignment.center,
padding: MaterialStateProperty.all(const EdgeInsets.only(
right: 75, left: 75, top: 15, bottom: 15)),
backgroundColor: MaterialStateProperty.all(Colors.transparent),
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(borderRadius)),
)),
onPressed: onPressed,
child: Text(
text,
style: const TextStyle(color: Colors.white),
)));
}
}
Solution 5:[5]
1st -> Remove Spacer() //which causes error
then add this: /* Expanded added . try this */
Column(
children: [
Container(
padding: EdgeInsets.only(left: 30, right: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text("Aantal personen",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
CustomNumberInput(),
SizedBox(height: 15),
Text("Datum",
style: TextStyle(
color: Color.fromRGBO(0, 0, 0, 0.3), fontSize: 16)),
SizedBox(height: 5),
MainButton(
btnText: (reservationDateTime == null
? "Datum kiezen"
: formatter.format(reservationDateTime!)),
btnAction: () => {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime.now(),
lastDate: DateTime.now()
.add(Duration(days: 14)))
.then((date) => {
setState(() {
reservationDateTime = date;
})
})
}),
Expanded(child:
MainButton(
btnText: "Volgende",
btnAction: () => {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
RestaurantReservationTableSelectionPage()),
)
}),
],
)),
],
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sowat Kheang |
| Solution 2 | |
| Solution 3 | ConcenTech |
| Solution 4 | Jeel Bhatti |
| Solution 5 | Tasnuva Tavasum oshin |
