flutter_socket_plugin 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 72

Language: English | 中文简体

FlutterSocket #

build status Pub support

FlutterSocket is a practical cross-platform socket plugin. At present, it has realized the basic functions of client side: create, connect, send, receive, disconnect, heartbeat and so on.

Demo #

Demo

Add dependency #

dependencies:
  flutter_socket_plugin: lastest version  

Simple to use #

import 'package:flutter_socket_plugin/flutter_socket_plugin.dart';
  ///
  /// @Method: initSocket
  /// @Parameter:
  /// @ReturnType:
  /// @Description: init socket
  /// @author: waitwalker
  /// @Date: 2019-08-23
  ///
  initSocket() {
    
    /// init socket
    flutterSocket = FlutterSocket();

    /// listen connect callback
    flutterSocket.connectListener((data){
      print("connect listener data:$data");
    });

    /// listen error callback
    flutterSocket.errorListener((data){
      print("error listener data:$data");
    });

    /// listen receive callback
    flutterSocket.receiveListener((data){
      print("receive listener data:$data");
      if (data != null) {
        receiveMessage = receiveMessage + "\n" + data;
      }
      setState(() {

      });
    });

    /// listen disconnect callback
    flutterSocket.disconnectListener((data){
      print("disconnect listener data:$data");
    });

  }

Steps #

Create socket :

await flutterSocket.createSocket("192.168.8.120", 10007, timeout: 20);

Connect connect :

flutterSocket.tryConnect();

Send message send :

flutterSocket.send(textEditingController.text);

Disconnect disconnect :

flutterSocket.tryDisconnect();

Examples #

you can find all examples code here.

Future #

FlutterSocket currently implements the sending and receiving of basic String messages on the client. In the future, it will gradually improve its functions if conditions permit. If you need some functions urgently, you can continue to add functions on the basis of FlutterSocket to make the function of FlutterSocket more powerful and perfect.

This project is completely open source and the license is MIT. If you like, welcome star.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Flutter Technology and Other Communication Groups #

wechat:dbzy_duzhao

Scan QR code👇:

0.0.1 #

Completion of basic plugin functions

0.0.2 #

Update some documents

0.0.3 #

Update some documents

0.0.4 #

Update group

1.0.0 #

Release version

example/README.md

flutter_socket_plugin_example #

Demonstrates how to use the flutter_socket_plugin plugin.

Getting Started #

import 'package:flutter/material.dart';
import 'package:flutter_socket_plugin/flutter_socket_plugin.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "home",
      home: HomePage(),
      theme: ThemeData(primaryColor: Colors.lightGreen),
    );
  }
}


class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  TextEditingController textEditingController = TextEditingController();

  FlutterSocket flutterSocket;
  bool connected = false;
  String _host = "192.168.8.82";
  int _port = 10007;
  String receiveMessage = "";

  @override
  void initState() {
    initSocket();
    textEditingController.addListener((){
      print("input text:${textEditingController.text}");
    });
    super.initState();
  }

  ///
  /// @Method: initSocket
  /// @Parameter:
  /// @ReturnType:
  /// @Description: init socket
  /// @author: waitwalker
  /// @Date: 2019-08-23
  ///
  initSocket() {

    /// init socket
    flutterSocket = FlutterSocket();

    /// listen connect callback
    flutterSocket.connectListener((data){
      print("connect listener data:$data");
    });

    /// listen error callback
    flutterSocket.errorListener((data){
      print("error listener data:$data");
    });

    /// listen receive callback
    flutterSocket.receiveListener((data){
      print("receive listener data:$data");
      if (data != null) {
        receiveMessage = receiveMessage + "\n" + data;
      }
      setState(() {

      });
    });

    /// listen disconnect callback
    flutterSocket.disconnectListener((data){
      print("disconnect listener data:$data");
    });

  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("FlutterSocket",style: TextStyle(fontSize: 18,color: Colors.white),),
        backgroundColor: Colors.lightBlue,
      ),
      body: Column(
        children: <Widget>[

          Padding(
            padding: EdgeInsets.only(top: 30,left: 20,right: 20),
            child: Row(
              children: <Widget>[
                Text("Host:",style: TextStyle(fontSize: 22,color: Colors.lightBlueAccent,fontWeight: FontWeight.w600),),
                Padding(padding: EdgeInsets.only(left: 10),),
                Container(
                  decoration: BoxDecoration(
                    border: Border(bottom: BorderSide(color: Colors.redAccent,width: 1.0)),
                  ),
                  child: Text(_host,style: TextStyle(fontSize: 18,color: Colors.black87,fontWeight: FontWeight.w300),),
                ),
                Padding(padding: EdgeInsets.only(left: 30),),
                Text("Port:",style: TextStyle(fontSize: 22,color: Colors.lightBlueAccent,fontWeight: FontWeight.w600),),
                Padding(padding: EdgeInsets.only(left: 10),),
                Container(
                  decoration: BoxDecoration(
                    border: Border(bottom: BorderSide(color: Colors.redAccent,width: 1.0)),
                  ),
                  child: Text("$_port",style: TextStyle(fontSize: 18,color: Colors.black87,fontWeight: FontWeight.w300),),
                ),
              ],
            ),
          ),

          Padding(
            padding: EdgeInsets.only(left: 20,right: 20,top: 30),
            child: Row(
              children: <Widget>[
                RaisedButton(
                  color: Colors.lightBlueAccent,
                  child: Text("Create",style: TextStyle(fontSize: 18,color: Colors.white,fontWeight: FontWeight.w500),),
                  onPressed: () async {
                    await flutterSocket.createSocket(_host, _port, timeout: 20);
                  },
                ),
                Padding(padding: EdgeInsets.only(left: 20)),
                RaisedButton(
                  color: Colors.lightBlueAccent,
                  child: Text("Connect",style: TextStyle(fontSize: 18,color: Colors.white,fontWeight: FontWeight.w500),),
                  onPressed: (){
                    flutterSocket.tryConnect();
                  },
                ),
                Padding(padding: EdgeInsets.only(left: 20)),
                RaisedButton(
                  color: Colors.lightBlueAccent,
                  child: Text("Disconnect",style: TextStyle(fontSize: 18,color: Colors.white,fontWeight: FontWeight.w500),),
                  onPressed: (){
                    flutterSocket.tryDisconnect();
                  },
                ),
              ],
            ),
          ),

          Padding(
            padding: EdgeInsets.only(left: 20,right: 20,top: 30,),
            child: Container(
              height: 150,
              decoration: BoxDecoration(
                border: Border.all(color: Colors.lightBlueAccent,width: 1.0)
              ),
              child: TextField(
                controller: textEditingController,
                decoration: InputDecoration(
                  labelText: "Input content",
                  border: InputBorder.none
                ),
              ),
            ),
          ),

          Padding(
            padding: EdgeInsets.only(left: 20,right: 20,top: 10),
            child: Container(
              alignment: Alignment.centerLeft,
              child: RaisedButton(
                color: Colors.lightBlueAccent,
                child: Text("Send",style: TextStyle(fontSize: 18,color: Colors.white,fontWeight: FontWeight.w500),),
                onPressed: (){
                  flutterSocket.send(textEditingController.text);
                },),
            ),
          ),

          Padding(
            padding: EdgeInsets.only(left: 20,right: 20,top: 30),
            child: Container(
              alignment: Alignment.centerLeft,
              child: RaisedButton(
                color: Colors.lightBlueAccent,
                child: Text("Receive Data",style: TextStyle(fontSize: 18,color: Colors.white,fontWeight: FontWeight.w500),),
                onPressed: (){},),
            ),
          ),

          Padding(
            padding: EdgeInsets.only(left: 20,right: 20,top: 10),
            child: Container(
              height: 150,
              width: MediaQuery.of(context).size.width - 40,
              decoration: BoxDecoration(
                  border: Border.all(color: Colors.lightBlueAccent,width: 1.0)
              ),
              child: Text(receiveMessage,style: TextStyle(fontSize: 15,color: Colors.lightBlueAccent,fontWeight: FontWeight.w500),),
            ),
          ),
        ],
      ),
    );
  }

}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_socket_plugin: ^1.0.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_socket_plugin/flutter_socket_plugin.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
45
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
72
Learn more about scoring.

We analyzed this package on Oct 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/flutter_socket.dart.

Run flutter format to format lib/flutter_socket.dart.

Format lib/flutter_socket_plugin.dart.

Run flutter format to format lib/flutter_socket_plugin.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test