RelativeScale is a simple custom sizing system for flutter widgets to achieve the same physical sizes across different devices.

Overview

RelativeScale is a simple custom sizing system for flutter widgets to achieve the same physical sizes across different devices.

Pub Version CI likes health popularity GitHub stars style: effective dart GitHub license GitHub last commit


Usage

It is VERY easy to use.

  • sy(value) and sx(value)

  • Example:

    • sy(10) - size relative to screen height.
    • sx(10) - size relative to screen width.
  • RelativeBuilder - A builder widget that provides the scaling methods sy and sx.

  • RelativeScale (deprecated) - A StatefulWidget mixin that provides the scaling methods sy and sx. It is highly recommended to use RelativeBuilder.


Example

RelativeBuilder example.

class Example extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RelativeBuilder(
      builder: (context, height, width, sy, sx) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              'App Bar Title',
              style: TextStyle(fontSize: sy(13)),
            ),
          ),
          body: Container(
            height: height,
            width: width,
            child: Text(
              'Body Text',
              style: TextStyle(fontSize: sy(11)),
            ),
          ),
        );
      },
    );
  }
}

Preview

The base screen size for these widgets is 480 x 800. The source code for this preview app is here.

Scaled with RelativeScale:

Scaled

Now, look at these scaled widgets with RelativeScale. There is a difference, yeah, but that's because of the system scaled sizes like the AppBar (look at the appbar's height :) ). Now let's forget about that and focus on the texts and the rectangle containers. They are the same sizes.

Normal scaling from a flutter:

Unscaled

Now, for unscaled sizes, no RelativeScale at all. Well, that's quite obvious :). Look at the texts on the last image, they are very small comparing to the first image. And the rectangle containers, very big difference.

The full example is in the Example section.


Scaling Notes

For instance, you have a container widget like this:

Container(
    height: 300,
    width: 500,
)

and you implemented RelativeScale:

Container(
    height: sy(300),
    width: sx(500),
)

they will not be the same size anymore, using relative scaler will make your sizes a bit bigger. But the hard work will payoff after adjusting your sizes because your app will now have the same widget sizes in every devices.

Please note that these scaler methods are relative to screen size. So basically in this case sy(50) and sx(50) is NOT the same size.

Also, another thing to note is that if you use sy for height and sx for width (or vice-versa), you'll get widgets with the same ratio (not size) which is still useful. The Scaled preview image above uses only sy, and containers and text have the same size across different screens.

If you want to make a perfect Square container, DON'T do this:

Container(
    height: sy(300),
    width: sx(300),
)
// Yeah they are the same value "300", but they are not the same unit 'cause you used "sx" on the width.

DO this instead:

Container(
    height: sy(300), // or sx(value)
    width: sy(300), // or sx(value)
)

Portrait & Landscape

The library doesn't support orientation yet. If you designed your app for portrait and the user rotates the app to landscape view, the sizes will be messed up. So if you want to use this library, it's highly recommended you lock the rotation. Or if your app should adapt to orientation, DO NOT use this library, there are a lot of alternatives.

You might also like...

SmallTask - Take small steps to achieve big things

SmallTask - Take small steps to achieve big things

SmallTask - Take small steps to achieve big things. Have you ever felt that when you are working on a project you kinda get confused about what to do?

Mar 7, 2022

An app to save money and achieve your goal! Available on iOS & Android 🚀

An app to save money and achieve your goal! Available on iOS & Android 🚀

BudgetMe An app that helps you save money and achieve your goal! Screenshots TestFlight & Google Play Beta License About This is an app to save money

Nov 27, 2022

Achieve ~60 FPS, no matter how heavy the tree is to build/layout

Achieve ~60 FPS, no matter how heavy the tree is to build/layout

flutter_smooth Achieve ~60 FPS, no matter how heavy the tree is to build/layout. 🎼 3-second video output_small.mp4 (left = without smooth, right = sm

Jan 9, 2023

A simple detailed flutter widget that looks almost the same as the real instagram mention widget.

A simple detailed flutter widget that looks almost the same as the real instagram mention widget.

Instagram Mention Widgets 'small details do matter' ❤️ This package provides simple and almost the same UI details that the real Instagram mention wid

Oct 10, 2022

Display multiple simple messages at a same time.

Display multiple simple messages at a same time.

Simple multipurpse flashes to announce different messages to the user and interact with them. Display multiple flashes at the same time and avoid writ

Oct 10, 2022

Practice code of simple flutter app to explain how provider works as across the widget state management.

vdo A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if this is y

Nov 7, 2022

An isolated worker for Flutter (Isolate) and Web (Web Worker). Behaves almost the same as the compute function, except it is not a one-off worker.

A singleton isolated worker for all platforms. On most platforms, it uses Flutter's Isolate, except on the web, since Isolate is not available, it use

Nov 11, 2022

Build a calculator app in iOS and Android with the same code by using flutter.

flutter_calculator Preview Structure Input Widgets input widgets is all extends StatefulWidget, because they need to response the onTap gesture. Numbe

Nov 24, 2022

Peek & Pop implementation for Flutter based on the iOS functionality of the same name.

peek_and_pop Peek & Pop implementation for Flutter based on the iOS functionality of the same name. Finally, the v1.0.0 release! More fluent, more opt

Dec 17, 2022
Comments
  • Can you please explain _calculate function?

    Can you please explain _calculate function?

    https://github.com/xamantra/relative_scale/blob/master/lib/src/relative_scale_base.dart

      double _calculate(double value) {
        return ((value / 100) / 5.333333333333333) * (widget.scale ?? 1);
      }
    

    What is going on here, and what is 5.333333333333333?

    opened by Turbozanik 0
Owner
xamantra
Traveling to Hanamura
xamantra
Flutter package that provides you custom clippers to help you achieve various custom shapes.

flutter_custom_clippers Flutter package that provides you custom clippers to help you achieve various custom shapes. Usage To use this plugin, add flu

Damodar Lohani 291 Dec 23, 2022
Flutter code extension that provides MediaQuery sizing info directly on the BuildContext instance

Flutter code extension that provides MediaQuery sizing info directly on the BuildContext instance. Also adds some helper methods for sizing and layout.

gskinner team 87 Dec 6, 2022
Bluecherry DVR client to run across range of devices. 💙

Bluecherry Client Bluecherry DVR client to run across range of devices. Website • Purchase • Chat [work-in-progress] Download iOS (coming soon) Androi

null 9 Dec 2, 2022
A productivity manager application, designed to sync across your devices

Potato A productivity manager application, designed to sync across your devices, it currently supports macOS! Demo Kapture.2022-08-22.at.11.10.44.mp4

Rohith Gilla 10 Sep 15, 2022
An application built using Flutter that can be used while playing board games if actual or physical dice is missing . This is a dual dice application.

diceee_utter A new Flutter application. Getting Started This project is a starting point for a Flutter application. A few resources to get you started

dev_allauddin 3 Feb 3, 2022
Todo app - contains same UI and different state management techniques (without using StatefulWidget)

This repo contains same UI and different state management techniques (without using StatefulWidget) to compare and prefer based on your coding style.

Aravind Vemula 5 May 19, 2022
Integration Test Preview allows tests on multiple screen sizes in a single e2e test run.

Integration Test Preview has pre-configured methods that allow for faster test deployment for end to end (e2e) test coverage (using Android and iOS pl

The Mobile Applications Community 3 Aug 23, 2022
Flutter remote control - The main use of LongPressDraggable and DragTarget to achieve the universal remote control interaction effect.

Flutter remote control - The main use of LongPressDraggable and DragTarget to achieve the universal remote control interaction effect.

唯鹿 165 Jan 2, 2023
A beautiful app to help you achieve your goals🎯

Goalkeeper ?? A neat, nice looking Flutter app to keep track of your goals! It can be built for iOS and Android, and is available for free on the Play

Urmil Shroff 40 Dec 28, 2022
Dart Koans. Achieve enlightenment through failure.

Dart Koans This application is inspired by Ruby Koans. The idea is simple: You achieve enlightenment though a process of failures. As each failure is

Matthew Butler 41 Oct 3, 2022