flutter_platform_widgets 0.32.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

Flutter Platform Widgets #

Pub GitHub

This project is an attempt to see if it is possible to create widgets that are platform aware. Currently in order to render targeted Android or iOS device specific styles, you need to either conditionaly check the platform or create a set of widgets to render differently depending on the running platform.

This package supports the Stable release as a full released version.

Beta or Dev channels might be supported when there is a pre-release version. Please check the CHANGELOG for version compatibility version.

Due to Master being in rapid development this package is unable to support Master. If this support is required then it is best to fork the repo and locally reference the forked version where changes can be made appropriately.

Installation #

pub.dev: https://pub.dev/packages/flutter_platform_widgets

How it works #

The flutter ThemeData object used with the Theme widget has a platform property. This defaults to TargetPlatform.android on Android and TargetPlatform.ios on iOS, but when creating a ThemeData object, it can be set programmatically. Calling Theme.of(context).platform will return the current platform. Several Flutter library widgets use this field to change how they are rendered, and all of the Flutter Platform Widgets library widgets use this field to render the platform specific versions of things.

Widgets #

These set of widgets allow for rendering based on the target platform using a single cross platform set of widget.

alt text

alt text

Each PlatformWidget provides common properties directly as constructor arguments. If required further customization can be achieved by using the platform widget builder. See the Enhance section of each widget.

PlatformWidget #

A widget that will render either the android widget or cupertino widget based on the target platform. The widgets themselves do not need to be specifically Material or Cupertino.

return PlatformWidget(
  ios: (_) => Icon(CupertinoIcons.flag),
  android: (_) => Icon(Icons.flag),
);

PlatformText #

A widget that will render uppercase for Android. iOS will remain unchanged.

return PlatformText('Cancel');

PlatformSwitch #

A switch widget that will use a Switch for android or a CupertinoSwitch for iOS.

return PlatformSwitch(
  onChanged: (bool value) {},
  value: value,
);

Enhance

return PlatformSwitch(
  onChanged: (bool value) {},
  value: value,
  android: (_) => MaterialSwitchData(...),
  ios: (_) => CupertinoSwitchData(...)
);

PlatformSlider #

A slider widget that will use a Slider for android or a CupertinoSlider for iOS

return PlatformSlider(
  onChanged: (bool value) {},
  value: value,
);

Enhance

return PlatformSlider(
  onChanged: (bool value) {},
  value: value,
  android: (_) => MaterialSliderData(...),
  ios: (_) => CupertinoSliderData(...)
);

PlatformTextField #

A text field widget that will use a TextField for android or a CupertinoTextField for iOS.

return PlatformTextField();

Enhance

return PlatformTextField(
  android: (_) => MaterialTextFieldData(...),
  ios: (_) => CupertinoTextFieldData(...)
);

PlatformButton #

A button that will render a RaisedButton or FlatButton for android or a CupertinoButton for iOS.

return PlatformButton(
  onPressed: () => print('send'),
  child: PlatformText('Send'),
);

Enhance

Extend with WidgetBuilder for android or iOS.

return PlatformButton(
  onPressed: () => print('send'),
  child: PlatformText('Send'),
  android: (_) => MaterialRaisedButtonData(...),
  ios: (_) => CupertinoButtonData(...)
);

Note: For android you can use the FlatButton instead. To do this use the MaterialFlatButtonData on the androidFlat argument.

return PlatformButton(
 onPressed: () => print('send'),
 child: PlatformText('Send'),
 androidFlat: (_) => MaterialFlatButtonData()
);

PlatformIconButton #

A clickable (tappable) button with an icon. Uses IconButton for android or CupertinoButton for iOS.

return PlatformIconButton(
  onPressed: () => print('info pressed'),
  iosIcon: Icon(
    CupertinoIcons.info,
    size: 28.0,
  ),
  androidIcon: Icon(Icons.info)
);

Enhance

Extend with WidgetBuilder for android or iOS.

Widget infoIconButton() {
  return PlatformIconButton(
    onPressed: () => print('info pressed'),
    iosIcon: Icon(CupertinoIcons.info),
    androidIcon: Icon(Icons.info),
    android: (_) => MaterialIconButtonData(...),
    ios: (_) => CupertinoIconButtonData(...),
  );
}

PlatformApp #

A top level widget for the applciation that uses MaterialApp for android or CupertinoApp for iOS.

return PlatformApp(
  title: 'Flutter Demo',
  home: ...
);

Enhance

Extend with WidgetBuilder for android or iOS.

return PlatformApp(
  home:  ...
  android: (_) => MaterialAppData(...)
  ios: (_) => CupertinoAppData(...)
);

PlatformScaffold #

A Scaffold that provides the correctly hosted header (AppBar) and navigation bar (Bottom Bar) for each platform. Uses Scaffold for android or CupertinoTabScaffold for iOS with bottom tabs or CupertinoPageScaffold for iOS without bottom tabs.

return PlatformScaffold(
  appBar: PlatformAppBar()
  body: _buildContent(),
  bottomNavBar: PlatformNavBar(),
  iosContentPadding: false,
  iosContentBottomPadding: false
);

Note that the use of iosContentPadding = true is only required if the content is being obstruced behind the appBar. iosContentBottomPadding is used if the content needs to be above the navBar and not go behind it. This will not have the translucent effect for iOS when these are set to true. If that is desirable, then the scrolling and content alignment need to be managed yourself.

Enhance

Extend with WidgetBuilder for android or iOS.

return PlatformScaffold(
  appBar: PlatformAppBar()
  body: _buildContent(),
  bottomNavBar: PlatformNavBar(),
  android: (_) => MaterialScaffoldData(...)
  ios: (_) => CupertinoPageScaffoldData(...);
);

Both the android and iOS builders are optional. If not provided the Container placeholder widget will be returned.

PlatformTabScaffold #

Note: Using PlatformTabScaffold provides a more refined and flexible experience than using PlatformScaffold.

A Scaffold that provides the correctly hosted header (AppBar) and navigation bar (Bottom Bar) for each platform. Uses Scaffold for android or CupertinoTabScaffold for iOS with bottom tabs.

return PlatformTabScaffold(
  tabController: tabController,
  appBarBuilder: (_, index) => PlatformAppBar(),
  bodyBuilder: (context, index) => _buildContent(index),
  items: _items(context),
);

More more detailed example look at:

Note that the use of iosContentPadding = true is only required if the content is being obstruced behind the appBar. iosContentBottomPadding is used if the content needs to be above the navBar and not go behind it. This will not have the translucent effect for iOS when these are set to true. If that is desirable, then the scrolling and content alignment need to be managed yourself.

Enhance

Extend with WidgetBuilder for android or iOS.

return PlatformTabScaffold(
  tabController: tabController,
  appBarBuilder: (_, index) => PlatformAppBar(),
  bodyBuilder: (context, index) => _buildContent(index),
  items: _items(context),
  android: (_) => MaterialTabScaffoldData(...)
  ios: (_) => CupertinoTabScaffoldData(...);
  androidTabs: (_) => MaterialNavBarData(...)
  iosTabs: (_) => CupertinoTabBarData(...);
);

Both the android and iOS builders are optional. If not provided the Container placeholder widget will be returned.

PlatformAppBar #

The AppBar is the top Header bar with a title, leftside or rightside buttons. Uses AppBar for android or CupertinoNavigationBar for iOS.

return PlatformAppBar(
    title: new Text('Platform Widgets'),
    leading: PlatformIconButton(),
    trailingActions: <Widget>[
      PlatformIconButton(),
    ],
  );

In iOS if a solid color header is required and there is a ListView on the page, you would need to add some alpha to the color so that the ListView is not pushed down too far

     appBar: PlatformAppBar(
       title: Text('iOS Colored Header'),
       ios: (_) => CupertinoNavigationBarData(
             // Issue with cupertino where a bar with no transparency
             // will push the list down. Adding some alpha value fixes it (in a hacky way)
             backgroundColor: Colors.lightGreen.withAlpha(254),
           ),
     ),

Enhance

Extend with WidgetBuilder for android or iOS.

return PlatformAppBar(
  title: new Text('Platform Widgets'),
  leading: PlatformIconButton(),
  trailingActions: <Widget>[
    PlatformIconButton(),
  ],
  android: (_) => MaterialAppBarData(...),
  ios: (_)=> CupertinoNavigationBarData(...),
);

PlatformNavBar #

The NavBar is placed at the bottom of the page with a set of buttons that typically navigate between screens. Implementing this widget requires the parent widget to manage the currentIndex of the page and to set PlatformNavBar.currrentIndex. Uses BottomAppBar with BottomNavigationBar for android or CupertinoTabBar for iOS.

return PlatformNavBar(
  currentIndex: _selectedTabIndex,
  itemChanged: (index) => setState(
        () {
          _selectedTabIndex = index;
        },
      ),
  items: [
    BottomNavigationBarItem(),
    BottomNavigationBarItem(),
  ],
);

Enhance

Extend with WidgetBuilder for android or iOS.

return PlatformNavBar(
  currentIndex: _selectedTabIndex,
  itemChanged: (index) => setState(
        () {
          _selectedTabIndex = index;
        },
      ),
  items: [
    BottomNavigationBarItem(),
    BottomNavigationBarItem(),
  ],
  android: (_) => MaterialNavBarData(...),
  ios: (_) => CupertinoTabBarData(...),
);

PlatformAlertDialog #

The AlertDialog will render a caption/title, body/text and a set of action buttons specific for the platform. Uses AlertDialog for android or CupertinoAlertDialog for iOS.

Note use showPlatformDialog instead of either showDialog from the Material library or showCupertinoDialog from the Cupertino library.

alt text

alt text

showPlatformDialog(
  context: context,
  builder: (_) => PlatformAlertDialog(
    title: Text('Alert'),
    content: Text('Some content'),
    actions: <Widget>[
      PlatformDialogAction(),
      PlatformDialogAction(),
    ],
  ),
);

Enhance

Extend with WidgetBuilder for android or iOS.

showDialog(
  context: context,
  builder: (_) => PlatformAlertDialog(...),
  ios: (_) => CupertinoAlertDialogData(...),
  android: (_) => MaterialAlertDialogData(...),
)

PlatformDialogAction #

The DialogAction widget is used to describe the set of buttons on the AlertDialog. Uses FlatButton for android or CupertinoDialogAction for iOS.

PlatformDialogAction(
  child: PlatformText('Cancel'),
  onPressed: () => Navigator.pop(context),
),

Enhance

Extend with WidgetBuilder for android or iOS.

PlatformDialogAction(
  child: PlatformText('Cancel'),
  onPressed: () => Navigator.pop(context),
  android: (_) => MaterialDialogActionData(...),
  ios: (_) => CupertinoDialogActionData(...),
),

PlatformCircularProgressIndicator #

A circular looking progress indicator. Uses CircularProgressIndicator for android or CupertinoActivityIndicator for iOS.

return PlatformCircularProgressIndicator();

Enhance

Extend with WidgetBuilder for android or iOS.

return PlatformCircularProgressIndicator(
  android: (_) => MaterialProgressIndicatorData(...),
  ios: (_)=> CupertinoProgressIndicatorData(...),
);

PlatformPageRoute #

This function can be used within the Navigator to push either the MaterialPageRoute for android or CupertinoPageRoute for iOS.

  Navigator.push(
    context,
    platformPageRoute(
      builder: pageToDisplayBuilder,
    ),
  );

ShowPlatformModalSheet #

This function is used to either display a ModalBottomSheet for android or CupertinoModalPopup for iOS.

  showPlatformModalSheet(
      context: context,
      builder: (_) => PlatformWidget(
        android: (_) => _androidPopupContent(),
        ios: (_) => _cupertinoSheetContent(),
      ),
    );

Note: Since Android and iOS content may be quite different it may be useful tl use PlatformWidget.

PlatformProvider #

A Provider that provides access to the functions of switching platforms which can be accessed from any screen.

Requires to be placed at the root (above MaterialApp, CupertinoApp or PlatformApp).

  return PlatformProvider(
    builder: (BuildContext context) MaterialApp(...)
  );

Arguments #

initialPlatform

An optional argument initialPlatform can be passed in to force the platform upon startup. This could be useful for development or if the platform is persisted externally (i.e. Shared preferences) and needs to be set on startup.

And to switch platforms...

PlatformProvider.of(context).changeToMaterialPlatform();

or

PlatformProvider.of(context).changeToCupertinoPlatform();

settings

The settings argument have been added to assist in configuring Platform Widgets.

iosUsesMaterialWidgets

  • If true it will add a Material widget above the CupertinoPageScaffold so that Material widgets can be added to the ios page. This does affect dark mode and some ios rendering so it is best to have it false (default). If you use Material widgets on the page simply add Material(child: yourWidget).

PlatformIcons #

Render a Material or Cupertino looking icon

  Icon(context.platformIcons.book)
//or
  Icon(PlatformIcons(context).book)

View the source for the list of icons.

TODO #

  • UI / Unit Tests.
  • Code documentation

Changing / Checking Platform #

When importing flutter_platform_widgets you can check isMaterial(context) or isCupertino(context) to determine what style will be used. This is independent to Platform.isAndroid or Platform.isIOS from 'import 'dart:io'

See the example code for how this is used.

Issues and Feedback #

Please create an issue to provide feedback or an issue.

Acknowledgements #

Inspired by the example given by Swav Kulinski (https://github.com/swavkulinski/flutter-platform-specific-widgets)

[0.32.5] - March 24, 2020 #

(Flutter version support: v1.12.13+hotfix.5)

  • Fixed PlatformButton for android FlatButton to directly use color, disabledColor and padding properties that were missing

[0.32.4] - January 21, 2020 #

(Flutter version support: v1.12.13+hotfix.5)

  • Fix for PlatformTextField not allowing for maxLines to be null which is required when expands is true to enable multiline. (Thanks bangfalse)

[0.32.3] - January 19, 2020 #

(Flutter version support: v1.12.13+hotfix.5)

  • Fix for PlatformTabScaffold to use androidTabs properly. (Thanks abahnj)

[0.32.2] - January 16, 2020 #

(Flutter version support: v1.12.13+hotfix.5)

  • Update widget arguments to match latest flutter widgets.

[0.32.1] - January 15, 2020 #

(Flutter version support: v1.12.13+hotfix.5)

  • Fix for PlatformTabScaffold that would not pickup the right index or set a default value.

[0.32.0] - January 14, 2020 #

(Flutter version support: v1.12.13+hotfix.5)

  • Increased SDK verson to be ">=2.6.0 <3.0.0"
  • Added PlatformIcons where Material and Cupertino icons are picked based on the platform
  • Added PlatformTabScaffold to offer a simplier and more flexible platform tabbed page

[0.31.0] - January 09, 2020 #

(Flutter version support: v1.12.13+hotfix.5)

  • Updated PlatformTextField for Cupertino to support dark mode
  • Removed Material from Cupertino Scaffold as it interfers with ios dark mode. If Material widgets are used on ios then this can be turned back on or add the Material widget to places that have it. Set iosUsesMaterialWidgets = true to retain behvour on the settings object passed in as an argument to PlatformProvider.
  • PlaformSlider for Cupertino uses Colors instead of CupertinoColors to match the underlying flutter widget

[0.30.1] - December 11, 2019 #

(Flutter version support: v1.12.13+hotfix.5)

  • Fixes the breaking change from flutter which causes the error: The getter 'fullObstruction' isn't defined for the class 'ObstructingPreferredSizeWidget'.
  • Updated all widgets compatible with Flutter v1.12.13+hotfix.5

[0.20.2] - October 12, 2019 #

(Flutter version support: v1.9.1+hotfix.2)

  • Fixed unselectedFontSize to be a double and not a bool

[0.20.1] - September 23, 2019 #

(Flutter version support: v1.9.1+hotfix.2)

  • Added a Builder to allow for children of PlatformProvider to get the Theme change on the builder argument.

[0.20.0] - September 16, 2019 #

(Flutter version support: v1.9.1+hotfix.2)

  • Switching of Platform based off theme. (thanks stefanrusek)
  • From now on the only way to dynamically change the target platform is to use PlatformProvider
  • PlatformIconButton can now take a Widget instead of it having to be an Icon (thanks GillesMontyne)

[0.13.0] - September 13, 2019 #

(Flutter version support: v1.9.1+hotfix.2)

  • Updated all widgets compatible with Flutter v 1.9.1
  • Added showPlatformModalSheet
  • Added check for web to allow web to compile (thanks cbenhagen)

[0.12.0] - June 24, 2019 #

(Flutter version support: v1.5.4-hotfix.2)

  • Added PlatformProvider so that swicthing platforms can rebuild the tree
  • Added Material Flat button option for PlatformButton (see Readme)
  • Fixed example issue

[0.11.5] - June 23, 2019 (Do not use) #

(Flutter version support: v1.6.3)

  • Added PlatformProvider so that swicthing platforms can rebuild the tree
  • Due to pub.dev supporting only Stable channel, version 0.11.5 is not supported.

[0.11.0] - June 22, 2019 (Do not use) #

(Flutter version support: v1.6.3)

  • Updated all widgets compatible with Flutter v 1.6.3
  • Added Material Flat button option for PlatformButton (see Readme)
  • Fixed example issue
  • Due to pub.dev supporting only Stable channel, version 0.11.0 is not supported.

[0.10.0] - May 12, 2019 #

(Flutter version support: v1.5.4-hotfix.2)

  • Fix for PlatformIconButton and added field. Thanks ericmartineau
  • Updated all widgets compatible with Flutter v1.5.4-hotfix.2

[0.9.6] - May 03, 2019 #

(Flutter version support: v1.1.8)

  • Added other checks for platforms.
    • Material = Android, Fuchsia, Windows, Linux
    • Cupertino = iOS, MacOS

[0.9.5] - Feb 07, 2019 #

(Flutter version support: v1.1.8)

  • Updated all widgets compatible with Flutter v 1.1.8
  • Added CupertnoThemeData to PlatformApp to style iOS apps.
  • If using PlatformScaffold on each page, no need to define Material widget when using Material widgets on the page

[0.9.0] - Jan 30, 2019 #

(Flutter version support: v1.0.0)

  • Added PlatformSlider

[0.8.3] - Jan 30, 2019 #

(Flutter version support: v1.0.0)

  • Fix for PlatformTextField keyboardType. Thanks furkantektas

[0.8.2] - Jan 29, 2019 #

(Flutter version support: v1.0.0)

  • Added iosContentBottomPadding on PlatformScaffold to prevent the content going behind the navBar
  • Optional android / ios builder for PlatformWidget. No need to specify both if only one if required.

[0.8.1] - Jan 20, 2019 #

(Flutter version support: v1.0.0)

  • Updated README

[0.8.0] - Jan 19, 2019 #

(Flutter version support: v1.0.0)

  • Added PlatformTextField

[0.7.1] - Jan 18, 2019 #

(Flutter version support: v1.0.0)

  • Updated the minSDK version
  • Moved example.dart up a level to appear on pubspec example page

[0.7.0] - Jan 18, 2019 #

(Flutter version support: v1.0.0)

  • Added PlatformSwitch

[0.6.0] - Jan 17, 2019 #

(Flutter version support: v1.0.0)

  • Added PlatformApp. Thanks cmengler
  • Added showPlatformDialog which is required to be used if PlatformApp is used and you need to show dialogs
  • Added platformPageRoute function to pick the right default route for the platform

[0.5.0] - Nov 30, 2018 #

(Flutter version support: v1.0.0)

  • Redone the way PlaformScaffold renders Cupertino. Requires fluttter version 0.11+
  • Added iosContentPadding to PlatformScaffold for iOS to push content down past the navigation (app) bar

[0.4.0] - Oct 27, 2018 #

(Flutter version support: v0.9.4)

  • Caught up with latest flutter verison (0.9.4) adding extra widget properties.

[0.3.4] - Oct 11, 2018 #

  • Fixed herotag issue when setting transitionBetweenRoutes to true

[0.3.3] - Oct 11, 2018 #

  • Added transitionBetweenRoutes and heroTag as attributes to CupertinoNavigationBarData

[0.3.2] - Oct 09, 2018 #

  • Fixed issue with AppBar padding

[0.3.1] - Aug 24, 2018 #

  • Updated environment to support Dart 2

[0.3.0] - Aug 13, 2018 #

  • Removed hasNotch to be compatible with the flutter develop branch

[0.2.0] - May 16, 2018 #

  • Added PlatformCircularProgressIndicator

[0.1.1 - 0.1.4] #

  • Bug fixes

[0.1.0] - May 11, 2018 #

  • Inital Release of Platform widgets

example/lib/main.dart

import 'package:flutter/cupertino.dart'
    show
        CupertinoActionSheet,
        CupertinoActionSheetAction,
        CupertinoDynamicColor,
        CupertinoIcons,
        CupertinoThemeData,
        DefaultCupertinoLocalizations;
import 'package:flutter/material.dart'
    show
        Colors,
        DefaultMaterialLocalizations,
        Icons,
        Theme,
        ThemeData,
        ThemeMode;
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';

import 'iconPage.dart';
import 'listViewHeaderPage.dart';
import 'listViewPage.dart';
import 'tabbed/basicTabbedPage.dart';
import 'tabbed/originalTabbedPage.dart';
import 'tabbed/sliverTabbedPage.dart';
import 'tabbedPage.dart';

void main() => runApp(Main());

class Main extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) => App();
}

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  Brightness brightness = Brightness.light;

  @override
  Widget build(BuildContext context) {
    final materialTheme = new ThemeData(
      primarySwatch: Colors.purple,
    );
    final materialDarkTheme = new ThemeData(
      brightness: Brightness.dark,
      primarySwatch: Colors.teal,
    );

    final cupertinoTheme = new CupertinoThemeData(
      brightness: brightness, // if null will use the system theme
      primaryColor: CupertinoDynamicColor.withBrightness(
        color: Colors.purple,
        darkColor: Colors.cyan,
      ),
    );

    // Example of optionally setting the platform upfront.
    //final initialPlatform = TargetPlatform.iOS;

    // If you mix material and cupertino widgets together then you cam
    // set this setting. Will mean ios darmk mode to not to work properly
    //final settings = PlatformSettingsData(iosUsesMaterialWidgets: true);

    // This theme is required since icons light/dark mode will look for it
    return Theme(
      data: brightness == Brightness.light ? materialTheme : materialDarkTheme,
      child: PlatformProvider(
        //initialPlatform: initialPlatform,
        builder: (context) => PlatformApp(
          localizationsDelegates: <LocalizationsDelegate<dynamic>>[
            DefaultMaterialLocalizations.delegate,
            DefaultWidgetsLocalizations.delegate,
            DefaultCupertinoLocalizations.delegate,
          ],
          title: 'Flutter Platform Widgets',
          android: (_) {
            return new MaterialAppData(
              theme: materialTheme,
              darkTheme: materialDarkTheme,
              themeMode: brightness == Brightness.light
                  ? ThemeMode.light
                  : ThemeMode.dark,
            );
          },
          ios: (_) => new CupertinoAppData(
            theme: cupertinoTheme,
          ),
          home: LandingPage(() {
            setState(() {
              brightness = brightness == Brightness.light
                  ? Brightness.dark
                  : Brightness.light;
            });
          }),
        ),
      ),
    );
  }
}

class LandingPage extends StatefulWidget {
  LandingPage(this.toggleBrightness);

  final void Function() toggleBrightness;

  @override
  LandingPageState createState() => LandingPageState();
}

class LandingPageState extends State<LandingPage> {
  @override
  initState() {
    super.initState();

    textControlller = TextEditingController(text: 'text');
    textMultiLineControlller = TextEditingController(text: 'text multi line');
  }

  bool switchValue = false;
  double sliderValue = 0.5;

  TextEditingController textControlller;

  TextEditingController textMultiLineControlller;

  _switchPlatform(BuildContext context) {
    if (isMaterial(context)) {
      PlatformProvider.of(context).changeToCupertinoPlatform();
    } else {
      PlatformProvider.of(context).changeToMaterialPlatform();
    }
  }

  _toggleBrightness() {
    widget.toggleBrightness();
  }

  @override
  Widget build(BuildContext context) {
    return PlatformScaffold(
      iosContentPadding: true,
      appBar: PlatformAppBar(
        title: Text('Flutter Platform Widgets'),
        trailingActions: <Widget>[
          PlatformIconButton(
            padding: EdgeInsets.zero,
            icon: Icon(context.platformIcons.share),
            onPressed: () {},
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                  'Primary concept of this package is to use the same widgets to create iOS (Cupertino) or Android (Material) looking apps rather than needing to discover what widgets to use.'),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text(
                  'This approach is best when both iOS and Android apps follow the same design in layout and navigation, but need to look as close to native styling as possible.'),
            ),
            Divider(),
            PlatformButton(
              child: PlatformText('Toggle Dark / Light mode'),
              onPressed: _toggleBrightness,
            ),
            Divider(),
            SectionHeader(title: '1. Change Platform'),
            PlatformButton(
              child: PlatformText('Switch Platform'),
              onPressed: () => _switchPlatform(context),
            ),
            PlatformWidget(
              android: (_) => Text('Currently showing Material'),
              ios: (_) => Text('Currently showing Cupertino'),
            ),
            Text('Scaffold: PlatformScaffold'),
            Text('AppBar: PlatformAppBar'),
            Divider(),
            SectionHeader(title: '2. Basic Widgets'),
            PlatformText(
              'PlatformText will uppercase for Material only',
              textAlign: TextAlign.center,
            ),
            PlatformButton(
              child: PlatformText('PlatformButton'),
              onPressed: () {},
            ),
            PlatformButton(
              child: PlatformText('Platform Flat Button'),
              onPressed: () {},
              androidFlat: (_) => MaterialFlatButtonData(),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: PlatformIconButton(
                androidIcon: Icon(Icons.home),
                iosIcon: Icon(CupertinoIcons.home),
                onPressed: () {},
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: PlatformSwitch(
                value: switchValue,
                onChanged: (bool value) => setState(() => switchValue = value),
              ),
            ),
            PlatformSlider(
              value: sliderValue,
              onChanged: (double newValue) {
                setState(() {
                  sliderValue = newValue;
                });
              },
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: PlatformTextField(
                controller: textControlller,
              ),
            ),
            Container(
              padding: const EdgeInsets.all(8.0),
              height: 100,
              child: PlatformTextField(
                expands: true,
                maxLines: null,
                controller: textMultiLineControlller,
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: PlatformCircularProgressIndicator(),
            ),
            Divider(),
            SectionHeader(title: '3. Dialogs'),
            PlatformButton(
              child: PlatformText('Show Dialog'),
              onPressed: () => _showExampleDialog(),
            ),
            Divider(),
            SectionHeader(title: '4. Popup/Sheet'),
            PlatformButton(
              child: PlatformText('Show Popup/Sheet'),
              onPressed: () => _showPopupSheet(),
            ),
            Divider(),
            SectionHeader(title: '5. Navigation'),
            PlatformButton(
              child: PlatformText('Open Tabbed Page'),
              onPressed: () => _openPage((_) => TabbedPage()),
            ),
            PlatformButton(
              child: PlatformText('Open Basic Tabbed Page'),
              onPressed: () => _openPage((_) => BasicTabbedPage()),
            ),
            PlatformButton(
              child: PlatformText('Open Sliver Tabbed Page'),
              onPressed: () => _openPage((_) => SliverTabbedPage()),
            ),
            PlatformButton(
              child: PlatformText('Open Original Tabbed Page'),
              onPressed: () => _openPage((_) => OriginalTabbedPage()),
            ),
            Divider(),
            SectionHeader(title: '6. Icons'),
            PlatformButton(
              child: PlatformText('Open Icons Page'),
              onPressed: () => _openPage((_) => IconsPage()),
            ),
            Divider(),
            SectionHeader(title: '7. Advanced'),
            PlatformButton(
              child: PlatformText('Page with ListView'),
              onPressed: () => _openPage((_) => ListViewPage()),
            ),
            PlatformWidget(
              android: (_) => Container(), //this is for iOS only
              ios: (_) => PlatformButton(
                child: PlatformText('iOS Page with Colored Header'),
                onPressed: () => _openPage((_) => ListViewHeaderPage()),
              ),
            ),
          ],
        ),
      ),
    );
  }

  _openPage(WidgetBuilder pageToDisplayBuilder) {
    Navigator.push(
      context,
      platformPageRoute(
        context: context,
        builder: pageToDisplayBuilder,
      ),
    );
  }

  _showPopupSheet() {
    showPlatformModalSheet(
      context: context,
      builder: (_) => PlatformWidget(
        android: (_) => _androidPopupContent(),
        ios: (_) => _cupertinoSheetContent(),
      ),
    );
  }

  Widget _androidPopupContent() {
    return Container(
      padding: const EdgeInsets.all(16),
      child: Column(
        children: <Widget>[
          GestureDetector(
            child: Container(
              padding: const EdgeInsets.all(8),
              child: PlatformText('Option 1'),
            ),
            onTap: () => Navigator.pop(context),
          ),
          GestureDetector(
            child: Container(
              padding: const EdgeInsets.all(8),
              child: PlatformText('Option 2'),
            ),
            onTap: () => Navigator.pop(context),
          ),
        ],
      ),
    );
  }

  Widget _cupertinoSheetContent() {
    return CupertinoActionSheet(
      title: const Text('Favorite Dessert'),
      message:
          const Text('Please select the best dessert from the options below.'),
      actions: <Widget>[
        CupertinoActionSheetAction(
          child: const Text('Profiteroles'),
          onPressed: () {
            Navigator.pop(context, 'Profiteroles');
          },
        ),
        CupertinoActionSheetAction(
          child: const Text('Cannolis'),
          onPressed: () {
            Navigator.pop(context, 'Cannolis');
          },
        ),
        CupertinoActionSheetAction(
          child: const Text('Trifle'),
          onPressed: () {
            Navigator.pop(context, 'Trifle');
          },
        ),
      ],
      cancelButton: CupertinoActionSheetAction(
        child: const Text('Cancel'),
        isDefaultAction: true,
        onPressed: () {
          Navigator.pop(context, 'Cancel');
        },
      ),
    );
  }

  _showExampleDialog() {
    showPlatformDialog(
      context: context,
      builder: (_) => PlatformAlertDialog(
        title: Text('Alert'),
        content: Text('Some content'),
        actions: <Widget>[
          PlatformDialogAction(
            android: (_) => MaterialDialogActionData(),
            ios: (_) => CupertinoDialogActionData(),
            child: PlatformText('Cancel'),
            onPressed: () => Navigator.pop(context),
          ),
          PlatformDialogAction(
            child: PlatformText('OK'),
            onPressed: () => Navigator.pop(context),
          ),
        ],
      ),
    );
  }
}

class SectionHeader extends StatelessWidget {
  final String title;

  const SectionHeader({
    @required this.title,
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 4.0),
      child: Text(
        title,
        style: TextStyle(fontSize: 18.0),
      ),
    );
  }
}

class Divider extends StatelessWidget {
  const Divider({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 1.0,
      color: new Color(0xff999999),
      margin: const EdgeInsets.symmetric(vertical: 12.0),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_platform_widgets: ^0.32.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
96
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Fix lib/src/platform_scaffold.dart. (-0.50 points)

Analysis of lib/src/platform_scaffold.dart reported 1 hint:

line 156 col 41: 'resizeToAvoidBottomPadding' is deprecated and shouldn't be used. Use resizeToAvoidBottomInset to specify if the body should resize when the keyboard appears. This feature was deprecated after v1.1.9..

Fix lib/src/platform_tab_scaffold.dart. (-0.50 points)

Analysis of lib/src/platform_tab_scaffold.dart reported 1 hint:

line 235 col 41: 'resizeToAvoidBottomPadding' is deprecated and shouldn't be used. Use resizeToAvoidBottomInset to specify if the body should resize when the keyboard appears. This feature was deprecated after v1.1.9..

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test