flutter RenderCustomMultiChildLayoutBox对象在布局过程中被赋予了无限大的大小,我该如何解决这个问题?

tf7tbtn2  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(205)

我运行了这段代码,它返回了一个错误:RenderCustomMultiChildLayoutBox object was given an infinite size during layout.我无法确定哪个小部件出错。这是我的代码。

import 'package:flutter/material.dart';

import '../mediaquery.dart';

class DropdownTextfield extends StatefulWidget {
  @override
  _DropdownTextfieldState createState() => _DropdownTextfieldState();
}

class _DropdownTextfieldState extends State<DropdownTextfield> {

  var properties = [
    'kg',
    'yd',
    'm',
  ];

  String dropdownvalue = 'kg';
  String _property1 = '';
  String _property2 = '';
  String _property3 = '';
  bool _isOptionSelected = false;

  TextEditingController quantitycontroller = TextEditingController();

  var selectedOption;
  TextEditingController textfieldValue = TextEditingController();
  final List<String> options = [];

  @override
  void initState() {
    super.initState();
    selectedOption = options.isNotEmpty ? options[0] : null;
  }

  @override
  Widget build(BuildContext context) {

    final screenHeight = ScreenInfo.screenHeight(context);
    final screenWidth = ScreenInfo.screenWidth(context);

    return Scaffold(
      body: SingleChildScrollView(
        child: SizedBox(
          height: screenHeight * 0.8,
          child: Column(
              children: [
                TextField(
                  onChanged: (value) {
                    setState(() {
                      textfieldValue.text = value;
                    });
                  },
                ),
                DropdownButton<String>(
                  value: selectedOption,
                  onChanged: (value) {
                    setState(() {
                      selectedOption = value!;
                      _isOptionSelected = true;
                    });
                  },
                  hint: const Text('Input from Text Field Above'),
                  items: options.map((option) {
                    return DropdownMenuItem<String>(
                      value: option,
                      child: Text(option),
                    );
                  }).toList(),
                ),
                TextButton(
                  onPressed: () {
                    setState(() {
                      options.add(textfieldValue.text);
                    });
                  },
                  child: Text("Add Option"),
                ),
                Visibility(
                  visible: _isOptionSelected,
                  child: Column(
                    children: <Widget>[
                      Row(
                        children: [
                          TextField(
                            controller: quantitycontroller,
                            decoration: InputDecoration(labelText: "Quantity"),
                            onChanged: (value) {
                              setState(() {
                                _property1 = value;
                              });
                            },
                          ),
                          const SizedBox(width: 10,),
                          DropdownButton(
                            value: dropdownvalue,
                              items: properties.map((properties) {
                                return DropdownMenuItem(
                                  value: properties,
                                  child: Text(properties),
                                );
                              }).toList(),
                              onChanged: (String? newValue){
                              setState(() {
                                dropdownvalue = newValue!;
                              });
                              },
                          )
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
        ),
      ),
    );
  }
}

我试着用ContainerSizedBox来固定高度,但是好像什么也没做。我也试着用ExpandedFlexible来 Package Row小部件的children,但是好像也没解决问题。我该怎么做呢?

9wbgstp7

9wbgstp71#

TextField正在尝试获取无限宽度,而行小部件提供了该宽度。请将TextField与Expanded小部件分隔以获取可用空间。

Expanded(
  child: TextField(
    controller: quantitycontroller,

相关问题