添加文本字段时,Flutter屏幕显示为空

用户名

我在制作一个Flutter应用程序时,里面有一个文本,并且可以正常工作,但是之后,当我添加一个文本字段时,我运行了该应用程序,发现该应用程序为空。

这是我的代码:

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: new MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Scaffold c = Scaffold(
    body: Padding(
      padding: const EdgeInsets.only(top:30.0),
      child: new Row(
        children: <Widget>[
          new TextField(
            decoration: InputDecoration(
                border: InputBorder.none,
                hintText: 'Please enter a search term'
            ),
          ),
          new Text(
            'Text',
            style: TextStyle(
              fontSize: 20.0
            ),
          ),
        ],
      ),
    ),
  );

  @override
  Widget build(BuildContext context) {
    return c;
  }
}
地狱战士

那是因为Row容器不知道TextField小部件的大小

这是您得到的错误:

    following function, which probably computed the invalid constraints in question:
        flutter:   _RenderDecoration._layout.layoutLineBox 
      (package:flutter/src/material/input_decorator.dart:808:11)
        flutter: The offending constraints were:
        flutter:   BoxConstraints(w=Infinity, 0.0<=h<=379.0)

为了解决该问题,请在您的Textfield的父容器中为其设置一个宽度,如下所示:

Container(
        width: 200.0,
        child: new TextField(
          decoration: InputDecoration(
              border: InputBorder.none,
              hintText: 'Please enter a search term'),
        ),
      ),

但它在屏幕上看起来很奇怪,因此您可以使用Flexible作为您TextFieldText

        Scaffold c = Scaffold(
            body: Padding(
              padding: const EdgeInsets.only(top:30.0),
              child: new Row(
                children: <Widget>[
                  Flexible(
                    flex: 1,
                              child: new TextField(
                      decoration: InputDecoration(
                          border: InputBorder.none,
                          hintText: 'Please enter a search term'
                      ),
                    ),
                  ),
                  Flexible(
                    flex: 1,
                              child: new Text(
                      'Text',
                      style: TextStyle(
                        fontSize: 20.0
                      ),
                    ),
                  ),
                ],
              ),
            ),
          );

灵活的小部件

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

仅当reactjs的文本字段中的特定对象值为空时,如何显示错误注释

当文本字段为空时隐藏自动完成 Tableview

PDFBox EOFException:设置文本字段值时为空

检查文本字段是否为空

单击时显示文本字段(输入)

Flutter Firestore 在文本字段中输入字段中的数据时显示文档 ID

防止在文本字段为空时用户按下退格按钮时出现错误消息

JavaFX如何检查所有动态添加的文本字段是否为空

如果文本字段为空,如何向用户显示通知

Python Selenium文本字段的打印输出值显示为空。该值不打印

如何在Flutter中为文本字段自动显示键盘

当文本字段为空且没有光标时将背景透明(onUnfocus)

当文本字段为空时,JS从div中删除类?

Flutter-键盘出现在Android上时未显示文本字段

ms access-将光标在文本字段中的位置设置为仅在文本框为空时开始

Formarray:仅当所选文本字段不为空时,才添加新文本字段。并且无法在formarray文本框中键入多个字母

未触发时,表单中的 SwiftUI 条件文本字段显示为空白

文本字段退格操作确定何时文本字段为空?

Flutter:如何在没有android底部导航栏聚焦文本字段的情况下显示屏幕键盘?

点击文本字段时如何突出显示文本

检查collectionView中的文本字段是否为空

只要多个文本字段为空,请禁用按钮

如果文本字段为空,则禁用AjaxSubmitButton

Swift UI:如果文本字段为空,则禁用按钮

如何快速检查文本字段是否为空

如果子报表为空,则隐藏文本字段

如何快速检测文本字段是否为空?

检查文本字段是否为空Swift

指令接受数字并在文本字段中为空