颤振堆积

时间:2020-05-02 12:51:32

标签: flutter flutter-layout

因此,我尝试创建一个屏幕,我进行了一半,但是每当我尝试在行中添加textfield时,我都会对重叠的图像进行一半的处理,但是当我尝试运行它时,它会抛出这样的错误,当我尝试添加文本字段时会发生这种情况

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
The following assertion was thrown during performLayout():
An InputDecorator, which is typically created by a TextField, cannot have an unbounded width.
This happens when the parent widget does not provide a finite width constraint. For example, if the InputDecorator is contained by a Row, then its width must be constrained. An Expanded widget or a SizedBox can be used to constrain the width of the InputDecorator or the TextField that contains it.
'package:flutter/src/material/input_decorator.dart':
Failed assertion: line 926 pos 7: 'layoutConstraints.maxWidth < double.infinity'

设计规范

 Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("Registration", style: TextStyle(color: Colors.black)),
        backgroundColor: Colors.orange,
      ),
      body: SingleChildScrollView(
        child: Stack(
          children: <Widget>[
            Container(
              child: Image.asset('assets/images/gym.png',
                  height: 150, width: double.infinity, fit: BoxFit.fitWidth),
            ),
            Padding(
              padding: EdgeInsets.only(top: 90, left: 20),
              child: CircleAvatar(
                backgroundImage: AssetImage("assets/images/user_avatar.png"),
                radius: 55.0,
              ),
            ),
            Row(
              children: <Widget>[
                TextField(
                  decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: 'USer'),
                ),

                TextField(
                  decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: 'Mo'),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

1 个答案:

答案 0 :(得分:1)

之所以会这样,是因为TextField需要整个屏幕的宽度,为避免这种情况,可以将其包装在Expanded窗口小部件中。

 SingleChildScrollView(
    child: Stack(
      children: [
        Column(
          children: [
            Container(
              child: Image.asset('assets/images/crown.png',
                  height: 150,
                  width: double.infinity,
                  fit: BoxFit.fitWidth),
            ),
            SizedBox(
              height: 50,
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: TextField(
                    decoration: InputDecoration(
                        border: InputBorder.none, hintText: 'USer'),
                  ),
                ),
                Expanded(
                  child: TextField(
                    decoration: InputDecoration(
                        border: InputBorder.none, hintText: 'Mo'),
                  ),
                ),
              ],
            ),
          ],
        ),
        Padding(
          padding: EdgeInsets.only(top: 90, left: 20),
          child: CircleAvatar(
            backgroundImage: AssetImage("assets/images/crown.png"),
            radius: 55.0,
          ),
        ),
      ],
    ),
  ),