flutter_socket_plugin 2.0.0 copy "flutter_socket_plugin: ^2.0.0" to clipboard
flutter_socket_plugin: ^2.0.0 copied to clipboard

PlatformAndroidiOS
outdated

FlutterSocket is a practical cross-platform socket plugin,supports both iOS and Android.

example/lib/main.dart

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.10.63";
  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>[
          Expanded(child: ListView(children: [
            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>[
                  ElevatedButton(
                    style: ButtonStyle(
                      backgroundColor: ButtonStyleButton.allOrNull<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)),
                  ElevatedButton(
                    style: ButtonStyle(
                      backgroundColor: ButtonStyleButton.allOrNull<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)),
                  ElevatedButton(
                    style: ButtonStyle(
                      backgroundColor: ButtonStyleButton.allOrNull<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: ElevatedButton(
                  style: ButtonStyle(
                    backgroundColor: ButtonStyleButton.allOrNull<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: ElevatedButton(
                  style: ButtonStyle(
                    backgroundColor: ButtonStyleButton.allOrNull<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: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(child: Text(receiveMessage,style: TextStyle(fontSize: 15,color: Colors.lightBlueAccent,fontWeight: FontWeight.w500),),)
                  ],
                ),
              ),
            ),
          ],),),
        ],
      ),
    );
  }

}
16
likes
150
points
33
downloads

Publisher

unverified uploader

Weekly Downloads

FlutterSocket is a practical cross-platform socket plugin,supports both iOS and Android.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on flutter_socket_plugin