wifip2p 0.0.1 copy "wifip2p: ^0.0.1" to clipboard
wifip2p: ^0.0.1 copied to clipboard

PlatformAndroid

A new flutter plugin project.

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:wifip2p/wifip2p.dart';

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

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

class _MyAppState extends State<MyApp> {
  bool _currentWifiStatus = false;
  List<String> dummyList = [""];
  String errorString = ">>>>>>>>>>>>>> PlatformException occurs <<<<<<<<<<<<<<";
  TextEditingController sendTextController = TextEditingController();
  String sendText = "";

  @override
  void initState() {
    super.initState();
  }

  Future<void> connectToDevice(int position) async {
    try {
      await WifiP2P.connectToDevice(position);
    } on PlatformException {
      print(errorString);
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Wifi Control App'),
        ),
        body: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    SizedBox(height: 10.0),
                    StreamBuilder(
                        stream: WifiP2P.connectionChangeEvent,
                        builder: (BuildContext context,
                            AsyncSnapshot<bool> snapshot) {
                          if (snapshot.hasData) {
                            return Text(
                                "Connection Status: ${snapshot.data! ? "Connected" : "Disconnected"}",
                                style: TextStyle(fontSize: 22.0));
                          } else {
                            return Text("Connection Status: Unknown",
                                style: TextStyle(fontSize: 22.0));
                          }
                        }),
                    StreamBuilder(
                        stream: WifiP2P.receiveData,
                        builder: (BuildContext context,
                            AsyncSnapshot<String> snapshot) {
                          return Text(
                              snapshot.hasData ? snapshot.data! : "Message");
                        }),
                    SizedBox(height: 40.0),
                    StreamBuilder(
                      stream: WifiP2P.wifiStateChangeEvent,
                      builder: (BuildContext context, AsyncSnapshot snapshot) {
                        if (snapshot.hasData) {
                          return Text(
                              "WIFI STATUS IS ${(snapshot.data!) ? "ON" : "OFF"}",
                              style: TextStyle(fontSize: 20.0));
                        } else {
                          return Text("WIFI STATUS IS UNKNOWN");
                        }
                      },
                    ),
                    ElevatedButton(
                      child: Text("WIFI ${_currentWifiStatus ? "ON" : "OFF"}"),
                      onPressed: () async {
                        bool currentWifiStatus = false;
                        print("wifi Button is Pressed");
                        try {
                          currentWifiStatus = await WifiP2P.toggleWifiState();
                        } on PlatformException {
                          print(errorString);
                        }
                        setState(() {
                          _currentWifiStatus = currentWifiStatus;
                        });
                      },
                    ),
                    ElevatedButton(
                      child: Text("Discover Devices"),
                      onPressed: () async {
                        try {
                          await WifiP2P.discoverDevices();
                        } on PlatformException {
                          print(errorString);
                        }
                      },
                    ),
                    StreamBuilder(
                        stream: WifiP2P.peerListChangeEvent,
                        builder: (BuildContext context,
                            AsyncSnapshot<List<String>> snapshot) {
                          if (snapshot.hasData) {
                            List<TextButton> list = [];
                            for (int i = 0; i < snapshot.data!.length; i++) {
                              list.add(TextButton(
                                  onPressed: () {
                                    connectToDevice(i);
                                  },
                                  child: Text(snapshot.data![i])));
                            }
                            return Column(children: list);
                          }
                          return Text("No Name");
                        }),
                    Padding(
                        padding: EdgeInsets.only(top: 20.0),
                        child: Text(sendText)),
                  ],
                ),
              ),
            ),
            Row(
              children: <Widget>[
                Expanded(
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: 10.0),
                    child: Container(
                      padding: EdgeInsets.symmetric(horizontal: 20.0),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(25.0),
                        border: Border.all(color: Colors.blue),
                        boxShadow: [
                          BoxShadow(
                            color: Colors.blue.withAlpha(50),
                            offset: Offset(0, 10),
                            blurRadius: 10.0,
                            spreadRadius: -5.0,
                          ),
                          BoxShadow(
                            color: Colors.blue.withAlpha(5),
                            offset: Offset(-10, -10),
                            blurRadius: 10.0,
                            spreadRadius: -5.0,
                          ),
                        ],
                      ),
                      child: TextField(
                        style: TextStyle(decoration: TextDecoration.none),
                        controller: sendTextController,
                        cursorHeight: 25.0,
                        decoration: InputDecoration(border: InputBorder.none),
                      ),
                    ),
                  ),
                ),
                GestureDetector(
                  onTap: () async {
                    sendText = sendTextController.text;
                    setState(() {});
                    try {
                      await WifiP2P.sendData(sendText);
                    } on PlatformException {
                      print(errorString);
                    }
                  },
                  child: Container(
                    margin: EdgeInsets.only(right: 10.0),
                    height: 55.0,
                    width: 55.0,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(25.0),
                      border: Border.all(color: Colors.blue),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.blue.withAlpha(100),
                          offset: Offset(0, 10),
                          blurRadius: 10.0,
                          spreadRadius: -5.0,
                        ),
                        BoxShadow(
                          color: Colors.blue.withAlpha(5),
                          offset: Offset(-10, -10),
                          blurRadius: 10.0,
                          spreadRadius: -5.0,
                        ),
                      ],
                    ),
                    child: Icon(Icons.send),
                  ),
                )
              ],
            ),
            SizedBox(height: 20.0),
          ],
        ),
      ),
    );
  }
}
3
likes
110
pub points
44%
popularity

Publisher

unverified uploader

A new flutter plugin project.

Homepage

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on wifip2p