flutter platform widgets
platform widgets, target the specific design of material for android and cupertino for ios widgets through a common set of platform aware widgets.
this project is an attempt to see if it is possible to create widgets that are platform aware (platform widgets). currently in order to render targted 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.
widgets
these set of widgets allow for rendering based on the target platform using a single cross platform set of widget (platform widgets).
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');
platformbutton
a button that will render a raisedbutton
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(...)
);
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(...),
);
}
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
);
note that the use of
ioscontentpadding = true
is only required if the content is being obstruced behind the appbar
enhance
extend with widgetbuilder
for android or ios.
return platformscaffold(
appbar: platformappbar()
body: _buildcontent(),
bottomnavbar: platformnavbar(),
android: (_) => materialscaffolddata(...)
ios: (_) => cupertinoscaffolddata(...);
);
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(),
],
);
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
note: hasnotch has been removed to allow for the widget to work with the change on the development branch of flutter. to work around the breaking change either use the material
bottomappbar
directly or cast the result from platformnavbar tobottomappbar
for android builds and set thehasnotch
property. otherwise target version 0.2.0
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 that
showdialog
from the material package needs to be used to make it easy to render.
showdialog(
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(...),
);
todo
- ui / unit tests.
- code documentation
the following are a list more platform aware widgets needing to be added.
changing / checking platform
when importing flutter_platform_widgets
you can check ismaterial
or iscupertino
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.
known limitations
- setting
bottomnavigationbartype.shifting
will cause the icon and text to render white: https://github.com/flutter/flutter/issues/15280.
best to set to fixed if the number of navigation items are 4 or more.
return platformnavbar(
android: (_) => materialnavbardata(
type: bottomnavigationbartype.fixed,
),
- setting
bottomnavigationbar.fixedcolor
to anything has no effect. - if using the cupertino widgets it may complain that there is no material parent when using material widgets further doen the widget tree. if this is the case you need to place
material
as a child widget
return platformscaffold(
body: material(
color: colors.white,
child: _thebodyofthepagewithmaterialwidgets(),
);
);
note: you may fine without setting the color of the material widget there will be a slight grey color appear as the background. you may need to explicitly set the color to match the rest of the page
- cupertino widgets do not pick up the theme in all cases. in particular the text() widget needs to have a defaulttheme set otherwise all text() widgets need to have their style property set.
return defaulttextstyle(
style: theme.of(context).texttheme.body1,
child: center(
child: column(
children: <widget>[
platformtext('text 1'),
platformtext('text 2'),
platformtext('text 3'),
],
),
),
);
Comments are closed.