颤振小部件不显示

Posted

技术标签:

【中文标题】颤振小部件不显示【英文标题】:Flutter Widgets Not Displaying 【发布时间】:2018-10-24 05:10:12 【问题描述】:

以下 Flutter 布局(一列中有两行,每行包含一个文本小部件和一个字段小部件)编译并运行 - 但文本和文本字段小部件未显示。您应该看到的是单词“用户名”后跟一行中的文本字段,以及单词“密码”后跟下一行中的文本字段。请问我做错了什么?

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget 
 @override
 Widget build(BuildContext context) 
   return new MaterialApp(
     title: 'Welcome to Flutter',
     home: new Scaffold(
         appBar: new AppBar(
           title: new Text('Welcome to Flutter'),
         ),
         body: new Container(
           padding: new EdgeInsets.all(20.0),
           child: new Column(
             crossAxisAlignment: CrossAxisAlignment.start,
             children: [
               new Row(
                 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                 children: [
                   new Text('User Name'),
                   new TextField(
                     autofocus: true,
                     decoration: new InputDecoration(
                         border: InputBorder.none,
                         hintText: 'Please enter your user name'),
                   ),
                 ],
               ),
               new Row(
                 mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                 children: [
                   new Text('User Name'),
                   new TextField(
                     decoration: new InputDecoration(
                         border: InputBorder.none,
                         hintText: 'Please enter your pasword'),
                   ),
                 ],
               ),
             ],
           ),
         )),
   );
 

【问题讨论】:

【参考方案1】:

根据https://***.com/a/45990477/1649135,如果将没有固有宽度的小部件放在一行内,则必须将它们嵌套在灵活的小部件中。

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return new MaterialApp(
      title: 'Welcome to Flutter',
      home: new Scaffold(
          appBar: new AppBar(
            title: new Text('Welcome to Flutter'),
          ),
          body: new Container(
            padding: new EdgeInsets.all(20.0),
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                new Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    new Flexible(
                      child: new Text('User Name:'),
                    ),
                    new Flexible(
                      child: new Container(
                        margin: const EdgeInsets.all(15.0),
                        padding: const EdgeInsets.all(3.0),
                        decoration: new BoxDecoration(
                            border: new Border.all(color: Colors.blueAccent)),
                        child: new TextField(
                          style: Theme.of(context).textTheme.body1,
                        ),
                      ),
                    ),
                  ],
                ),
                new Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    new Flexible(
                      child: new Text('Password:'),
                    ),
                    new Flexible(
                      child: new Container(
                        margin: const EdgeInsets.all(15.0),
                        padding: const EdgeInsets.all(3.0),
                        decoration: new BoxDecoration(
                            border: new Border.all(color: Colors.blueAccent)),
                        child: new TextField(
                          style: Theme.of(context).textTheme.body1,
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          )),
    );
  

【讨论】:

好答案!非常感谢【参考方案2】:

使用新的 Flexible() 儿童小部件

  Column(
  mainAxisAlignment: MainAxisAlignment.start,
  children: [
        Row(
        children: [
            new Flexible(
                child: new MyText(
                labelText: 'Select Country',
                color: MyColors.hint,),
            ),
            new Flexible(
                child: TextFormField()
            ),
        ],
   ),
],
)

【讨论】:

以上是关于颤振小部件不显示的主要内容,如果未能解决你的问题,请参考以下文章

我调用的小部件未显示在颤振列中

颤振显示裁剪的小部件

通过 addPostFrameCallback 访问 Flutter Provider 时说小部件在小部件树之外,但颤振检查器显示其他情况

如何暗示颤振小部件是可滚动的?

颤振显示上下文操作快捷方式不起作用

有没有办法让颤振小部件违反脚手架填充?