【问题标题】:List View disappears after adding list tile inside a row FLUTTER在行 FLUTTER 中添加列表图块后,列表视图消失
【发布时间】:2021-01-12 17:15:50
【问题描述】:

我在编辑用户个人资料屏幕上工作,我需要创建一个 Row,它应该采用两个 RadioListTile 小部件来表示性别。但在列表视图中添加行后,一切都消失了,我得到了这个错误

2: 'child.hasSize': is not true.
I/flutter (31410): Another exception was thrown: NoSuchMethodError: The
getter 'scrollOffsetCorrection' was called on null.
I/flutter (31410): Another exception was thrown: NoSuchMethodError: The
method 'debugAssertIsValid' was called on null.
I/flutter (31410): Another exception was thrown: NoSuchMethodError: The
getter 'visible' was called on null.

我真的不明白,如果我将它们作为列小部件的直接子级,它工作正常,但我需要将性别放在一行内,尝试搜索解决方案但找不到任何解决方案,请帮助伙计们,这是我下面的代码

class EditUserProfile extends StatefulWidget {
  static String id = 'edit-profile';
  @override
  _EditUserProfileState createState() => _EditUserProfileState();
}

enum Gender { male, female }

class _EditUserProfileState extends State<EditUserProfile> {
  Gender _gender = Gender.male;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Colors.black),
        title: Text(
          "Edit Profile",
          style: TextStyle(color: Colors.black),
        ),
        actions: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: RaisedButton(
              onPressed: null,
              child: Text('Done'),
            ),
          )
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: ListView(
          children: [
            InputField(
              icon: Icon(
                Icons.person,
                color: Colors.white,
              ),
              showText: true,
              labelText: 'Nickname',
              hintText: 'What do they call you?',
            ),
            SizedBox(
              height: 10,
            ),
            InputField(
              icon: Icon(
                Icons.phone_in_talk,
                color: Colors.white,
              ),
              showText: true,
              labelText: 'Mobile Number',
              hintText: 'How can people reach your cell?',
            ),
            SizedBox(
              height: 10,
            ),
            InputField(
              icon: Icon(
                FontAwesomeIcons.child,
                color: Colors.white,
              ),
              showText: true,
              labelText: 'Age',
              hintText: 'How old are you?',
            ),
            SizedBox(
              height: 10,
            ),
            InputField(
              icon: Icon(
                Icons.person,
                color: Colors.white,
              ),
              showText: true,
              labelText: 'Gender',
              hintText: 'Are you single or married?',
            ),
            SizedBox(
              height: 10,
            ),
            Expanded(
              child: Row(
                children: [
                  RadioListTile<Gender>(
                    title: Text('Male'),
                    value: Gender.male,
                    groupValue: _gender,
                    onChanged: (Gender value) {
                      setState(() {
                        _gender = value;
                      });
                    },
                  ),
                  RadioListTile<Gender>(
                    title: Text('Female'),
                    value: Gender.female,
                    groupValue: _gender,
                    onChanged: (Gender value) {
                      setState(() {
                        _gender = value;
                      });
                    },
                  ),
                ],
              ),
            ),
            InputField(
              icon: Icon(
                FontAwesomeIcons.locationArrow,
                color: Colors.white,
              ),
              showText: true,
              labelText: 'Lives In',
              hintText: 'Where do you reside',
            ),
            SizedBox(
              height: 10,
            ),
            InputField(
              icon: Icon(
                Icons.person,
                color: Colors.white,
              ),
              showText: true,
              labelText: 'Relationship Status',
              hintText: 'Are you single or married?',
            ),
            SizedBox(
              height: 10,
            ),
            Expanded(
              child: Row(
                children: [
                  RadioListTile<Gender>(
                    title: Text('Male'),
                    value: Gender.male,
                    groupValue: _gender,
                    onChanged: (Gender value) {
                      setState(() {
                        _gender = value;
                      });
                    },
                  ),
                  RadioListTile<Gender>(
                    title: Text('Female'),
                    value: Gender.female,
                    groupValue: _gender,
                    onChanged: (Gender value) {
                      setState(() {
                        _gender = value;
                      });
                    },
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

我尝试为我的列设置一个固定的高度和宽度,但仍然没有成功。

child: Expanded(
          child: Container(
            width: double.infinity,
            height: 200,
            child: ListView(
              children: [
                InputField(
                  icon: Icon(
                    Icons.person,
                    color: Colors.white,
                  ),

【问题讨论】:

  • 谢谢,我早先看到并尝试了所有三种方法,但似乎都没有。最后一种方法执行热重载时出现以下错误... /I/flutter (32208): ══╡ 渲染库╞══════════════════ ═══════════════════════════════════════ I/flutter (3222)在 performLayout() 期间抛出:I/flutter (32208): BoxConstraints 强制无限宽度。 I/flutter (32208):这些无效的约束被提供给 RenderParagraph 的 layout()
  • 你能发布一个可执行代码吗,我收到错误:InputField & FontAwesomeIcons

标签: flutter flutter-layout


【解决方案1】:

在列表视图中添加 shrinkwrap = true。

【讨论】:

    【解决方案2】:

    以防万一其他人对此感到困惑,我可以通过用 IntrinsicHeight 包装我的 Row 小部件并用 Flexible 包装每个 RadioListTile 小部件来完成这项工作。我能够在这里找到帮助。 BoxConstraints forces an infinite width

    class EditUserProfile extends StatefulWidget {
      static String id = 'edit-profile';
      @override
      _EditUserProfileState createState() => _EditUserProfileState();
    }
    
    enum Gender { male, female }
    
    class _EditUserProfileState extends State<EditUserProfile> {
      Gender _gender = Gender.male;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.white,
            iconTheme: IconThemeData(color: Colors.black),
            title: Text(
              "Edit Profile",
              style: TextStyle(color: Colors.black),
            ),
            actions: [
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: RaisedButton(
                  onPressed: null,
                  child: Text('Done'),
                ),
              )
            ],
          ),
          body: ListView(
            children: [
              InputField(
                icon: Icon(
                  Icons.person,
                  color: Colors.white,
                ),
                showText: true,
                labelText: 'Nickname',
                hintText: 'What do they call you?',
              ),
              SizedBox(
                height: 10,
              ),
              InputField(
                icon: Icon(
                  Icons.phone_in_talk,
                  color: Colors.white,
                ),
                showText: true,
                labelText: 'Mobile Number',
                hintText: 'How can people reach your cell?',
              ),
              SizedBox(
                height: 10,
              ),
              InputField(
                icon: Icon(
                  FontAwesomeIcons.child,
                  color: Colors.white,
                ),
                showText: true,
                labelText: 'Age',
                hintText: 'How old are you?',
              ),
              SizedBox(
                height: 10,
              ),
              InputField(
                icon: Icon(
                  Icons.person,
                  color: Colors.white,
                ),
                showText: true,
                labelText: 'Gender',
                hintText: 'Are you single or married?',
              ),
              SizedBox(
                height: 10,
              ),
              IntrinsicHeight(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Flexible(
                      child: RadioListTile<Gender>(
                        title: Text('Male'),
                        value: Gender.male,
                        groupValue: _gender,
                        onChanged: (Gender value) {
                          setState(() {
                            _gender = value;
                          });
                        },
                      ),
                    ),
                    Flexible(
                      child: RadioListTile<Gender>(
                        title: Text('Female'),
                        value: Gender.female,
                        groupValue: _gender,
                        onChanged: (Gender value) {
                          setState(() {
                            _gender = value;
                          });
                        },
                      ),
                    ),
                  ],
                ),
              ),
              InputField(
                icon: Icon(
                  FontAwesomeIcons.locationArrow,
                  color: Colors.white,
                ),
                showText: true,
                labelText: 'Lives In',
                hintText: 'Where do you reside',
              ),
              SizedBox(
                height: 10,
              ),
              InputField(
                icon: Icon(
                  Icons.person,
                  color: Colors.white,
                ),
                showText: true,
                labelText: 'Relationship Status',
                hintText: 'Are you single or married?',
              ),
              SizedBox(
                height: 10,
              ),
            ],
          ),
        );
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2011-08-26
      • 2017-02-06
      相关资源
      最近更新 更多