Dario Digregorio
Dario Digregorio

Reputation: 452

Force Flutter Android Application to behave like it is on an iOS Device

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

Upvotes: 3

Views: 3314

Answers (4)

Justin McCandless
Justin McCandless

Reputation: 671

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 debugDefaultTargetPlatformOverride. 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.
  debugDefaultTargetPlatformOverride = 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(),
        ),
      ),
    ),
  );
}

Upvotes: 12

Chenna Reddy
Chenna Reddy

Reputation: 2341

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.

Upvotes: 5

j4hangir
j4hangir

Reputation: 3069

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'),
    );
  }

Upvotes: 3

Bobby X
Bobby X

Reputation: 1

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.

Upvotes: -2

Related Questions