欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解如何在Flutter中獲取設備標識符

 更新時間:2022年04月11日 09:15:06   作者:大前端之旅  
這篇文章主要為大家介紹了幾種通過Flutter讀取設備信息的方法,文中的示例代碼講解詳細,感興趣的小伙伴快跟隨小編一起學習一下

本文將引導您完成 2 個示例,演示如何在 Flutter 中獲取設備標識符

使用 platform_device_id

如果您只需要運行應用程序的設備的 id,最簡單快捷的解決方案是使用platform_device_id包。它適用于 Android (AndroidId)、iOS (IdentifierForVendor)、Windows (BIOS UUID)、macOS (IOPlatformUUID) 和 Linux (BIOS UUID)。在 Flutter Web 應用程序中,您將獲得 UserAgent(此信息不是唯一的)。

應用預覽

我們要構建的示例應用程序包含一個浮動按鈕。按下此按鈕時,設備的 ID 將顯示在屏幕上。以下是它在 iOS 和 Android 上的工作方式:

代碼

1.通過運行安裝插件:

flutter pub add platform_device_id

然后執(zhí)行這個命令:

flutter pub get

不需要特殊權限或配置。

2.完整代碼:

// main.dart
import 'package:flutter/material.dart';
import 'package:platform_device_id/platform_device_id.dart';
?
void main() {
  runApp(const MyApp());
}
?
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: '大前端之旅',
        theme: ThemeData(
          primarySwatch: Colors.indigo,
        ),
        home: const HomePage());
  }
}
?
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);
?
  @override
  _HomePageState createState() => _HomePageState();
}
?
class _HomePageState extends State<HomePage> {
  String? _id;
?
  // This function will be called when the floating button is pressed
  void _getInfo() async {
    // Get device id
    String? result = await PlatformDeviceId.getDeviceId;
?
    // Update the UI
    setState(() {
      _id = result;
    });
  }
?
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('大前端之旅')),
      body: Padding(
        padding: const EdgeInsets.all(20),
        child: Center(
            child: Text(
          _id ?? 'Press the button',
          style: TextStyle(fontSize: 20, color: Colors.red.shade900),
        )),
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: _getInfo, child: const Icon(Icons.play_arrow)),
    );
  }
}

使用 device_info_plus

device_info_plus為您提供作為 platform_device_id 的設備 ID,并提供有關設備的其他詳細信息(品牌、型號等)以及 Flutter 應用運行的 Android 或 iOS 版本。

應用預覽

我們將制作的應用程序與上一個示例中的應用程序非常相似。但是,這一次我們將在屏幕上顯示大量文本。返回的結果因平臺而異。如您所見,Android 上返回的信息量遠遠超過 iOS。

代碼

1. 通過執(zhí)行以下操作安裝插件:

flutter pub add device_info_plus

然后運行:

flutter pub get

2. main.dart中的完整源代碼:

// main.dart
import 'package:flutter/material.dart';
import 'package:device_info_plus/device_info_plus.dart';
?
void main() {
  runApp(const MyApp());
}
?
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        // Remove the debug banner
        debugShowCheckedModeBanner: false,
        title: '大前端之旅',
        theme: ThemeData(
          primarySwatch: Colors.amber,
        ),
        home: const HomePage());
  }
}
?
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);
?
  @override
  _HomePageState createState() => _HomePageState();
}
?
class _HomePageState extends State<HomePage> {
  Map? _info;
?
  // This function is triggered when the floating button gets pressed
  void _getInfo() async {
    // Instantiating the plugin
    final deviceInfoPlugin = DeviceInfoPlugin();
?
    final result = await deviceInfoPlugin.deviceInfo;
    setState(() {
      _info = result.toMap();
    });
  }
?
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('大前端之旅')),
      body: _info != null
          ? Padding(
              padding: const EdgeInsets.all(20),
              child: ListView(
                children: _info!.entries
                    .map((e) => Wrap(
                          children: [
                            Text(
                              "${e.key} :",
                              style: const TextStyle(
                                  fontSize: 18, color: Colors.red),
                            ),
                            const SizedBox(
                              width: 15,
                            ),
                            Text(
                              e.value.toString(),
                              style: const TextStyle(
                                fontSize: 18,
                              ),
                            )
                          ],
                        ))
                    .toList(),
              ),
            )
          : const Center(
              child: Text('Press the button'),
            ),
      floatingActionButton: FloatingActionButton(
        onPressed: _getInfo,
        child: const Icon(Icons.info),
      ),
    );
  }
}

結論

我們已經(jīng)介紹了幾種讀取設備信息的技術。選擇一個適合您在項目中實施的需求。

以上就是詳解如何在Flutter中獲取設備標識符的詳細內容,更多關于Flutter獲取設備標識符的資料請關注腳本之家其它相關文章!

相關文章

最新評論