Flutter网络延迟测试插件ping_view_widget的使用

Flutter网络延迟测试插件ping_view_widget的使用

PingViewWidget

PingViewWidget 是一个简单且可自定义的动画连接按钮小部件。
项目源码完全由 Dart 编写。

Motivation

我在开发 Flutter 应用时需要一些干净的动画 3D 视图来展示网络延迟。


开始使用

安装

将此库添加到项目的 pubspec.yaml 文件中:

dependencies:
  ping_view_widget: ^1.0.0

然后运行以下命令以安装依赖:

flutter pub get

使用方法

导入库后,可以直接在 Widget 树中使用该视图。

导入库

import 'package:ping_view_widget/ping_view_widget.dart';

使用示例

PingViewWidget(
  ispInformationText: TextSpan(
    style: TextStyle(
      color: Colors.black,
      fontSize: 12,
    ),
    children: [
      TextSpan(
        text: "LOREM SERVER\n",
        style: TextStyle(
          color: Colors.black,
          fontWeight: FontWeight.w500,
        ),
      ),
      TextSpan(
          text: "São Paulo, Brasil",
          style: TextStyle(color: Colors.grey[600])),
    ],
  ),
  locationInformatinText: TextSpan(
    style: TextStyle(
        color: Colors.grey[600],
        fontSize: 10,
        fontWeight: FontWeight.w500),
    children: [
      TextSpan(text: "IP Interno: 198.162.1.8\n"),
      TextSpan(text: "IP Externo: 198.162.1.7\n"),
      TextSpan(
        text: "Operadora: Jio",
      ),
    ],
  ),
  techInformationText: TextSpan(
    text: "LTE",
    style: TextStyle(color: Color(0xFF3ebdb8), fontSize: 11),
  ),
)

自定义选项

您可以根据需要调整 UI。以下是可用的自定义属性:

属性名称 类型 描述
ispInformationText TextSpan 用于自定义 ISP 服务器信息的 TextSpan 小部件
locationInformatinText TextSpan 用于自定义 ping 结束位置信息的 TextSpan 小部件
techInformationText TextSpan 用于自定义所用技术信息的 TextSpan 小部件

截图



示例代码

以下是一个完整的示例代码,展示如何在 Flutter 中使用 PingViewWidget

// example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:ping_view_widget/ping_view_widget.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: MyHomePage(title: 'Ping View Widget Demo'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
          height: 240,
          child: PingViewWidget(
            ispInformationText: TextSpan(
              style: TextStyle(color: Colors.black, fontSize: 12),
              children: [
                TextSpan(
                  text: "LOREM SERVER\n",
                  style: TextStyle(color: Colors.black, fontWeight: FontWeight.w500),
                ),
                TextSpan(
                    text: "São Paulo, Brasil",
                    style: TextStyle(color: Colors.grey[600])),
              ],
            ),
            locationInformatinText: TextSpan(
              style: TextStyle(color: Colors.grey[600], fontSize: 10, fontWeight: FontWeight.w500),
              children: [
                TextSpan(text: "IP Interno: 198.162.1.8\n"),
                TextSpan(text: "IP Externo: 198.162.1.7\n"),
                TextSpan(text: "Operadora: Jio"),
              ],
            ),
            techInformationText: TextSpan(
              text: "LTE",
              style: TextStyle(color: Color(0xFF3ebdb8), fontSize: 11),
            ),
          ),
        ),
      ),
    );
  }
}

更多关于Flutter网络延迟测试插件ping_view_widget的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter网络延迟测试插件ping_view_widget的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


ping_view_widget 是一个 Flutter 插件,用于在应用程序中测试网络延迟(Ping)。它提供了一个简单易用的界面,允许用户在应用中直接进行网络延迟测试,而无需离开应用。

安装

首先,你需要在 pubspec.yaml 文件中添加 ping_view_widget 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  ping_view_widget: ^0.0.1  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

使用

  1. 导入插件

    在你的 Dart 文件中导入 ping_view_widget

    import 'package:ping_view_widget/ping_view_widget.dart';
    
  2. 添加 PingViewWidget 到你的 UI

    你可以在你的 UI 中添加 PingViewWidget,并配置相关参数:

    class MyHomePage extends StatelessWidget {
      [@override](/user/override)
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Ping Test'),
          ),
          body: Center(
            child: PingViewWidget(
              host: 'www.google.com', // 要 ping 的主机地址
              count: 5, // 发送的 ping 请求数量
              interval: Duration(seconds: 1), // 每次 ping 之间的间隔
              timeout: Duration(seconds: 5), // 每次 ping 的超时时间
              onPingResult: (PingResult result) {
                // 处理 ping 结果
                print('Ping Result: ${result.toString()}');
              },
            ),
          ),
        );
      }
    }
    
  3. 处理 Ping 结果

    PingViewWidget 提供了一个 onPingResult 回调,你可以在其中处理每次 ping 的结果。PingResult 包含了以下信息:

    • host: 被 ping 的主机地址。
    • sequence: 当前 ping 的序列号。
    • time: ping 的响应时间。
    • status: ping 的状态(成功、超时等)。
    onPingResult: (PingResult result) {
      if (result.status == PingStatus.success) {
        print('Ping to ${result.host} succeeded in ${result.time}ms');
      } else {
        print('Ping to ${result.host} failed');
      }
    },
    
  4. 自定义 UI

    你可以通过 PingViewWidgetbuilder 参数来自定义显示 ping 结果的 UI:

    PingViewWidget(
      host: 'www.google.com',
      count: 5,
      interval: Duration(seconds: 1),
      timeout: Duration(seconds: 5),
      builder: (BuildContext context, PingResult result) {
        return Card(
          child: ListTile(
            title: Text('Ping to ${result.host}'),
            subtitle: Text('Status: ${result.status}, Time: ${result.time}ms'),
          ),
        );
      },
    ),
    

注意事项

  • ping_view_widget 依赖于平台的底层网络功能,因此在某些平台上可能会有不同的表现。
  • 确保你拥有网络权限,特别是在 Android 和 iOS 上。

示例

以下是一个完整的示例,展示如何在 Flutter 应用中使用 ping_view_widget

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Ping Test',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Ping Test'),
      ),
      body: Center(
        child: PingViewWidget(
          host: 'www.google.com',
          count: 5,
          interval: Duration(seconds: 1),
          timeout: Duration(seconds: 5),
          onPingResult: (PingResult result) {
            print('Ping Result: ${result.toString()}');
          },
        ),
      ),
    );
  }
}
回到顶部