Platform-adapting flutter widgets.

Related tags

Templates platty
Overview

platty

Platform Conforming Widgets for Flutter!

Flutter makes no attempt to provide familiar widgets for a specific platform (unlike React Native, ionic, and other cross platform tooling). This has enormous benefits to unified rendering on all platforms, maximum flexibility, and eliminating a whole class of bugs and testing done for each platform. While this is great, many scenarios we want our apps to look and feel like an Android or iOS app. Platty allows you to render iOS (Cupertino) and Android (Material) like widgets with minimal effort and maximum control in a unified API.

No more checking for platform inside render blocks to render a CupertinoButton or FlatButton, let platty do the logic for you! Want to use bottom tabs in your app that resolve to platform specific UI? No problem!

Widgets

List of Widget Files:

PAlertDialog

PBackButton

PButton/PFlatButton

PNavigationBar/PSliverNavigationBar

PActivityIndicator

PRoute

PScaffold

PSlider

PSwitch

PTabBar

PTextField

Getting Started

Use platty to unify render-specific APIs for you. The library utilizes the BuildContext theming APIs to propagate platform information into the Widgets.

By default, all widgets conform to the default TargetPlatform. It looks up the Theme.of(context).platform for its default. Also, all widgets provide a renderPlatform prop that allows you to choose which one to render (if you wish).

Replace MaterialApp and CupertinoApp with PlatformApp:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return PlatformApp(
      title: 'Flutter Demo',
      // specify our app theme here. We do the leg work of bridging it to Cupertino.
      unifiedTheme: ThemeData(
            colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.red),
            bottomAppBarColor: Colors.red,
          ),
      home: ExamplePage(),
    );
  }
}

PlatformApp unifies all of the same properties between MaterialApp and CupertinoApp to allow both instances of widgets in the hiearchy and switching styling based on platform.

Now you replace widgets that are included in this library with their "P" counterparts:

Buttons

Button/CupertinoButton -> PButton Source

FlatButton/CupertinoButton -> PFlatButton Source

Below is a side-by-side comparison of the different button states. Note how iOS and Android have similar theming.

Button Example

Navigation Bars

AppBar/CupertinoNavigationBar -> PNavigationBar

Android Nav iOS Nav

By default, the PNavigationBar on iOS will mirror Material Android theming. This means button tint and text style of the title will match. If you wish to change that, set iosMirrorAndroid: false. Otherwise it will default to cupertino theming:

Plain iOS Nav

Source

SliverAppBar/CupertinoSliverNavigationBar -> PSliverNavigationBar

Sliders

Slider/CupertinoSlider -> PSlider

Sliders

Source

Switches

Switch/CupertinoSwitch -> PSwitch

Switch

Source

Inputs

TextField/CupertinoTextField -> PTextField

TextField

By default, the PTextField on iOS will mirror Android styling and decoration (map OutlineInputBorder to a similar outline for iOS). Also PTextField on iOS will show helperText and errorText (even though not native iOS widget).

Bottom Navigation

BottomNavigationBar/CupertinoTabBar -> PTabBar

Bottom Navigation Android Bottom Navigation iOS

Source

Scaffold

Scaffold/CupertinoScaffold -> PScaffold

Progress Indicators

CircularProgressIndicator/CupertinoActivityIndicator -> PActivityIndicator

Progress

Source

Back Button

BackButton/CupertinoNavigationBarBackButton -> PBackButton

Alerts

AlertDialog/CupertinoAlertDialog -> PAlertDialog

Android Alert Ios Alert

Source

The Alert expect a List<Widget>. When feeding PFlatButton, utilize the helper methods to theme the buttons properly for iOS:

PAlertDialog(
  title: Text("Sample Alert"),
  content:
      Text("I can adapt based on target platform defaults, PTheme wrapper, "
          "or individual render platform overrides."),
  actions: <Widget>[
    PFlatButton.alertPrimary(
      text: "Ok",
      onPressed: () {
        Navigator.of(context).pop();
      },
    ),
    PFlatButton.alertSecondary(
      text: "Cancel",
      onPressed: () {
        Navigator.of(context).pop();
      },
    )
  ],
)

Properties Specific to a platform have a prefix

Any widgets that have ios-only or android-only counterparts, they are prefixed to android/ios accordingly:

For example PButton, androidShape applies to RaisedButton.shape property. It does not exist on a CupertinoButton. However CupertinoButton has a borderRadius and pressedOpacity. Those two props become iosBorderRadius and iosPressedOpacity.

Helpers

This library bundles a few standard functions to easily return code that is unique for each platform. Instead of checking
and switching on the result of Theme.of(context).targetPlatform, utilize the following methods:

Specific Platform Instance

To have a specific P-Widget utilize a specific platform theme only, such as Material or Cupertino, you can wrap it in a PTheme instance:

PTheme(
  data: PThemeData(
    platform: TargetPlatform.android,  // or iOS
    child: child,
  ),
);

Or, more simply, utilize helper method:

PTheme.ios(child);
PTheme.android(child);

Also, all P-widgets and methods allow you to override the PTheme with a renderPlatform parameter in their constructor or calling method:

PButton("Hello Android", 
  renderPlatform: TargetPlatform.Android,
)

This will swap the rendering over to Material widgets for this specific widget.

Note: Wrapping a widget with the PTheme will propagate that instance down the widget hierarchy and is thus preferred than manually specifying the renderPlatform for each individual widget.

Creating Your Own Platform-Adapting Widgets

We can extend upon the logic included in this library to build our own, powerful platform-adapting widgets. Included in the library is the PlatformAdaptingWidget base class, which inherits from StatelessWidget.

class SamplePlatformWidget extends PlatformAdaptingWidget {
  final Color color;

  SamplePlatformWidget({Key key, @required this.color, TargetPlatform renderPlatform}) // should allow consumers to choose TargetPlatform
      : super(key: key, renderPlatform: renderPlatform);

  /// Render a material widget here. Most Material widgets require a Material Theme instance above it.
  @override
  get renderMaterial => (BuildContext context) {
        return BackButton(
          color: color,
        );
      };

  /// Render a cupertino widget here.
  @override
  get renderCupertino => (BuildContext context) {
        return CupertinoNavigationBarBackButton(
          color: color,
        );
      };
  
  /// Render a fuchsia widget here. (defaults to material)
    @override
    get renderFuchsia => (BuildContext context) {
          return BackButton(
            color: color,
          );
        };
}

Platform-specific logic

This library comes with a few standard ways to implement behavior based on platform. You can utilize platformWrap, which allows you to specify a child, and on 1 or all platforms, wrap it with another widget:

platformWrap(
      context,
      child: PButton(
        padding: EdgeInsets.all(0.0),
        child: Text(title),
        color: Colors.red,
        onPressed: () {
          Navigator.push(context, PRoute.of(context, builder: page));
        },
      ),
      renderCupertino: (context, child) => Padding(
            padding: EdgeInsets.only(bottom: 8.0),
            child: child,
          ),
    );

You can specify any of renderCupertino, renderMaterial, or renderFuschia (or none). Any render methods not specified default to the child.

Also, platformSelect is a helper that enables returning different objects based on platform in a unified way. In our PlatformAdaptingWidget, we utilize it to return a different widget based on platform. You can use it to return any return type based on platform:

Column(
  children: [
    platformSelect(context, 
      renderMaterial: (context) => Text("I am android"),
      renderCupertino: (context) => Text("I am iOS"),
      renderFuchsia: (context) => Text("I am FUCHSIA")) 
  ],
),

renderMaterial and renderCupertino are required. renderFuchsia defaults to material.

or you can return a non-widget too:

Column(
  children: [
    Text(platformSelect(context, 
      renderMaterial: (context) => "I am android"),
      renderCupertino: (context) => "I am iOS",
      renderFuchsia: (context) => "I am FUCHSIA")) 
  ],
),
You might also like...

A platform-adaptive search for Flutter

A platform-adaptive search for Flutter

Flutter Platform Search Flutter platform search is an abstract implementation of the Flutter search delegate. It is a time saving way to implement a p

Dec 31, 2022

A comprehensive guide on learning how to code cross platform mobile applications with the Flutter framework, from the ground up.

A comprehensive guide on learning how to code cross platform mobile applications with the Flutter framework, from the ground up.

✳️ The Ultimate Guide to App Development with Flutter ✳️ A complete and comprehensive guide to learning Flutter with explanations, screenshots, tips,

Jan 1, 2023

Cross Platform mobile application built using Flutter for a group project

splitz A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if this i

Jul 30, 2022

A cross-platform flutter package to convert your links into rich beautiful previews.

A cross-platform flutter package to convert your links into rich beautiful previews.

Link Preview Generator A cross-platform flutter package to convert your links into rich beautiful previews. This package is inspired from Any Link Pre

Oct 21, 2022

Open-source, cross-platform, hassle-free file sharing with AES-256 encryption made with Flutter & Dart.

Open-source, cross-platform, hassle-free file sharing with AES-256 encryption made with Flutter & Dart.

Odin ⚡ Open source easy file sharing for everyone. ⚡ Cross-platform hassle-free file sharing with AES-256 encryption made with Flutter & Dart. Getting

Dec 22, 2022

Allows send emails from flutter using native platform functionality.

flutter_email_sender Allows send emails from flutter using native platform functionality. In android it opens default mail app via intent. In iOS MFMa

Jan 3, 2023

Permission plugin for Flutter. This plugin provides a cross-platform (iOS, Android) API to request and check permissions.

Flutter permission_handler plugin The Flutter permission_handler plugin is build following the federated plugin architecture. A detailed explanation o

Dec 31, 2022

A cross-platform Flutter home workout app that respects your privacy. THIS IS A GITLAB MIRROR, file issues and contribute there.

A cross-platform Flutter home workout app that respects your privacy. THIS IS A GITLAB MIRROR, file issues and contribute there.

Feeel Feeel is an open-source workout app for doing simple at-home exercises. This is a rewrite of the original app in Flutter, to make development ea

Dec 26, 2022

A cross platform mobile application developed in flutter

A cross platform mobile application developed in flutter

A cross platform mobile application 📱 developed in flutter to keep track of dai

Dec 16, 2021
Comments
  • Consider Fuchsia platform

    Consider Fuchsia platform

    Even though it is extremely early, and not exactly testable, the logic on choosing between material and cupertino is a bit flawed here.

    Consider something like this instead:

        return platform == TargetPlatform.ios
            ? renderCupertino(context)
            : renderMaterial(context);
    

    This way, Cupertino will be applied on iOS only, as it should be. The original logic would place Fuchsia in the Cupertino bucket as well.

    opened by hundeva 3
  • More unified widgets

    More unified widgets

    1. Unify button styling between platforms more easily
    2. Added many more examples of buttons, alerts, nav, etc
    3. NEW: Added PTextField with ability to use same between platforms in different visual paradigms
    4. Updated readme
    5. Rename PlatformRoute to PRoute
    6. Add many missing passdown props on PButton/PFlatButton for Android configuration
    7. Added AlertData which is used for PFlatButton to style in alert mode easily.
    opened by agrosner 0
  • MaterialBasedCupertinoThemeData isn't defined for the class PlatformApp

    MaterialBasedCupertinoThemeData isn't defined for the class PlatformApp

    Hi I try use PlatformApp but i got this error message.

    Error: The method 'MaterialBasedCupertinoThemeData' isn't defined for the class '#lib1::PlatformApp'. Try correcting the name to the name of an existing method, or defining a method named 'MaterialBasedCupertinoThemeData'. MaterialBasedCupertinoThemeData(materialTheme: unifiedTheme),

    opened by samanzamani 1
Owner
Fuzz Productions
Fuzz Productions
Target the specific design of Material for Android and Cupertino for iOS widgets through a common set of Platform aware widgets

Flutter Platform Widgets This project is an attempt to see if it is possible to create widgets that are platform aware. Currently in order to render t

null 1.3k Jan 4, 2023
Target the specific design of Material for Android and Cupertino for iOS widgets through a common set of Platform aware widgets

Flutter Platform Widgets This project is an attempt to see if it is possible to create widgets that are platform aware. Currently in order to render t

null 1.3k Jan 4, 2023
ABC of Flutter widgets. Intended for super beginners at Flutter. Play with 35+ examples in DartPad directly and get familiar with various basic widgets in Flutter

Basic Widgets Examples This is aimed for complete beginners in Flutter, to get them acquainted with the various basic widgets in Flutter. Run this pro

Pooja Bhaumik 815 Jan 3, 2023
Widgets intermediate - Intermediate Widgets For Flutter

Intermediate Widgets 4-MODUL 5-LESSON Alert Dialog (Android & IOS) Drawer Single

Tukhtamurodov Sardorbek 2 Feb 6, 2022
Arisprovider - A mixture between dependency injection (DI) and state management, built with widgets for widgets

A mixture between dependency injection (DI) and state management, built with wid

Behruz Hurramov 1 Jan 9, 2022
Flutter-for-Wordpress-App - Cross platform wordpress news app built with Flutter and WP REST API

Flutter for Wordpress A flutter app for a wordpress websites with clean and elegant design. This app is available in free and pro version. You can cho

Madhav Poudel 243 Dec 23, 2022
Flutter-Wordpress-App - Cross platform wordpress news app built with Flutter

Flutter for Wordpress A flutter app for a wordpress websites with clean and elegant design. This app is available in free and pro version. You can cho

Madhav Poudel 243 Dec 23, 2022
Bytebank - Mobile app developed with Flutter in the Flutter training courses on the Alura platform.

bytebank A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if this

Ismael Gomes 1 Feb 3, 2022
The LoginRadius Flutter SDK will let you integrate LoginRadius' customer identity platform with your Flutter application(s).

TODO: Put a short description of the package here that helps potential users know whether this package might be useful for them. Features TODO: List w

Ahmed Yusuf 4 Feb 3, 2022
Flutter Multi-platform allows developers to unleash their app to run on the wide variety of different platforms with little or no change.

Flutter Multi-platform sample Flutter Multi-platform allows developers to unleash their app to run on the wide variety of different platforms with lit

MindInventory 22 Dec 31, 2022