responsive sizer
responsive sizer helps implement a responsive layout by providing helper widgets and extensions. responsive_sizer has a lot of improvements over the unmaintained sizer package:
- sizer’s maintainer copy-pasted the entire code of this package from ^2.0.1 and didn’t maintain the package ever since 🙁
- ever since ^2.0.1, responsive_sizer has many improvements that includes the current breaking changes
installation
add responsive_sizer
to pubspec.yaml
dependencies:
responsive_sizer: 3.0.4+4
breaking changes
- the package will now use the current width and height regardless of the orientation instead of only using the width and height of portrait mode see issue#6
- changed
.sp
to calculate the value using device’s pixel density ratio instead of calculating the value based on the device’s width
parameters
adaptive.h()
– returns a calculated height based on the device (does the same thing as .h)adaptive.w()
– returns a calculated width based on the device (does the same thing as .w)adaptive.sp()
– returns a calculated sp based on the device (does the same thing as .sp).h
– returns a calculated height based on the device (does the same thing as adaptive.h).w
– returns a calculated width based on the device (does the same thing as adaptive.w).sp
– returns a calculated sp based on the device (does the same thing as adaptive.sp)device.boxconstraints
– returns the device’s boxconstraintsdevice.orientation
– returns the screen orientation (portrait or landscape)device.screentype
– returns the screen type (mobile or tablet)device.aspectratio
– returns the device’s aspect ratiodevice.pixelratio
– returns the device’s pixel ratio
usage
import the package
import 'package:responsive_sizer/responsive_sizer.dart';
wrap materialapp with responsivesizer widget
responsivesizer(
builder: (context, orientation, screentype) {
return materialapp();
}
)
widget size
container(
width: adaptive.w(20), // this will take 20% of the screen's width
height: 30.5.h // this will take 30.5% of the screen's height
)
font size
text(
'responsive sizer',
style: textstyle(fontsize: 15.sp),
)
orientation
if you want to support both portrait and landscape orientations
device.orientation == orientation.portrait
? container( // widget for portrait
width: 100.w,
height: 20.5.h,
)
: container( // widget for landscape
width: 100.w,
height: 12.5.h,
)
screentype
if you want the same layout to look different in tablet and mobile, use the device.screentype
method:
device.screentype == screentype.tablet
? container( // widget for tablet
width: 100.w,
height: 20.5.h,
)
: container( // widget for mobile
width: 100.w,
height: 12.5.h,
)
take note
you need to import responsive_sizer
package in order to access number.h
, number.w
, and number.sp
auto import in vscode and android studio doesn’t work for dart extension methods. typing 10.h
would not bring up auto import suggestion for this package
one workaround is to type device
so that the auto import suggestion would show up:
import 'package:responsive_sizer/responsive_sizer.dart';
Comments are closed.