Responsive Widgets and Screen Utilities
Learn about the widgets and utilities provided by GetX to easily implement responsive layouts.
The need for responsiveness
Modern applications aren’t limited to just one type of screen. We use apps on our computers, phones, and even wearable devices. This becomes even more evident when we talk about Flutter, the framework that lets us deploy apps to any platform.
So, developers need to make their apps responsive so that they look good on every device and screen size. GetX helps us achieve responsiveness easily with its structured API and utilities, which give us the necessary information.
One widget, multiple layouts
GetResponsiveView
is a widget that lets us build different layouts for different screen sizes, all in one class. Screen sizes are categorized by the relevant device names and each one has a corresponding method. We can return a different layout in each of these methods, and when the app window is resized, or the app is opened on a device of a different screen size, the relevant layout is rendered automatically. For example, we can have a detailed UI for the desktop app, and a minimal one for the mobile app.
Here’s what the GetResponsiveView
widget looks like:
class MyResponsiveWidget extends GetResponsiveView {@overrideWidget? desktop() {// return desktop layout}@overrideWidget? tablet() {// return tablet layout}@overrideWidget? phone() {// return phone layout}@overrideWidget? watch() {// return watch layout}}
Depending on the screen size, the widget automatically changes its layout as declared in these methods.
Specifying the default layout with the builder
method
We also get a builder
method that the system defaults to in case we don’t specify a widget for a particular screen size. So if we miss out filling the watch
method, for example, the builder
method will take its place.
@overrideWidget? builder() {// return default layout}
It is necessary to use at least one of these methods, all of them cannot be null at the same time.
If we want to render only the builder
method irrespective of the screen size, we can do that by setting the ... ...