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
Please let me know what is causing this and how I can fix this!?
CodePudding user response:
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()),
)
}),
),
)
],
)),
],
),
)
],
)));
}
CodePudding user response:
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()),
),
},
),
),
)
],
),
),
],
),
),
),
],
),
),
);
}
