mirrored_repositories/flutter_plugins_all/websocket_demo
import 'package:web_socket_channel/io.dart';

import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:websocket_demo/main.dart';

class MainPage extends StatefulWidget
{
  @override
  createState() => _MainPage();
}

class _MainPage extends State<MainPage>
{ 
  TextEditingController _controller = new TextEditingController();
  String _text = "";
  final IOWebSocketChannel channel = new IOWebSocketChannel.connect('ws://127.0.0.1:8000/server/189/');

  void initState(){
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("WebSocket(内容回显)"),
      ),
      body: new Padding(
        padding: const EdgeInsets.all(20.0),
        child: new Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            new Form(
              child: new TextFormField(
                controller: _controller,
                decoration: new InputDecoration(labelText: 'Send a message'),
              ),
            ),
            new StreamBuilder(
              stream: channel.stream,
              builder: (context, snapshot) {
                
                //网络不通会走到这
                if (snapshot.hasError) {
                  _text = "网络不通...";
                } else if (snapshot.hasData) {
                  _text = "echo: "+snapshot.data;
                }
                return new Padding(
                  padding: const EdgeInsets.symmetric(vertical: 24.0),
                  child: new Text(_text),
                );
              },
            )
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _sendMessage,
        tooltip: 'Send message',
        child: new Icon(Icons.send),
      ),
    );
  }

  void _sendMessage()  {
    if (_controller.text.isNotEmpty) {
      print("send new");
      channel.sink.add(_controller.text);
    }
  }

 
}