Closed7

Flutterの情報取得パッケージを使ってみる

薄田達哉 / tatsuyasusukida薄田達哉 / tatsuyasusukida

はじめに

このスクラップでは下記の情報取得パッケージを使ってみる

https://pub.dev/packages/package_info_plus

https://pub.dev/packages/device_info_plus

https://pub.dev/packages/permission_handler

薄田達哉 / tatsuyasusukida薄田達哉 / tatsuyasusukida

package_info_plus

準備コマンド

flutter create hello_package_info
cd hello_package_info
flutter pub add package_info_plus

コード

hello_package_info/lib/main.dart
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';

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

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

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({super.key});

  final Future<PackageInfo> _info = PackageInfo.fromPlatform();

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Demo Home Page'),
      ),
      body: FutureBuilder(
        future: _info,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: CircularProgressIndicator());
          } else {
            final info = snapshot.data!;
            return Column(
              children: [
                ListTile(title: const Text("App name"), subtitle: Text(info.appName)),
                ListTile(title: const Text("Package name"), subtitle: Text(info.packageName)),
                ListTile(title: const Text("Version"), subtitle: Text(info.version)),
                ListTile(title: const Text("Build number"), subtitle: Text(info.buildNumber)),
                ListTile(title: const Text("Build signature"), subtitle: Text(info.buildSignature)),
              ],
            );
          }
        },
      ),
    );
  }
}

実行コマンド

Android 実機で実行する

flutter run

実行結果

薄田達哉 / tatsuyasusukida薄田達哉 / tatsuyasusukida

device_info_plus

準備コマンド

flutter create hello_device_info
cd hello_device_info
flutter pub add device_info_plus

コード

hello_device_info/lib/main.dart
import 'dart:io';

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

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

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

  
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

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

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Demo Home Page'),
      ),
      body: FutureBuilder(
        future: DeviceInfoPlugin().androidInfo,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: CircularProgressIndicator());
          } else {
            final info = snapshot.data!;
            return Column(
              children: [
                ListTile(title: const Text("Is Android"), subtitle: Text(Platform.isAndroid.toString())),
                ListTile(title: const Text("Brand"), subtitle: Text(info.brand)),
                ListTile(title: const Text("Hardware"), subtitle: Text(info.hardware)),
                ListTile(title: const Text("Manufacturer"), subtitle: Text(info.manufacturer)),
                ListTile(title: const Text("Model"), subtitle: Text(info.model)),
                ListTile(title: const Text("Product"), subtitle: Text(info.product)),
                ListTile(title: const Text("Is Physical Device"), subtitle: Text(info.isPhysicalDevice.toString())),
                ListTile(title: const Text("Width Pixel"), subtitle: Text(info.displayMetrics.widthPx.toString())),
                ListTile(title: const Text("Height Pixel"), subtitle: Text(info.displayMetrics.heightPx.toString())),
              ],
            );
          }
        },
      ),
    );
  }
}

実行コマンド

Android 実機で実行する

flutter run

実行結果

薄田達哉 / tatsuyasusukida薄田達哉 / tatsuyasusukida

permission_handler

準備コマンド

flutter create hello_permission_handler
cd hello_permission_handler
flutter pub add permission_handler

コード

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

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

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

  
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class PermissionResult {
  final Permission permission;
  final PermissionStatus status;

  const PermissionResult(this.permission, this.status);
}

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

  
  Widget build(BuildContext context) {
    final permissions = [
      Permission.camera,
      Permission.storage,
      Permission.photos,
      Permission.calendar,
      Permission.audio,
      Permission.location,
    ];

    final futures = permissions.map((permission) async {
      final status = await permission.status;
      return PermissionResult(permission, status);
    });

    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter Demo Home Page'),
      ),
      body: FutureBuilder(
        future: Future.wait(futures),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: CircularProgressIndicator());
          } else {
            final results = snapshot.data!;
            return Column(
              children: [
                for (final result in results)
                  ListTile(
                    title: Text(result.permission.toString()),
                    subtitle: Text(result.status.toString()),
                  ),
              ],
            );
          }
        },
      ),
    );
  }
}

実行コマンド

Android 実機で実行する

flutter run

実行結果

このスクラップは2023/01/10にクローズされました