Ip_Country_Lookup

一个 Flutter 包,用于根据用户的公共 IP 地址检索国家/地区信息。

特点

获取用户的公共 IP 地址。检索国家/地区信息,例如国家/地区代码、国家/地区名称和 ISP(互联网服务提供商)。

应用内截图

入门

要使用此包,请按照以下步骤操作

将包添加到您的 pubspec.yaml 文件

dependencies:
  ip_country_lookup: ^1.0.0

在您的Dart文件中导入包

import 'package:ip_country_lookup/ip_country_lookup.dart';

获取用户的公共 IP 地址

String publicIp = await IpCountryLookup().getUserIpAddress();

使用用户的 IP 地址检索国家/地区信息

IpCountryData countryData = await IpCountryLookup().getIpLocationData();

注意:请确保您拥有必要的权限和互联网连接来检索用户的 IP 地址并访问国家/地区信息。

示例

import 'package:flutter/material.dart';
import 'package:ip_country_lookup/ip_country_lookup.dart';
import 'package:ip_country_lookup/models/ip_country_data_model.dart';

void main(List<String> args) {
  runApp(const IpCountryLookupExampleApp());
}

class IpCountryLookupExampleApp extends StatelessWidget {
  const IpCountryLookupExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: MainScreen(),
    );
  }
}

class MainScreen extends StatefulWidget {
  const MainScreen({super.key});

  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  bool isDataLoaded = false;
  bool isLoading = false;
  IpCountryData? countryData;
  String? usersPublicIpAddress;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          "IP Country Lookup",
        ),
      ),
      body: SizedBox(
        width: MediaQuery.of(context).size.width,
        child: isLoading
            ? const Center(
                child: CircularProgressIndicator(),
              )
            : Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  isDataLoaded
                      ? Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 44),
                          child: Column(
                            children: [
                              Text(
                                "Country name: ${countryData!.country_name.toString()}",
                              ),
                              Text(
                                "Country code: ${countryData!.country_code.toString()}",
                              ),
                              const SizedBox(
                                height: 30,
                              ),
                              const Divider(),
                              const SizedBox(
                                height: 30,
                              ),
                              Text(
                                "Users public ip: ${countryData!.ip.toString()}",
                              ),
                              Text(
                                "Users ISP: ${countryData!.isp.toString()}",
                              ),
                              const SizedBox(
                                height: 30,
                              ),
                            ],
                          ),
                        )
                      : const SizedBox.shrink(),
                  ElevatedButton(
                    onPressed: () async {
                      setState(() {
                        isLoading = true;
                      });
                      countryData = await IpCountryLookup().getIpLocationData();
                      setState(() {
                        isLoading = false;
                        isDataLoaded = true;
                      });
                    },
                    child: const Text(
                      "Get country data from IP",
                    ),
                  ),
                ],
              ),
      ),
    );
  }
}

支持

如有任何问题、疑问或功能请求,请发送电子邮件至:[email protected]

结论

ip_country_lookup 包可让您轻松获取用户的公共 IP 地址并获取国家/地区信息,例如国家/地区代码、国家/地区名称和 ISP。它简化了根据 IP 地址识别用户位置的过程,使您能够轻松构建地理位置感知型应用程序。

GitHub

查看 Github