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))
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With