native_id 1.0.0
native_id: ^1.0.0 copied to clipboard
A Flutter package to retrieve unique device identifiers and comprehensive device information across different platforms.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:native_id/native_id.dart';
import 'package:native_id/legacy.dart' as old;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Native ID Demo",
theme: ThemeData(colorSchemeSeed: const Color(0x9f4376f8)),
home: const DeviceInfoPage(),
);
}
}
class DeviceInfoPage extends StatefulWidget {
const DeviceInfoPage({super.key});
@override
State<DeviceInfoPage> createState() => _DeviceInfoPageState();
}
class _DeviceInfoPageState extends State<DeviceInfoPage> {
String? _legacyDeviceId;
String? _deviceId;
DeviceInfoModel? _deviceInfo;
bool _isLoading = true;
@override
void initState() {
super.initState();
_loadDeviceInfo();
}
Future<void> _loadDeviceInfo() async {
setState(() => _isLoading = true);
final deviceId = await NativeIdService.getDeviceId();
final deviceInfo = await NativeIdService.getDeviceInfo();
// Legacy implementation
// ignore: deprecated_member_use
final legacyDeviceId = await old.NativeId.getId();
setState(() {
_deviceId = deviceId;
_legacyDeviceId = legacyDeviceId;
_deviceInfo = deviceInfo;
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Device Identifier'), elevation: 4),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: ListView(
padding: const EdgeInsets.all(16),
children: [
_buildSection('Device ID', _deviceId ?? 'N/A'),
const SizedBox(height: 8),
_buildSection('Legacy Device ID', _legacyDeviceId ?? 'N/A'),
const Divider(height: 32),
if (_deviceInfo != null) ..._buildDeviceInfoWidgets(),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _loadDeviceInfo,
tooltip: 'Refresh',
child: const Icon(Icons.refresh),
),
);
}
Widget _buildSection(String title, String value) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(12),
width: double.infinity,
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(8),
),
child: SelectableText(value, style: const TextStyle(fontSize: 14)),
),
],
);
}
List<Widget> _buildDeviceInfoWidgets() {
final info = _deviceInfo!;
final items = <Widget>[];
void addItem(String label, String? value) {
if (value != null && value.isNotEmpty) {
items.add(_buildInfoRow(label, value));
}
}
addItem('Platform', info.platform);
addItem('Model', info.model);
addItem('Manufacturer', info.manufacturer);
addItem('Brand', info.brand);
addItem('Name', info.name);
addItem('OS Version', info.osVersion);
addItem('SDK Version', info.sdkVersion);
addItem('Is Physical Device', info.isPhysicalDevice?.toString());
if (info.additionalInfo != null && info.additionalInfo!.isNotEmpty) {
items.add(const SizedBox(height: 16));
items.add(
const Text(
'Additional Information',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
);
items.add(const SizedBox(height: 8));
info.additionalInfo!.forEach((key, value) {
items.add(_buildInfoRow(key, value.toString()));
});
}
if (info.error != null) {
items.add(_buildInfoRow('Error', info.error!));
}
return items;
}
Widget _buildInfoRow(String label, String value) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 4),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
width: 150,
child: Text(
label,
style: const TextStyle(fontWeight: FontWeight.w600),
),
),
Expanded(
child: SelectableText(
value,
style: const TextStyle(color: Colors.black87),
),
),
],
),
);
}
}