flutter_adapter
a plugin that adapts the flutter application to different platforms, allowing your flutter application to flexibly and efficiently adapt to various platforms in the same flutter project, maximizing ui multiplexing, and sharing business logic code across different platforms.
preview
usage adapts
the flutter_adapter plugin has three built-in platforms: mobile phone (teadaptplatform.phone), pad horizontal screen (teadaptplatform.padlandscape), pad vertical screen (teadaptplatform.padportrait). if you only need to adapt part of platforms, you only need to make the widget implement the platform-specific build function. other unsuited platforms will return the phone style by default.
if you need to extend the adapted platform, you only need to implement an abstract class that inherits from flexiblestatelesswidget for statelesswidget, then implement the build function of the new platform and register the platform. as for statefulwidget, you only need to implement an abstract class that inherits from flexiblestate, and then implement the build function of the new platform and register the platform.
example
when you use flutter_adapter, you only need to use screenadaptwidget at the entrance of the app, and then set the platform name that the current app needs to adapt.
screenadaptwidget(
platform: teadaptplatform.phone.tostring(),
child: any widget
)),
statelesswidget example
if one of your statelesswidgets needs to be adapted to a particular platform, just pass the widget from the flexiblestatelesswidget and implement the platform-specific build function.
class mystatelesspage extends flexiblestatelesswidget {
@override
widget buildphone(buildcontext context) {
return text('phone',style: textstyle(fontsize: 18.0),);
}
@override
widget buildpadportrait(buildcontext context) {
return text('padportrait',style: textstyle(fontsize: 22.0),);
}
@override
widget buildpadlandscape(buildcontext context) {
return text('padlandscape',style: textstyle(fontsize: 30.0),);
}
}
statefulwidget example
if one of your statefulwidgets needs to be adapted to a specific platform, you only need to inherit the state corresponding to the statefulwidget from flexiblestate, and then implement the build function of the specific platform.
class mystatefulpagestate extends flexiblestate<mystatefulpage> {
@override
widget buildphone(buildcontext context) {
return text('phone',style: textstyle(fontsize: 18.0),);
}
@override
widget buildpadportrait(buildcontext context) {
return text('padportrait',style: textstyle(fontsize: 22.0),);
}
@override
widget buildpadlandscape(buildcontext context) {
return text('padlandscape',style: textstyle(fontsize: 30.0),);
}
}
normal widget example
if one of your widgets only needs to change the value of an individual attribute value on a different platform, then only a cross-platform adaptation of the specific attribute is required. flutter_adapter provides a superobjectadapter
function to solve the cross-platform adaptation problem of attribute values.
class mynormalpage extends statelesswidget {
final string textstr;
mynormalpage(this.textstr);
@override
widget build(buildcontext context) {
return scaffold(
appbar: appbar(
title: const text('page normal'),
),
body: column(
children: <widget>[
container(
padding: edgeinsets.all(10.0),
margin: edgeinsets.only(bottom: 30.0),
width: double.infinity,
height: 100.0,
color: superobjectadapter(context, {teadaptplatform.phone.tostring(): colors.yellow, teadaptplatform.padportrait.tostring(): colors.greenaccent}),
child: center(
child: text(
'$textstr ${superobjectadapter(context, {
teadaptplatform.phone.tostring(): "[phone]",
teadaptplatform.padportrait.tostring(): "[padportrait]"
})}',
style: textstyle(
fontsize: superobjectadapter(context, {teadaptplatform.phone.tostring(): 18.0, teadaptplatform.padportrait.tostring(): 38.0}),
color: colors.black),
),
),
),
],
),
);
}
}
extend the platform that needs to be adapted
the three platforms built into the plug-in may not be sufficient in the actual use process, so we provide an adaptation solution for the user-defined platform.
statelesswidget adapts to the new platform
for statelesswidget, you only need to implement an abstract class that inherits from flexiblestatelesswidget, and then implement the build function of the new platform, and then register the platform.
abstract class customflexiblestatelesswidget extends flexiblestatelesswidget {
@protected
widget buildnewplatform(buildcontext context) {
return buildphone(context); // by default, you can return the phone's style
}
@protected
void initadapter() {
super.initadapter();
addadapter(constant.newplatform, buildnewplatform);// register new platform
}
}
statelesswidget adaptation new platform example:
class mystatelesspage extends customflexiblestatelesswidget {
@override
widget buildphone(buildcontext context) {
return text('phone',style: textstyle(fontsize: 18.0),);
}
@override
widget buildpadportrait(buildcontext context) {
return text('padportrait',style: textstyle(fontsize: 22.0),);
}
@override
widget buildnewplatform(buildcontext context) {
return text('buildnewplatform',style: textstyle(fontsize: 30.0),);
}
}
statefulwidget adapts to the new platform
for statefulwidget, you only need to implement an abstract class that inherits from flexiblestate, and then implement the build function of the new platform, and then register the platform.
abstract class customflexiblestate<t extends statefulwidget> extends flexiblestate<t> {
@protected
widget buildnewplatform(buildcontext context) {
return buildphone(context); // by default, you can return the phone's style
}
@protected
void initadapter() {
super.initadapter();
addadapter(constant.newplatform, buildnewplatform);// register new platform
}
}
statefulwidget adaptation new platform example:
class mystatefulpagestate extends customflexiblestate<mystatefulpage> {
@override
widget buildphone(buildcontext context) {
return text('phone',style: textstyle(fontsize: 18.0),);
}
@override
widget buildpadportrait(buildcontext context) {
return text('padportrait',style: textstyle(fontsize: 22.0),);
}
@override
widget buildnewplatform(buildcontext context) {
return text('newplatform',style: textstyle(fontsize: 30.0),);
}
}
Comments are closed.