在Flutter app中看不到输入

时间:2017-05-30 18:58:34

标签: flutter

我遵循了本教程:https://codelabs.developers.google.com/codelabs/flutter/index.html#4,但在运行应用程序后,我看不到输入小部件。

enter image description here

main.dart

import 'package:flutter/material.dart';

void main() {
    runApp(new MaterialApp(
        title: "Friendlychat",
        home: new Scaffold(
            appBar: new AppBar(
                title: new Text("Friendlychat")
            )
        )
    ));
}

class FriendlyChatApp extends StatelessWidget {

    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            title: "Friendlychat",
            home: new ChatScreen()
        );
    }
}

class ChatScreen extends StatefulWidget {

    @override
    State createState() => new ChatScreenState();
}

class ChatScreenState extends State<ChatScreen> {

    final TextEditingController _textController = new TextEditingController();

    @override
    Widget build(BuildContext context) {
        return new Scaffold(
            appBar: new AppBar(
                title: new Text("Friendlychat")
            ),
            body: _buildTextComposer()
        );
    }

    Widget _buildTextComposer() {
        return new IconTheme(
            data: new IconThemeData(color: Theme
                .of(context)
                .accentColor),
            child: new Container(
                margin: const EdgeInsets.symmetric(horizontal: 8.0),
                child: new Row(
                    children: <Widget>[
                        new Flexible(
                            child: new TextField(
                                controller: _textController,
                                onSubmitted: _handleSubmitted,
                                decoration: new InputDecoration.collapsed(
                                    hintText: "Send a message"),
                            ),
                        ),
                        new Container(
                            margin: new EdgeInsets.symmetric(horizontal: 4.0),
                            child: new IconButton(
                                icon: new Icon(Icons.send),
                                onPressed: () =>
                                    _handleSubmitted(_textController.text)),
                        ),
                    ]
                ),
            )
        );
    }

    void _handleSubmitted(String text) {
        _textController.clear();
    }
}

1 个答案:

答案 0 :(得分:1)

您没有完成上一步的部分内容。您的问题在于此代码:

void main() {
  runApp(new MaterialApp(
    title: "Friendlychat",
    home: new Scaffold(
      appBar: new AppBar(
        title: new Text("Friendlychat")
      )
    )
  ));
}

您应该将其替换为:

void main() {
  runApp(new FriendlyChatApp());
}