Flutter通信插件teliverflutter的功能使用

Flutter通信插件TeliverFlutter的功能使用

Teliver 是一个用于所有基于GPS的位置跟踪解决方案的一站式平台。

开始使用

在你的项目的 pubspec.yaml 文件中添加以下依赖:

dependencies:
  teliverflutter: ^1.0.1
Android - 配置
  1. 从 Google 地图页面获取地图密钥。
  2. 打开 AndroidManifest.xml 文件,并在 <application> 标签内粘贴以下代码(将 API_KEY_FOR_MAP 替换为你从 Google 获取的地图密钥):
<meta-data
  android:name="com.google.android.geo.API_KEY"
  android:value="API_KEY_FOR_MAP"/>

注意:如果你已经拥有地图密钥并已在清单文件中添加,或者你只想获取位置更新,可以跳过此步骤。

app/build.gradle 文件的 dependencies 下添加以下行:

implementation 'io.teliver.sdk:TeliverSdk:4.0.9'
implementation 'com.google.code.gson:gson:2.8.6'

app/build.gradle 文件的 android 下添加以下行:

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
}

注意:应用的 minSdkVersion 必须大于或等于 21。

iOS

注意:最低部署目标支持为 11。

使用/示例

首先,通过添加以下代码片段初始化我们的 SDK:

import 'package:teliverflutter/teliverflutter.dart';

final _teliverFlutter = Teliverflutter();

var result = await _teliverFlutter.initSDK("teliver_key");

接下来,设置运营商应用的位置传输。运营商应用是需要被跟踪的应用。

result = await _teliverFlutter.startTrip("Tracking_Id");

注意:这里的 Tracking_Id 是你的行程唯一标识符;基本上是你系统中的订单号或司机ID(目前 startTrip 仅适用于 Android)。

既然运营商应用已准备好进行传输,我们现在将设置消费者端以在地图上定位。

_teliverFlutter.startTracking("Tracking_Id");

注意:这里的 Tracking_Id 与之前运营商开始行程时给出的 ID 相同。

停止行程 - 使用此方法并传入跟踪 ID 在运营商端停止行程。

var result = await _teliverFlutter.stopTrip("Tracking_Id");

注意:目前 stopTrip 仅适用于 Android。

停止跟踪 - 调用此方法以在消费者端停止运营商的跟踪。

_teliverFlutter.stopTracking("Tracking_Id");

完整示例 Demo

以下是一个完整的示例代码,展示了如何使用 teliverflutter 插件来管理行程和跟踪。

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

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

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  final _teliverflutterPlugin = Teliverflutter();
  var textInput = TextEditingController();

  [@override](/user/override)
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 平台消息是异步的,因此我们在异步方法中初始化。
  Future<void> initPlatformState() async {
    var result = _teliverflutterPlugin.initSDK("YOUR_TELIVER_KEY");
    _teliverflutterPlugin.setLogVisible(true);
    //  _teliverflutterPlugin.identifyUser("Test User");

    if (kDebugMode) {
      print("CONNECTION LISTENER ::  :::::::::::: ");
      print(result);
    }
    if (!mounted) return;
    setState(() {
      //_platformVersion = platformVersion;
    });
  }

  Future<void> onStartStopTrack(bool isTrack) async {
    var result;
    if (textInput.text.trim().isNotEmpty) {
      try {
        if (isTrack) {
          result = await _teliverflutterPlugin
              .startTracking(textInput.text.toString());
          print(result);
        } else {
          result =
              _teliverflutterPlugin.stopTracking(textInput.text.toString());
        }
        if (kDebugMode) {
          print("ON START STOP RESULT MEHTOD CALLING :::: ");
          print(result);
        }
      } on Exception catch (exception) {
        exception.runtimeType;
      }
    } else {
      if (kDebugMode) print("Enter TRIP ID");
    }
  }

  Future<void> onStartStopTrip(bool isStart) async {
    if (kDebugMode) {
      print("FROM INSIDE ON START STOP TRIP ");
    }
    if (textInput.text.trim().isNotEmpty) {
      try {
        var result;
        if (isStart) {
          result =
              await _teliverflutterPlugin.startTrip(textInput.text.toString());
        } else {
          result =
              await _teliverflutterPlugin.stopTrip(textInput.text.toString());
        }
        if (kDebugMode) {
          print("ON START STOP RESULT MEHTOD CALLING :::: ");
          print(result);
        }
      } on Exception catch (exception) {
        exception.runtimeType;
      }
    } else {
      if (kDebugMode) print("Enter TRIP ID");
    }
  }

  Future<void> onStartStopMultipleTracking(bool isTrack) async {
    if (textInput.text.trim().isNotEmpty) {
      try {
        if (isTrack) {
          _teliverflutterPlugin.startMultiTracking(textInput.text.toString());
        } else {
          _teliverflutterPlugin.stopMultiTracking(textInput.text.toString());
        }
      } on Exception catch (exception) {
        exception.runtimeType;
      }
    } else {
      if (kDebugMode) print("Enter TRIP ID");
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('插件示例应用'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                margin: const EdgeInsets.all(10),
                padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5),
                  color: Colors.grey.shade300,
                ),
                child: TextField(
                  controller: textInput,
                  decoration: const InputDecoration(
                      border: InputBorder.none, hintText: "输入行程ID"),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () {
                      onStartStopTrack(true);
                    },
                    child: const Text("开始跟踪"),
                  ),
                  const SizedBox(
                    width: 15,
                  ),
                  ElevatedButton(
                    onPressed: () {
                      onStartStopTrack(false);
                    },
                    child: const Text("停止跟踪"),
                  ),
                ],
              ),
              const SizedBox(
                height: 20,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  ElevatedButton(
                    onPressed: () {
                      onStartStopTrip(true);
                    },
                    child: const Text("开始行程"),
                  ),
                  const SizedBox(
                    width: 15,
                  ),
                  ElevatedButton(
                    onPressed: () {
                      onStartStopTrip(false);
                    },
                    child: const Text("结束行程"),
                  ),
                ],
              ),
              const SizedBox(
                height: 20,
              ),
              Column(
                children: [
                  ElevatedButton(
                    onPressed: () {
                      onStartStopMultipleTracking(true);
                    },
                    child: const Text("开始多个跟踪"),
                  ),
                  const SizedBox(
                    width: 15,
                  ),
                  ElevatedButton(
                    onPressed: () {
                      onStartStopMultipleTracking(false);
                    },
                    child: const Text("停止多个跟踪"),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

更多关于Flutter通信插件teliverflutter的功能使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter通信插件teliverflutter的功能使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


teliverflutter 是一个用于在 Flutter 应用中集成 Teliver 服务的插件。Teliver 是一个实时跟踪和物流管理平台,允许开发者轻松集成实时位置跟踪、配送管理等功能到他们的应用中。

以下是 teliverflutter 插件的基本功能和使用方法:

1. 安装插件

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

dependencies:
  teliverflutter: ^1.0.0  # 请根据最新版本号进行替换

然后运行 flutter pub get 来安装插件。

2. 初始化 Teliver

在你的 Flutter 应用中初始化 Teliver 服务。通常,你可以在 main.dart 文件中的 main 函数中进行初始化:

import 'package:teliverflutter/teliverflutter.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化 Teliver
  await Teliver.init(
    apiKey: 'YOUR_API_KEY',  // 替换为你的 Teliver API 密钥
  );

  runApp(MyApp());
}

3. 开始跟踪

你可以使用 Teliver 开始跟踪一个配送订单。通常,你需要提供一个 trackingIddriverId

await Teliver.startTracking(
  trackingId: 'TRACKING_ID',  // 替换为你的订单跟踪ID
  driverId: 'DRIVER_ID',      // 替换为司机ID
);

4. 停止跟踪

当配送完成或需要停止跟踪时,你可以调用 stopTracking 方法:

await Teliver.stopTracking();

5. 监听位置更新

你可以监听司机的实时位置更新。Teliver 提供了 onLocationUpdate 回调:

Teliver.onLocationUpdate.listen((location) {
  print('Driver Location: ${location.latitude}, ${location.longitude}');
});

6. 处理事件

Teliver 还提供了其他事件,如 onTripStartedonTripEnded,你可以监听这些事件来处理特定的业务逻辑:

Teliver.onTripStarted.listen((trackingId) {
  print('Trip Started: $trackingId');
});

Teliver.onTripEnded.listen((trackingId) {
  print('Trip Ended: $trackingId');
});

7. 自定义 UI

你可以使用 Teliver 提供的地图组件来显示司机的实时位置。teliverflutter 插件提供了一个 TeliverMap 组件,你可以将其集成到你的 UI 中:

import 'package:teliverflutter/teliverflutter.dart';

class MyMapScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Driver Tracking'),
      ),
      body: TeliverMap(
        trackingId: 'TRACKING_ID',  // 替换为你的订单跟踪ID
      ),
    );
  }
}

8. 处理错误

你可以监听 onError 事件来处理可能发生的错误:

Teliver.onError.listen((error) {
  print('Error: $error');
});
回到顶部