Flutter小部件未显示

10
下面的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'),
                   ),
                 ],
               ),
             ],
           ),
         )),
   );
 }
}
3个回答

21

根据https://dev59.com/gVcO5IYBdhLWcg3wVAE0#45990477的说法,如果您在行中放置没有固有宽度的小部件,您必须将它们嵌套在一个灵活的小部件中。

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,
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          )),
    );
  }
}

好的回答!非常感谢。 - Gathua Kiragu

0

也可以尝试使用其他指定颜色的方式:

Color(0xff041421)

或者

Colors.red

有时候我们会忽略这些小细节。

0
使用新的Flexible()来处理子Widget。
  Column(
  mainAxisAlignment: MainAxisAlignment.start,
  children: [
        Row(
        children: [
            new Flexible(
                child: new MyText(
                labelText: 'Select Country',
                color: MyColors.hint,),
            ),
            new Flexible(
                child: TextFormField()
            ),
        ],
   ),
],
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接