facebook twitter hatena line email

Flutter/外部ライブラリ/flutter user agent

提供: 初心者エンジニアの簡易メモ
2020年12月1日 (火) 12:52時点におけるAdmin (トーク | 投稿記録)による版 (ページの作成:「==インストール== pubspec.yaml <pre> dependencies: flutter_user_agent: ^1.2.2 </pre> ==公式== https://pub.dev/packages/flutter_user_agent ==サンプル起動=...」)

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索

インストール

pubspec.yaml

dependencies:
  flutter_user_agent: ^1.2.2

公式

https://pub.dev/packages/flutter_user_agent

サンプル起動

WebView.platform = SurfaceAndroidWebView(); in initState().

サンプル

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

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _userAgent = '<unknown>';
  String _webUserAgent = '<unknown>';

  @override
  void initState() {
    super.initState();
    initUserAgentState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initUserAgentState() async {
    String userAgent, webViewUserAgent;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      userAgent = await FlutterUserAgent.getPropertyAsync('userAgent');
      await FlutterUserAgent.init();
      webViewUserAgent = FlutterUserAgent.webViewUserAgent;
      print('''
applicationVersion => ${FlutterUserAgent.getProperty('applicationVersion')}
systemName         => ${FlutterUserAgent.getProperty('systemName')}
userAgent          => $userAgent
webViewUserAgent   => $webViewUserAgent
packageUserAgent   => ${FlutterUserAgent.getProperty('packageUserAgent')}
      ''');
    } on PlatformException {
      userAgent = webViewUserAgent = '<error>';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _userAgent = userAgent;
      _webUserAgent = webViewUserAgent;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('User agent example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('Device userAgent: $_userAgent'),
              Text('Web userAgent: $_webUserAgent')
            ],
          ),
        ),
      ),
    );
  }
}