Giter VIP home page Giter VIP logo

sizer's Introduction

Sizer (Responsive UI solution for Mobile App,Web and Desktop)

Sizer is helps you to create responsive UI easily.


A Flutter package that effortlessly makes your apps responsive. It automatically adapts the UI to various screen sizes, making responsiveness simple and intuitive. Responsiveness made easy.

Alt Text

Alt Text

Content

Installation ⬇️

Add sizer to pubspec.yaml

dependencies:
  sizer: ^3.0.3

How to use ⚙️

Import the Package

import 'package:sizer/sizer.dart';

Wrap MaterialApp with Sizer widget

Sizer( 
  builder: (context, orientation, screenType) {
    return MaterialApp(
      home: HomePage(),
    );
  },
);

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(
  'Sizer', 
  style: TextStyle(fontSize: 15.dp), 
  // 15.sp can also be used instead of .dp
  // To know their differences, check #FAQ
)

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

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,
   )

*Device.ScreenType can not be Desktop unless maxTabletWidth is set

Guideline

Sizer

  • maxMobileWidth - Maximum width of a mobile device (If the device's width is larger than this, it will be categorized as a tablet) - Default value: 599
  • maxTabletWidth - Maximum width of a tablet device (If the device's width is larger than this, it will be categorized as a desktop) - Optional: enables Desktop ScreenType if enabled

Extensions

  • Adaptive.h() or .h - Calculated percentage of the device's height (40.h -> 40% of device's height)
  • Adaptive.w() or .w - Calculated percentage of the device's width (40.w -> 40% of device's width)
  • Adaptive.sp() or .sp - Calculated sp based on the device's pixel density and aspect ratio (See FAQ)
  • Adaptive.dp() or .dp - Calculated dp based on the device's pixel density (See FAQ)
*Note: Only use .sh and .sw if you want height and width to depend on the device's available height and width after applying SafeArea. Use .h and .w by default.
  • Adaptive.sh() or .sh - Calculated percentage of the remaining device's height after applying SafeArea
  • Adaptive.sw() or .sw - Calculated percentage of the remaining device's width after applying SafeArea

  • Device.boxConstraints - BoxConstraints of the device
  • Device.orientation - Screen Orientation of the device (portrait or landscape)
  • Device.screenType - Screen type of the device (mobile or tablet)
  • Device.aspectRatio - Aspect ratio of the device
  • Device.pixelRatio - Pixel density ratio of the device

  • Adaptive.cm() or .cm - The respective value in value in centimeters
  • Adaptive.mm() or .mm - The respective value in value in millimeters
  • Adaptive.Q() or .Q - The respective value in quarter-millimeters
  • Adaptive.inches() or .inches - The respective value in inches
  • Adaptive.pc() or .pc - The respective value in picas (1/6th of 1 inch)
  • Adaptive.pt() or .pt - The respective value in points (1/72th of 1 inch)
  • Adaptive.px() or .px - The respective value in pixels

Note

You need to import sizer package in order to access number.h, number.w, number.dp, 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:sizer/sizer.dart';

sizer's People

Contributors

technourmish avatar technoprashant avatar

Stargazers

Ivan Aguilar avatar Ahmed_hamdy avatar Ibrahim avatar  avatar  avatar soone avatar Olim avatar AYOUB EL MHAMDI avatar mfauzanakmalpratama avatar Mohammed Al Sahaf avatar 魔咔啦咔 avatar  avatar Shedrack  avatar  avatar flypigz avatar 深雨 avatar Ya Liu avatar  avatar Felipe Estrada avatar Taegyu(Tony) Lim avatar  avatar Sidnei Matos avatar Edvaldo Martins avatar  avatar Deniz Coskun avatar Conrad Mo avatar Aleksej Makaji avatar Srinivas Nahak avatar tan huohui avatar oyuk avatar Mohamad Kaakati avatar Matheus Lopes avatar tom quas avatar Emmanuel Edgar avatar Kuba Wichliński avatar Meissa (Papi) Mbaye avatar  avatar Bovonrajt Phaengsrisara avatar Abdulkhak avatar l3o5s avatar 至东至西至清溪 avatar John Shiver avatar Aditya Bhushan Nagar avatar Hendra avatar Levi avatar Doğan Oğuz avatar Yunus Dikenlitarla avatar  avatar  avatar Ecem Bostancıoğlu avatar  avatar Yılmaz Yağız Dokumacı avatar Ilya Oberemok avatar Taishi Akamatsu avatar  avatar Onur Kağan ALDEMİR avatar  avatar Kevin Wilfried avatar Bashar Harfoush avatar Kassim Belghait avatar Fille avatar Aslam Anver avatar Petros Stergioulas avatar Germán Castro Montes avatar Masahiro Okubo avatar  avatar ysonconcepcion avatar Hüseyin Telli avatar Irina Shefer avatar dddrop avatar SooYoung Kim avatar RagnarokStack avatar Verdi Pratama avatar TheCocce avatar  avatar  avatar  avatar Nattapon Pondongnok avatar LeoLu avatar ChunPangWong avatar Wilsley Germano avatar Yudi Setiawan avatar Pele avatar Pichet Itngam avatar Furkan Karadaş avatar Earl Celis avatar Milad avatar Superazan avatar İlyas Hayrat avatar Autumn avatar Labib Muhajir avatar Guilherme Henrique avatar Masahiro avatar Shalom Hedima avatar Alex McLean avatar Zahid Shaikh avatar Ahmed Ali avatar Ali Ali El-Dabaa avatar Abhishek Thakur avatar Hasan Talha Yalmanbaş avatar

Watchers

 avatar  avatar

sizer's Issues

Make width and height follow orientation properly

Hi, I'm starting to use Sizer and it is great. I'm having an issue though, when trying to support both landscape and portrait orientation, things gets messy, as for some reason Sizer is swapping the width and height when in landscape (for example MediaQuery.of(context).size is Size(668.0, 360.0) (so width: 668, height: 360) but 100.w is 360.0 and 100.0.h is 668.0 (again, when orientation is Orientation.landscape, here both SizerUtil and MediaQuery match).

I notice this is even mentioned in the README, that you should swap width and height based on the orientation, so I guess there is a reason for this, but I can't figure out how to make sense of it. At lease when using the automatic orientation changed by the device.

Am I missing something or is this a bug?

Maybe readme typo errors?

Hi,

in the readme file there is this example:

Container(
      width: 20.0.w,    //It will take a 20% of screen width
      height:30.0.h     //It will take a 20% of screen height
    )

maybe there is a typo error and comment for height: 30.0.h should be It will take a 30% of screen height?

Another question,
TextStyle(fontSize: 15.0.sp), 15.0.sp is always a percentile of screen width or height?

Thanks!

Responsiveness issue in flutter web

Using the below code I had output for flutter web and mobile part.but I see responsiveness is not proper in flutter web part,
Please provide solution for this issue

Code:
Container( decoration: BoxDecoration( border: Border.all(color: Colors.blueAccent)), height: 10.h, padding: const EdgeInsets.all(10), child: Text( "User Name", style: TextStyle(fontSize: 20.sp), ), ),

Web output:
Screenshot 2021-11-30 at 1 18 18 PM

Mobile output:
Screenshot 2021-11-30 at 1 19 32 PM

Mobile output looks neat and In the web output image I see that container is too big and also text font size is also too large.

I know that the responsiveness is calculated based on screen height and text-scale factor because of this issue occurs, but my requirement is I need to release same app page for web and mobile.

MY current workaround is like: if platform is web use different height
Container( decoration: BoxDecoration( border: Border.all(color: Colors.blueAccent)), height:Platform.isWeb?5:h: 10.h, padding: const EdgeInsets.all(10), child: Text( "User Name", style: TextStyle(fontSize:Platform.isWeb?10.sp:20.sp), ), ),

But I don't want to use Platform check for whole application the ui looks to complex if I use platform check,Is there is direct solution for this issue?

Confused about Sizer

Sory for my poor english.I have used this code below
SizerUtil.orientation == Orientation.portrait ? Container( width: 100.0.w, height: 20.0.h, color: Colors.black, ) //for portrait screen : Container( width: 100.0.w, height: 12.0.h, color: Colors.black, )

The problem is landscape mode not working Please check this link
https://ibb.co/SrCPjDL

README

Update the readme for use.

USE SIZER instead of ResponsiveSizer()

Not Giving Any Size(width or Hight) When Android Released App Until First Screen Refresh

Min APK - 16 and 21(i tried both)
In debug mode its works perfectly. But after the app is released to android, it won't work on the first screen until refresh. I tried even with fresh flutter app result is the same.

** in my example second container MediaQuery.of(context).size.height work perfectly in first screen.

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Sizer(
      builder: (context, orientation, deviceType) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const HomeScreen(),
        );
      },
    );
  }
}
class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              height: 10.h,
              width: 10.w,
              color: Colors.grey,
            ),
            Container(
              height: MediaQuery.of(context).size.height/10,
              width: MediaQuery.of(context).size.width/10,
              color: Colors.amber,
            )
          ],
        ),
      ),
    );
  }
}

The Ratio of width and height is incorrect #266

The size of design is 375w and 768h.

And I have a rectangle with 90w and 102h.

Now In a Phone size with 360w and 592.

This rectangle will be: 86w and 78h.

Because of:

(360*90/375) = 86
(592*102/768) = 78

So the [90w, h102] size changed to [86w, 78h]

The problem here is, we have a rectangle with bigger height in the Design,
But we have a rectangle with bigger width in the Phone.

React to changes of the operating system font size

The sizer font size function does not react to the font size of the underlying operating system.

When I increase my system font size, the text is not displayed properly. I guess the sp- function needs to interact with MediaQuery.textScaleFactorOf(context) or something like this.

Read Me Version is incorrect

I am unable to use this plugin and get sizer ^2.0.12 which doesn't match any versions, version solving failed. when trying to install.

Can you also put it for int?

I was using it but I got tired of always having to write 18.0.sp if for example I wanted that font size, it is a bit cumbersome, since we normally use round sizes, and although if it must be in double for those cases where we need to be specific, I I would like you to put it also for int, since it would be much more comfortable. Otherwise it works very well, it is a very good job and I thank you.

hgfdrth

Package does not support web

Hello,
I have tried using this package for web development using Flutter but it throws an error.

Code that causes an error:

`
import 'package:sizer/sizer.dart';

// NavBar constants
double navbarVerticalMargin = 5.0.h;
`

Screenshot (2549)

Can you please consider adding web and desktop support for this package?
For mobile app development, this is a very useful package.
Thanks.

ScreenType has been replaced by DeviceType

I may be wrong but ScreenType has been replaced by DeviceType. This change is not reflected on the README, on pub.dev, or anywhere for that matter.

I can do a PR if you can't update the documentation at the moment.

Defined Size as dp and sp not as percentage.

I see your package to defined size is as a percentage (Max to define the size is 100, ex: 100.0.w / 100.0.h --> it's mean the size is 100% / full screen). But can we specify the size is not using a percentage but using a dp for width and height and a sp for the font?

For example, if in the mockup get the size for the font is 14, so we can't implement your package like this:

style: TextStyle(fontSize: 14.0.sp), // This is not sp, but the font size is 14% from the screen. So it not accurate with the size in the mockup that created by designer.

Also if in the mockup get width is 100 and height is 50 for the Button, so we can't implement your package like this:

SizedBox(
   width: 100.0.w,  // This is not accurate with the size in the mockup that created by designer.
   height: 50.0.h, // This is not accurate with the size in the mockup that created by designer.
   child: RaisedButton(),
),

I think this is the major issue.

In landscape mode, DeviceType is wrong.

My environment is iOS 14.5 Xs simulator. Flutter version is 2.5.3.
DeviceType is set to .tablet.

I think that swapping the size dimensions when the orientation is landscape is causing this bug.

Blank White Page with GetMaterialApp

When using Sizer package along with Getx package. Sizer is not working it showing Empty white screen.

This is how Sizer is applied to GetMaterialApp widget.

GetBuilder<ThemeController>(builder: (themeController) { return Sizer(builder: (context, orientation, deviceType) { return GetMaterialApp( debugShowCheckedModeBanner: false, enableLog: true, logWriterCallback: Logger.write, title: StringHelper.appTitle, theme: themeController.getTheme(), initialRoute: GetPlatform.isMobile ? Routes.splash : isLogin ? Routes.home : Routes.auth, getPages: AppPages.routes, ); }); });

There is no error log to provide.

S20+ is recognized as a tablet

As I already wrote in title Samsung S20+ is recognised as a tablet. It shouldn't and it makes harder to benefit from this package.

Feature Request : Add DeviceType android, iOS

Hi,

I would recommend to add a DeviceType for android and iOS.

With that it would be easier to check device, instead of adding another library to my application project and write own check functionality.

Is the SP value correct ?

Hi,

I'm wondering if your calcul of the sp value is correct because it only use the width and not the height...
/// Calculates the sp (Scalable Pixel) depending on the device's screen size
double get sp => this * (SizerUtil.width / 3) / 100;

Refered to the material documentation (https://material.io/design/layout/pixel-density.html#density-independence) sp must be equal to dp which is dp = (width in pixels * 160) / screen density.

Is there any way to change the value of sp because it seems not right to accord a font value using only the width of the screen...

Different values on the same page?

Hi, I use the library to calculate my header size.
I use 24.h which is equal to 193 pixels.
but when I navigate to a different page and then come back to the old page, 24.h equals 160 pixels, and the header size changes.
Why is that?
I found out that opening and closing the keyboard fixes that issue and the value returns to 193 pixels

Update Documentation

Please update the documentation. It still says to wrap the material app in ResponsiveSizer instead of Sizer

Can't use the dp in a custom AppTheme

Hey guys!

So, I'm using a custom AppTheme, like this:

class AppThemes {
  static final TextTheme _lightTextTheme = TextTheme(
    headline2: TextStyle(
      fontFamily: 'Palanquin',
      fontSize: 26.sp,
      fontWeight: FontWeight.bold,
      color: Colors.neutralTxt,
    ),
   )
}

I did not put here the part of the code where I put the others styles. Also I set the MaterialApp like this:

  Sizer(builder: (context, orientation, deviceType) {
      return MaterialApp(
          theme: AppThemes.lightTheme
   )
}

My issue is: The .dp does not work when I use the text them in a widget.Like this:

Text(
              "Test"                
              style: Theme.of(context).textTheme.headline2,
              textAlign: TextAlign.left,
),

The other font styles (e.g. fontWeight, fontSize and color) works as expected. However, if I put the .dp directly in the widget style like this,

Text(
    "Test"                
     style: TextStyle(
         fontFamily: 'Palanquin',
          fontSize: 26.sp,
          fontWeight: FontWeight.bold,
          color: Colors.neutralTxt,
      ),
      textAlign: TextAlign.left,
),

it works as expected. Do you guys have any clue about what is happening? Thx!

The method '*' was called on null.

Implemented sizer: ^1.1.8 but getting error when I set it as following:

SizedBox(height: 20.0.h),

Error:

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building LoginWithSet(dirty, dependencies: [_FocusMarker, MediaQuery, _InheritedTheme, _LocalizationsScope-[GlobalKey#39772]], state: _LoginWithSetState#2e207):
The method '*' was called on null.
Receiver: null
Tried calling: *(20.0)

The relevant error-causing widget was: 
  
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      SizerUtil.height (package:sizer/sizer_util.dart:32:20)
#2      SizerExt.h (package:sizer/sizer_ext.dart:7:29)
#3      StatefulElement.build (package:flutter/src/widgets/framework.dart:4744:28)
...


Another LateInitializationError issue even after implementing 'return Sizer'

Hello, I've checked many answers on here regarding this issue but I havent been able to solve it :<

class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);

@OverRide
Widget build(BuildContext context) {
return Sizer(
builder: (context, orientation, deviceType){
return GetMaterialApp(
navigatorKey: Get.key,
initialRoute: '/home',
getPages: [
GetPage(
name: '/home',
page: () => HomePage(),
binding: HomeBinding(),
),
],
);
},
);
}
}

which is run on Main with

void main() {
runApp(const MyApp());
}

Any help is very appreciated, Im looking forward to use this plugin :D

Failed to run - dependency issues

Because no versions of freezed match >0.11.6 <0.12.0 and freezed >=0.1.2 <0.11.6 depends on analyzer ^0.39.4, freezed >=0.1.2 <0.11.6-∞ or >0.11.6 <0.12.0 requires analyzer ^0.39.4.
And because analyzer >=0.32.4 <0.39.5 depends on crypto >=1.1.1 <3.0.0, freezed >=0.1.2 <0.11.6-∞ or >0.11.6 <0.12.0 requires crypto >=1.1.1 <3.0.0 or analyzer ^0.39.5.
And because freezed 0.11.6 depends on analyzer ^0.39.5 which depends on crypto ^2.0.0, freezed >=0.1.2 <0.12.0 requires crypto >=1.1.1 <3.0.0.
And because sizer >=2.0.11 depends on universal_io ^2.0.4 which depends on crypto ^3.0.0, freezed >=0.1.2 <0.12.0 is incompatible with sizer >=2.0.11.
So, because XXXX depends on both sizer ^2.0.12 and freezed ^0.11.2, version solving failed.
Running "flutter pub get" in XXXX...
pub get failed (1; So, because XXXX depends on both sizer ^2.0.12 and freezed ^0.11.2, version solving failed.)

Can you please fix it?

LateInitializationError

Hello, i'm starting to use Sizer, thank for for this plugin, but im having an issue though, i tried to make my Text widget like this :

 Text(
                                      "Ajouter au panier",
                                      style: TextStyle(
                                          color: Colors.white, fontSize: 20.sp),
                                    ),

In order to make the fontSize responsive, but i got this error :

LateInitializationError: Field 'width' has not been initialized.

What's the problem with that ?
Thank you in advance.

ReadMe not up to date

Just wanted to inform, that in the readme it says Wrap MaterialApp with ResponsiveSizer widget but actually it is called Sizer now. Took me some time to realize. Maybe it helps others if this is fixed. Cheers and thanks for this cool lib!

web and desktop part in device.type

Hi, thanks for this great plugin. Can you also put web and desktop version of device.type. Currently flutter sdk 2 has web and desktop as stable channel. Thank you.

method 'ResponsiveSizer' isn't defined for the class

Even after importing sizer. dart I am not able to access the ResponsiveSizer.
In Pubspec.yaml

`dependencies:
flutter:
sdk: flutter

cupertino_icons: ^1.0.3
sizer: ^2.0.13`

Error:
Try correcting the name to the name of an existing method, or defining a method named 'ResponsiveSizer'. return ResponsiveSizer( ^^^^^^^^^^^^^^^

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.