0

Is there a way to force an Flutter Android App to behave like it is on an iOS Device?

I'm not referring to the Cupertino package.

Here is what I mean with Android/iOS Style

iOS Style:

ios

Android style:

Android Style

Edric
  • 24,639
  • 13
  • 81
  • 91
Dario Digregorio
  • 657
  • 6
  • 15

4 Answers4

9

You can set the platform parameter in the ThemeData of the app to TargetPlatform.iOS. Then the app will behave as if it were running on an iOS device, including showing iOS text controls, etc. Very useful for testing things out at least!

Here's a full app that will use iOS stylings even when run on an Android device:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        // The line below forces the theme to iOS.
        platform: TargetPlatform.iOS,
      ),
      home: Scaffold(
        body: Center(
          child: TextField(),
        ),
      ),
    ),
  );
}

Edit

If you also want to fake defaultTargetPlatform, you can do that by setting debugDefaultTargetPlatform. Including that as well we have:

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


void main() {
  // This line will fake defaultTargetPlatform to iOS.
  debugDefaultTargetPlatform = TargetPlatform.iOS;

  runApp(
    MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        // The line below forces the theme to iOS.
        platform: TargetPlatform.iOS,
      ),
      home: Scaffold(
        body: Center(
          child: TextField(),
        ),
      ),
    ),
  );
}
Justin McCandless
  • 621
  • 1
  • 10
  • 20
5

Update

This is not valid anymore with latest Flutter version

Solution from Previous Versions

Yes, it is possible during development stage, if that is what you want.

If the App is started from Intellij/Android studio, open Flutter Performance tab from Intellij/Android Studio, there you can find the option Platform and you can toggle it between iOS and Android.

If you are launching app from command line and using flutter devtools, in Devtools Web Interface, go to tab Flutter Inspector, there you can find the option Platform and you can toggle it between iOS and Android.

Chenna Reddy
  • 2,241
  • 18
  • 17
  • Thank you this is exactly what I was looking for. – Dario Digregorio Sep 22 '19 at 13:43
  • @ChennaReddy I cannot see "Platform" in "Flutter Performance" of "Android 4.1". – S.N Nov 03 '20 at 21:57
  • @DarioDigregorio I cannot see "Platform" in "Flutter Performance" of "Android 4.1". – S.N Nov 03 '20 at 21:57
  • 2
    @TonyBrand it seems like the button to choose the target platform has been removed from Android Studio. But there should be still the possibility to set the platform in the property. See the Answer of Justin McCandless. – Dario Digregorio Nov 05 '20 at 14:31
2

Use debugDefaultTargetPlatformOverride to force it, obviously in debug:


Widget build(BuildContext context) {
    debugDefaultTargetPlatformOverride = TargetPlatform.iOS;

    return PlatformApp(
      title: 'Lovey-Dovey',
      home: MyHomePage(title: 'Lovey-Dovey2'),
    );
  }
j4hangir
  • 2,532
  • 1
  • 13
  • 14
-2

Flutter will default the style to the native platform. So iOS/Android apps, while maintaining similar functionality, won't look out of place on the phone as they will follow Cupertino/Material guidelines respectively.