flutter_socket 1.0.1 copy "flutter_socket: ^1.0.1" to clipboard
flutter_socket: ^1.0.1 copied to clipboard

在flutter中使用websocket的一个插件,暂时只支持安卓设备

example/lib/main.dart

import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_socket/connect_close_model.dart';
import 'package:flutter_socket/flutter_socket.dart';
import 'package:flutter_socket/flutter_socket_util.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final String _url = 'ws://192.168.199.63:8088/websocket/3/phone';

  // String _url = 'ws://192.168.199.63:8088/phone';

  final util = FlutterWebSocketUtil();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Container(
          height: MediaQueryData.fromWindow(window).size.height,
          width: MediaQueryData.fromWindow(window).size.width,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              renderBtn("初始化连接", () {
                util.connect(
                    url: _url,
                    onClose: (SocketConnectCloseModel detail) {
                      print('是否远程地址:${detail.remote}');
                      print('连接被断开code:${detail.code}');
                      print('连接被断开:${detail.message}');
                    },
                    onMessage: (String message) {
                      print('收到消息:$message');
                    },
                    onOpen: (String url) {
                      print('连接成功,地址是:$url');
                    },
                    onError: (String message) {
                      print('连接失败:$message');
                    });
              }),
              renderBtn("断开连接", util.close),
              renderBtn("开启心跳", util.openHeart),
              renderBtn("检测是否已经连接", FlutterSocket.isOpen),
              renderBtn("是否打开了GPS设置", ()async{
              final isOpen = await  FlutterSocket.gpsIsOpen();
              print('gps是否打开:$isOpen');
              }),
              renderBtn("打开gps设置页面", FlutterSocket.openGPSSystemPage)
            ],
          ),
        ),
      ),
    );
  }

  // 渲染按钮
  Widget renderBtn(String text, Function onTap) {
    return MaterialButton(
        color: Colors.blue,
        onPressed: onTap,
        child: Text(
          '$text',
          style: TextStyle(color: Colors.white),
        ));
  }
}
0
likes
135
pub points
14%
popularity

Publisher

verified publisheritbug.shop

在flutter中使用websocket的一个插件,暂时只支持安卓设备

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on flutter_socket