flutter_ip 0.2.0 copy "flutter_ip: ^0.2.0" to clipboard
flutter_ip: ^0.2.0 copied to clipboard

Flutter plugin - Get External IP, Internal IP address for wifi, cellular and wired.

example/lib/main.dart

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

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

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

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

class _MyAppState extends State<MyApp> {
  String _internalIP = 'Unknown';
  String _externalIP = 'Unknown';
  String _networkType = "Unknown";
  Color _typeColor = Colors.yellow;

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

  Future<void> _updateInternalIP() async {
    String ip;
    try {
      ip = await FlutterIp.internalIP;
    } on PlatformException {
      ip = 'Failed to get ip.';
    }
    if (!mounted) return;

    setState(() {
      _internalIP = ip;
    });
  }

  Future<void> _updateExternalIP() async {
    String ip;
    try {
      ip = await FlutterIp.externalIP;
    } on PlatformException {
      ip = 'Failed to get ip.';
    }
    if (!mounted) return;

    setState(() {
      _externalIP = ip;
    });
  }

  Future<void> _updateNetworkType() async {
    String networkType;
    try {
      networkType = await FlutterIp.networkType;
    } on PlatformException {
      networkType = 'Failed to get network type.';
    }
    if (!mounted) return;

    setState(() {
      _networkType = networkType;
      switch (_networkType) {
        case 'wired':
          {
            _typeColor = Colors.yellow;
            break;
          }
        case 'wifi':
          {
            _typeColor = Colors.red.withOpacity(0.5);
            break;
          }
        case 'cellular':
          {
            _typeColor = Colors.lightBlue.withOpacity(0.5);
            break;
          }
        default:
          {
            _typeColor = Colors.transparent;
            break;
          }
      }
    });
  }

  _updateNetworkInformation() {
    _updateInternalIP();
    _updateExternalIP();
    _updateNetworkType();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black.withOpacity(0.7),
          title: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('Flutter Plugin',
                  style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.w400,
                      letterSpacing: 1.0)),
              SizedBox(
                width: 5,
              ),
              Text('flutter_ip',
                  style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.w200,
                      letterSpacing: 2.0)),
            ],
          ),
        ),
        body: Center(
            child: InkWell(
          onTap: _updateNetworkInformation,
          child: Container(
            padding: const EdgeInsets.all(40.0),
            width: double.infinity,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  'Internal IP',
                  style: titleStyle,
                ),
                Padding(
                  padding: const EdgeInsets.only(bottom: 20.0, top: 5),
                  child: Text(
                    '$_internalIP\n',
                    style: resultStyle,
                  ),
                ),
                Text(
                  'External IP',
                  style: titleStyle,
                ),
                Padding(
                  padding: const EdgeInsets.only(bottom: 20.0, top: 5),
                  child: Text(
                    '$_externalIP\n',
                    style: resultStyle,
                  ),
                ),
                Text(
                  'Network',
                  style: titleStyle,
                ),
                Padding(
                  padding: const EdgeInsets.only(bottom: 20.0, top: 5),
                  child: Text(
                    '$_networkType\n',
                    style: resultStyle.copyWith(backgroundColor: _typeColor),
                  ),
                ),
              ],
            ),
          ),
        )),
      ),
    );
  }
}

TextStyle titleStyle = TextStyle(
  color: Colors.black.withOpacity(0.6),
  fontSize: 18,
  fontWeight: FontWeight.w200,
  letterSpacing: 0.5,
);
TextStyle resultStyle = TextStyle(
  color: Colors.black,
  fontSize: 30,
);
13
likes
40
pub points
75%
popularity

Publisher

unverified uploader

Flutter plugin - Get External IP, Internal IP address for wifi, cellular and wired.

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_ip