SHARE
TWEET

Untitled

a guest May 24th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import "package:flutter/material.dart";
  2. import 'package:flutter/foundation.dart';
  3. import 'package:web_socket_channel/io.dart';
  4.  
  5. void main() {
  6.   runApp(MyApp());
  7. }
  8.  
  9. class MyApp extends StatelessWidget {
  10.   // Para dibujar en la pantalla
  11.   @override
  12.   Widget build(BuildContext context) {
  13.     // Devuelvo todo el app
  14.     return MaterialApp(
  15.       home: Scaffold(
  16.           appBar: AppBar(
  17.             title: Text('Emergencia'),
  18.           ),
  19.           body: HomePage("Desconectarse", "assets/conectado.png",
  20.               "Esperando emergencias")),
  21.     );
  22.   }
  23. }
  24.  
  25. class HomePage extends StatefulWidget {
  26.   final String state;
  27.   final String path;
  28.   final String phrase;
  29.   //final IOWebSocketChannel channel = new IOWebSocketChannel.connect('ws://echo.websocket.org');
  30.   HomePage(
  31.     this.state,
  32.     this.path,
  33.     this.phrase,
  34.   );
  35.   @override
  36.   State<StatefulWidget> createState() {
  37.     return _HomePageState();
  38.   }
  39. }
  40.  
  41. class _HomePageState extends State<HomePage> {
  42.   String estado = "";
  43.   String ruta = "";
  44.   String frase = "";
  45.   IOWebSocketChannel urlWS;
  46.   @override
  47.   void initState() {
  48.     // TODO: implement initState
  49.     estado = widget.state;
  50.     ruta = widget.path;
  51.     frase = widget.phrase;
  52.     //urlWS = widget.channel;
  53.     urlWS = new IOWebSocketChannel.connect('ws://echo.websocket.org');
  54.     urlWS.sink.add('Hello!');
  55.  
  56.     super.initState();
  57.   }
  58.  
  59.   // para rerender
  60.   @override
  61.   Widget build(BuildContext context) {
  62.     return Column(mainAxisAlignment: MainAxisAlignment.center, children: <
  63.         Widget>[
  64.       Image.asset(ruta),
  65.       Container(
  66.         margin: EdgeInsets.all(10.0),
  67.       ),
  68.       Text(frase, style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold)),
  69.       Container(
  70.         //alignment: Alignment.bottomCenter,
  71.         child: Center(
  72.           child: RaisedButton(
  73.             elevation: 5.0,
  74.             onPressed: () {
  75.               setState(() {
  76.                 if (estado == "Conectarse") {
  77.                   // Iniciar comunicacion con channel
  78.                   estado = "Desconectarse";
  79.                   ruta = "assets/conectado.png";
  80.                   frase = "Esperando emergencias";
  81.                   urlWS =
  82.                       new IOWebSocketChannel.connect('ws://echo.websocket.org');
  83.                   urlWS.sink.add('Hello again!');
  84.                 } else {
  85.                   // Terminar la comunicacion
  86.                   estado = "Conectarse";
  87.                   ruta = "assets/desconectado.png";
  88.                   frase = "Con├ęctate para recibir emergencias";
  89.                   urlWS.sink.add('Bye!');
  90.                   urlWS.sink.close();
  91.                 }
  92.               });
  93.               // Tell flutter that im changing the state
  94.             },
  95.             child: Text(estado, style: TextStyle(fontSize: 40)),
  96.           ),
  97.         ),
  98.  
  99.         //margin: EdgeInsets.all(80),
  100.         margin: EdgeInsets.all(50.0),
  101.       ),
  102.       new StreamBuilder(
  103.         stream: urlWS.stream,
  104.         builder: (context, snapshot) {
  105.           return new Padding(
  106.             padding: const EdgeInsets.all(30.0),
  107.             child: new Text(snapshot.hasData ? '${snapshot.data}' : ''),
  108.           );
  109.         },
  110.       ),
  111.     ]);
  112.   }
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top