getUserAgent method

Future<String> getUserAgent()

Implementation

Future<String> getUserAgent() async {
  String userAgent = 'Mozilla/5.0 ';
  DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

  if (Platform.isAndroid) {
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;

    bool isTablet;

    final double devicePixelRatio = ui.window.devicePixelRatio;
    final ui.Size size = ui.window.physicalSize;
    final double width = size.width;
    final double height = size.height;


    if(devicePixelRatio < 2 && (width >= 1000 || height >= 1000)) {
      isTablet = true;
    }
    else if(devicePixelRatio == 2 && (width >= 1920 || height >= 1920)) {
      isTablet = true;
    }
    else {
      isTablet = false;
    }


    userAgent += '(Linux; Android ${androidInfo.version.release}; ${isTablet ? 'Tablet' : 'Mobile'}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Mobile Safari/537.36';
  } else if (Platform.isIOS) {
    IosDeviceInfo iosInfo = await deviceInfo.iosInfo;
    String deviceName = iosInfo.name;
    String model = iosInfo.model;
    String iosVersion = iosInfo.systemVersion.replaceAll('.', '_');

    userAgent += '($deviceName - $model CPU iOS $iosVersion like Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/537.36';
  }

  return userAgent;
}