Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import "package:flutter/material.dart";
- import 'package:flutter/foundation.dart';
- import 'package:web_socket_channel/io.dart';
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatelessWidget {
- // Para dibujar en la pantalla
- @override
- Widget build(BuildContext context) {
- // Devuelvo todo el app
- return MaterialApp(
- home: Scaffold(
- appBar: AppBar(
- title: Text('Emergencia'),
- ),
- body: HomePage("Desconectarse", "assets/conectado.png",
- "Esperando emergencias")),
- );
- }
- }
- class HomePage extends StatefulWidget {
- final String state;
- final String path;
- final String phrase;
- //final IOWebSocketChannel channel = new IOWebSocketChannel.connect('ws://echo.websocket.org');
- HomePage(
- this.state,
- this.path,
- this.phrase,
- );
- @override
- State<StatefulWidget> createState() {
- return _HomePageState();
- }
- }
- class _HomePageState extends State<HomePage> {
- String estado = "";
- String ruta = "";
- String frase = "";
- IOWebSocketChannel urlWS;
- @override
- void initState() {
- // TODO: implement initState
- estado = widget.state;
- ruta = widget.path;
- frase = widget.phrase;
- //urlWS = widget.channel;
- urlWS = new IOWebSocketChannel.connect('ws://echo.websocket.org');
- urlWS.sink.add('Hello!');
- super.initState();
- }
- // para rerender
- @override
- Widget build(BuildContext context) {
- return Column(mainAxisAlignment: MainAxisAlignment.center, children: <
- Widget>[
- Image.asset(ruta),
- Container(
- margin: EdgeInsets.all(10.0),
- ),
- Text(frase, style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold)),
- Container(
- //alignment: Alignment.bottomCenter,
- child: Center(
- child: RaisedButton(
- elevation: 5.0,
- onPressed: () {
- setState(() {
- if (estado == "Conectarse") {
- // Iniciar comunicacion con channel
- estado = "Desconectarse";
- ruta = "assets/conectado.png";
- frase = "Esperando emergencias";
- urlWS =
- new IOWebSocketChannel.connect('ws://echo.websocket.org');
- urlWS.sink.add('Hello again!');
- } else {
- // Terminar la comunicacion
- estado = "Conectarse";
- ruta = "assets/desconectado.png";
- frase = "Conéctate para recibir emergencias";
- urlWS.sink.add('Bye!');
- urlWS.sink.close();
- }
- });
- // Tell flutter that im changing the state
- },
- child: Text(estado, style: TextStyle(fontSize: 40)),
- ),
- ),
- //margin: EdgeInsets.all(80),
- margin: EdgeInsets.all(50.0),
- ),
- new StreamBuilder(
- stream: urlWS.stream,
- builder: (context, snapshot) {
- return new Padding(
- padding: const EdgeInsets.all(30.0),
- child: new Text(snapshot.hasData ? '${snapshot.data}' : ''),
- );
- },
- ),
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement