【问题标题】:Flutter screen appears empty when adding text field添加文本字段时颤动屏幕显示为空
【发布时间】:2018-08-18 21:40:15
【问题描述】:

我正在制作一个颤振应用程序,其中有一个文本并且它正在工作,但是之后当我添加一个文本字段时,我运行该应用程序,我发现该应用程序是空的。

这是我的代码:

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;
  }
}

【问题讨论】:

    标签: android dart flutter textfield


    【解决方案1】:

    这是因为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
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              );
    

    Flexible Widget

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-26
    • 2019-08-13
    • 2020-12-27
    • 2021-03-13
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多