【问题标题】:Flutter RenderIndexedStack object was given an infinite size during layoutFlutter RenderIndexedStack 对象在布局期间被赋予无限大小
【发布时间】:2018-03-31 13:05:49
【问题描述】:

我正在与这个DropDownItem 框错误作斗争,一切似乎都正常,但在加载时弹出黄色超出范围。尝试了几件事,但无法解决。我的Widget 有这个代码。

@override
Widget build(BuildContext context) {
    var _children = <Widget>[
      !_createNew ? _referrerPractice() : _referrerCreate(),
    ];
    return new Scaffold(
        appBar: new AppBar(
          title: new Text(widget.title),
        ),
        body: new Column(
          mainAxisSize: MainAxisSize.max,
          children: _children,
        ));
  }

然后我将其用作功能之一。

Widget _referrerPractice() {
assert(!_createNew);
return new Form(
  key: _formKey2,
  child: new Expanded(
    child: new ListView(
      padding: const EdgeInsets.symmetric(horizontal: 16.0),
      children: <Widget>[
        new InputDecorator(
          decoration: const InputDecoration(
            labelText: 'Referrer Practice',
            hintText: 'Choose Referrer Practice or choose new',
          ),
          isEmpty: _referPractice == null,
          child: new DropdownButton<String>(
            value: _referPractice,
            isDense: true,
            onChanged: (String newValue) {
              setState(() {
                _referPractice = newValue;
              });
            },
            items: practicelist.map((Map value) {
              return new DropdownMenuItem<String>(
                value: value['id'].toString(),
                child: new Text(value['name']),
              );
            }).toList(),
          ),
        ),
        new RaisedButton(
            child: new Text('Start'), onPressed: _startReferrer),
      ],
    ),
  ),
);

}

它似乎在另一个页面上工作,我没有使用函数来填充小部件,只是一个常规的小部件 var,但似乎它应该是相同的,因为结构是相同的。

任何想法我错过了什么?这是一个DropDownButton,但它发生在具有DropDownButton 的结果页面上以及另一个函数。

编辑********** 这是工作代码

final List<String> _allTypeAppt = <String>['Elective OP', 'Hospital Transfer', 'Phone Consult'];

new Form(
      key: _formKey,
      autovalidate: _autovalidate,
      onWillPop: _warnUserAboutInvalidData,
      child: new Expanded(
          child: new ListView(
            padding: const EdgeInsets.symmetric(horizontal: 16.0),
            children: <Widget>[
              new TextFormField(
                decoration: const InputDecoration(
                  icon: const Icon(Icons.person),
                  hintText: 'First Name?',
                  labelText: 'First Name *',
                ),
                controller: _fnameController,

                onSaved: (String value) { referral.fname = value; },
                validator: _validateName,
              ),

              new TextFormField(
                decoration: const InputDecoration(
                  icon: const Icon(Icons.person),
                  hintText: 'Last Name?',
                  labelText: 'Last Name *',
                ),
                controller: _lnameController,
                onSaved: (String value) { referral.lname = value; },
                validator: _validateName,
              ),

              new _DateTimePicker(
                labelText: 'DOB',
                selectedDate: _fromDate,
                selectDate: (DateTime date) {
                  setState(() {
                    _fromDate = date;
                  });
                },
              ),

              new TextFormField(
                decoration: const InputDecoration(
                    icon: const Icon(Icons.phone),
                    hintText: 'How to contact?',
                    labelText: 'Phone Number *',
                ),
                controller: _phoneController,
                keyboardType: TextInputType.phone,
                onSaved: (String value) { referral.contact = value; },
                validator: _validatePhoneNumber,
                // TextInputFormatters are applied in sequence.
                /*inputFormatters: <TextInputFormatter> [
                  WhitelistingTextInputFormatter.digitsOnly,
                  // Fit the validating format.
                  _phoneNumberFormatter,
                ],*/
              ),

              new TextFormField(
                decoration: const InputDecoration(
                  icon: const Icon(Icons.phone),
                  hintText: 'Alt contact?',
                  labelText: 'Alt Phone Number *',
                ),
                controller: _altPhoneController,
                keyboardType: TextInputType.phone,
                onSaved: (String value) { referral.altcontact = value; },
                validator: _validatePhoneNumber,
                // TextInputFormatters are applied in sequence.
                /*inputFormatters: <TextInputFormatter> [
                  WhitelistingTextInputFormatter.digitsOnly,
                  // Fit the validating format.
                  _phoneNumberFormatter,
                ],*/
              ),

              new TextFormField(
                decoration: const InputDecoration(
                  icon: const Icon(Icons.email),
                  hintText: 'Patien Email?',
                  labelText: 'Patient Email *',
                ),
                controller: _emailController,
                onSaved: (String value) { referral.altcontact = value; },
                //validator: _validatePhoneNumber,
                // TextInputFormatters are applied in sequence.
                /*inputFormatters: <TextInputFormatter> [
                  WhitelistingTextInputFormatter.digitsOnly,
                  // Fit the validating format.
                  _phoneNumberFormatter,
                ],*/
              ),

              new TextFormField(
                decoration: const InputDecoration(
                  hintText: 'Tell us about patient',
                  helperText: 'It does not have to be detailed yet',
                  labelText: 'Referral Details',
                ),
                controller: _detailsController,
                maxLines: 5,
              ),

              new InputDecorator(
                decoration: const InputDecoration(
                  labelText: 'Type of Appointment',
                  hintText: 'Choose an Appointment Type',
                ),
                isEmpty: _typeAppt == null,
                child: new DropdownButton<String>(
                  value: _typeAppt,
                  isDense: true,
                  onChanged: (String newValue) {
                    setState(() {
                      _typeAppt = newValue;
                    });
                  },
                  items: _allTypeAppt.map((String value) {
                    return new DropdownMenuItem<String>(
                      value: value,
                      child: new Text(value),
                    );
                  }).toList(),
                ),
              ),

              new RaisedButton(
                child: new Text('Submit Referral'),
                onPressed: _submitData,
              ),

            ],

          )
      )
  ),

【问题讨论】:

  • 我不太明白你的问题,你能说清楚一点吗?您面临的错误是什么?你到底在哪里面对它?请尝试以清晰的方式解释问题?有什么问题?
  • 你能显示在另一个页面上工作的代码吗?

标签: flutter dart layout flutter-layout flutter-widget


【解决方案1】:

我能够重现您的问题,但我不确定您为什么要以这种方式构建布局。您将DropDownButton 作为child 属性提供给InputDecorator,这很好。但是,它会抛出此错误,因为DropDownMenuItem 溢出了您的InputDecorator。换句话说,您不仅将DopDownButton 包含在您的InputDecorator 中,而且您的items 也试图包含在同一个空间中。因此,Flutter 对如何将 items 的列表定位在 InputDecorator 提供的空间内感到困惑。

我真的很困惑您要构建什么样的布局,为什么需要FormInputDecorator

也许可以为您的设计选择提供一些背景信息,以便我们提供更好的帮助。

我用RowTextField 小部件创建了一个更简单的布局,它可能会有所帮助。

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _mySelection;
  String _text = "Choose";
   List<Map> _myJson = [
    {"id": "ID 1310012", "name": "Newcommer"}, {"id": "ID 0000TEMP", "name": "Temp"}];

  Widget _children() {
    TextEditingController _controller = new TextEditingController(text: _text);
    return  new Expanded(child: new ListView(
            padding: const EdgeInsets.symmetric(horizontal: 16.0),
            children: <Widget>[
              new Container (
        child: new Row(
                  children: <Widget>[
                    new Expanded(
                      child: new TextField(
                        controller: _controller,
                      ),
                    ),
                    new DropdownButton(
                      isDense: true,
                      value: _mySelection,
                      items: _myJson.map((Map map) {
                        return new DropdownMenuItem<String>(
                          value: map["id"],
                          child: new Text(
                            map["name"],
                          ),
                        );
                      }).toList(),
                      onChanged: (String newValue) {
                        setState(() {
                          _mySelection = newValue;
                          _text = newValue;
                        });
                      })
              ],),),
              new RaisedButton(
                  child: new Text('Start'), onPressed: null),

            ]
        ));
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text("Test")),
      body:  new Column(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
           _children(),
        ],
      ),
    );
  }
}

【讨论】:

  • 所以我从flutter示例中举了一个下拉按钮的例子,我使用输入装饰器,因为它允许图标以及提示文本和标签。工作代码和非工作代码之间的唯一区别是,在非工作代码中,我使用带有断言的小部件函数填充 _children,以从一种形式更改为另一种形式,而工作代码具有静态列表而不是动态列表创建的项目列表。所以它为什么会抛出错误很奇怪。让它与输入装饰器一起工作的任何其他想法都会很棒,这样我就可以拥有图标等......
  • 你能告诉我正在工作的静态代码吗?而且我仍然认为您根本不需要 InputDecorator
  • 当您在刚刚添加的静态视图上打开下拉菜单时,您确定没有在控制台上收到任何调试错误吗?我的意思是我理解 DropDown 的工作原理,但是您在控制台上看到了什么?
  • 我没有收到任何错误,我只是再次运行它,当非工作运行时我收到我在原始文件中显示的错误,但工作的没有错误。这就是为什么它没有意义。
  • 我有这个简单的构建,我得到了错误,我不明白它是如何与你一起工作的。 dartpad.dartlang.org/51cc48e85041bcd53e2a2cafbc5d4aa8
猜你喜欢
  • 2020-08-13
  • 2021-08-06
  • 2020-05-20
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 2020-04-15
  • 2021-05-13
相关资源
最近更新 更多