fendy3d
fendy3d

Reputation: 413

Do we have to write the code twice in Flutter to export as iOS and Android?

In Flutter, MaterialApp() and CupertinoApp() widgets give "Android" and "Apple" feel respectively. I would like to build an app that serves these 2 operating system but most of the functions are going to be the same except for the looks of it. Do I duplicate my code or is there a way to write once and it can be exported as .apk or .ipa?

[MY EXAMPLE OF DUPLICATING CODE]
if operating_system == "android"{
  runApp(MaterialApp(some code here))
} else if operating_system == "ios"{
  runApp(CupertinoApp(same code as above))
}

Upvotes: 4

Views: 2335

Answers (2)

Yadhu
Yadhu

Reputation: 3305

the answer depends on what you need to achieve,

you can write the whole UI to match the Apple's ios and it'll run without any problem on android, same in the case of Android, so it completely depends on you, if you need to personalise your app for users of different mobile platforms, then you have to write two different UI implementation (not fully though) or you can get away with using a single UI across all the platforms

Upvotes: 0

Adithya Shetty
Adithya Shetty

Reputation: 1287

Yes for the UI, No for most of the backend.

If you want platform Specific UI like Button, then you would have to check the platform and then return the Material Widget or Cupertino Widget


import 'dart:io';
Platform.isIOS // Returns true on iOS devices
Platform.isAndroid // Returns true on Android devices

This Medium Post explains how you can do this in detail : https://medium.com/@mehmetf_71205/platform-specific-ui-with-flutter-7a4da3cc6ed

Here are all the Cupertino(iOS) Widgets available: https://flutter.dev/docs/development/ui/widgets/cupertino

Here are all the Material(Android) Widgets available:

https://flutter.dev/docs/development/ui/widgets/material



Most of the UI behaviour is automatically adapted by flutter while running on iOS and Android. This article shows what platform-specific behaviours and adaptations flutter does automatically: https://flutter.dev/docs/resources/platform-adaptations


Your App backend will not require a platform-specific code. But if you want to use a platform-specific API Then you will have to write platform-specific code.

this post will help you with this: https://flutter.dev/docs/development/platform-integration/platform-channels


Upvotes: 4

Related Questions