Home > Back-end >  Flutter Flex exception when placing widget at the bottom
Flutter Flex exception when placing widget at the bottom

Time:01-28

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;
  List<ChoiceChipData> timeChips = [
    ChoiceChipData(
        label: "17:00",
        isSelected: false,
        isEnabled: false,
        textColor: Colors.black,
        selectedColor: Colors.blue),
    ChoiceChipData(
        label: "17:30",
        isSelected: false,
        isEnabled: false,
        textColor: Colors.black,
        selectedColor: Colors.blue),
    ChoiceChipData(
        label: "18:00",
        isSelected: false,
        isEnabled: false,
        textColor: Colors.black,
        selectedColor: Colors.blue),
    ChoiceChipData(
        label: "18:30",
        isSelected: false,
        isEnabled: false,
        textColor: Colors.black,
        selectedColor: Colors.blue),
    ChoiceChipData(
        label: "19:00",
        isSelected: false,
        isEnabled: false,
        textColor: Colors.black,
        selectedColor: Colors.blue),
    ChoiceChipData(
        label: "19:30",
        isSelected: false,
        isEnabled: true,
        textColor: Colors.black,
        selectedColor: Colors.blue),
    ChoiceChipData(
        label: "20:00",
        isSelected: false,
        isEnabled: true,
        textColor: Colors.black,
        selectedColor: Colors.blue)
  ];

  @override
  void dispose() {
    super.dispose();
  }

  @override
  initState() {
    super.initState();
  }

  @override
  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",
          ),
        ),
        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),
                    buildTimeChoiceChips(),
                    Expanded(
                      child: Align(
                        alignment: FractionalOffset.bottomCenter,
                        child: MaterialButton(
                          onPressed: () => {},
                          child: MainButton(
                              btnText: "Volgende",
                              btnAction: () => {
                                    Navigator.push(
                                      context,
                                      MaterialPageRoute(
                                          builder: (context) =>
                                              RestaurantReservationTableSelectionPage()),
                                    )
                                  }),
                        ),
                      ),
                    )
                  ],
                )),
          ],
        )
      ],
    )));
  }

  Widget buildTimeChoiceChips() => Wrap(
      runSpacing: 5,
      spacing: 5,
      children: timeChips
          .map((timeChip) => ChoiceChip(
              label: Text(timeChip.label),
              selectedColor: timeChip.selectedColor,
              disabledColor: Colors.grey[150],
              backgroundColor: Colors.grey[300],
              labelStyle: const TextStyle(
                  fontWeight: FontWeight.bold, color: Colors.black),
              onSelected: (timeChip.isEnabled
                  ? (isSelected) => setState(() {
                        timeChips = timeChips.map((otherChip) {
                          final newChip = otherChip.copy(isSelected: false);

                          return timeChip == newChip
                              ? newChip.copy(isSelected: isSelected)
                              : newChip;
                        }).toList();
                      })
                  : null),
              selected: timeChip.isSelected))
          .toList());
}

When I run this code, I get the error:

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#17184 relayoutBoundary=up6 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE(creator: Column ← Padding ← Container ← Column ← Column ← Stack ← MediaQuery ← Padding ← SafeArea ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← ⋯, 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: max, crossAxisAlignment: start, textDirection: ltr, verticalDirection: down)
The creator information is set to:
  Column ← Padding ← Container ← Column ← Column ← Stack ← MediaQuery ← Padding ← SafeArea ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← ⋯
The nearest ancestor providing an unbounded width constraint is: RenderFlex#cace9 relayoutBoundary=up3 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE:
  creator: Column ← Stack ← MediaQuery ← Padding ← SafeArea ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ← ⋯
  parentData: not positioned; 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: max
  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

I have looked at similar problems, but I still don't understand why this is happening. I already tried using Spacer(), but that results in the same error. I thought that it throws the error, because the parent column doesn't have a fixed height, so it doesn't have an ending, but I don't know how to prevent this then?

What am I doing wrong?

EXTRA

The exception that gets thrown is on line 926 of the build-in flex.dart: enter image description here

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()),
                                  ),
                                },
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  •  Tags:  
  • Related