Flutter Widgets不显示

时间:2018-05-14 13:51:47

标签: flutter-layout

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

2 个答案:

答案 0 :(得分:1)

https://stackoverflow.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,
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          )),
    );
  }
}

答案 1 :(得分:0)

使用新的Flexible() 儿童小工具

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