1

I want to make a responsive widget. I want it to change to another style of screen when using a different device.

I've read multiple questions like this, but still can't find an answer. The answer is pretty much checking if the device is mobile, tablet or desktop by screen width. If I do this, when I rotate the mobile device, it shows the tablet style screen even though I'm using a mobile device.

However, I see an answer that uses the package, not the way. However, I get the following error when using the package:

════════ Exception caught by widgets library ═══════════════════════════════════
LateInitializationError: Field 'deviceType' has not been initialized.
The relevant error-causing widget was
LayoutBuilder
lib/…/responsive.dart:18
════════════════════════════════════════════════════════════════════════════════

My code:

import 'package:flutter/material.dart';
import 'package:sizer/sizer.dart';

class Responsive extends StatelessWidget {
  const Responsive({required this.mobile, required this.tablet, required this.desktop, super.key});

  final Widget mobile;
  final Widget tablet;
  final Widget desktop;

  static bool isMobile(BuildContext context) => MediaQuery.of(context).size.width < 600.0 && SizerUtil.deviceType == DeviceType.mobile;

  static bool isTablet(BuildContext context) => MediaQuery.of(context).size.width < 1100.0 && MediaQuery.of(context).size.width >= 600.0 && SizerUtil.deviceType == DeviceType.tablet;

  static bool isDesktop(BuildContext context) => MediaQuery.of(context).size.width >= 1100.0 && SizerUtil.deviceType != DeviceType.mobile && SizerUtil.deviceType == DeviceType.tablet;

  @override
  Widget build(BuildContext context) => LayoutBuilder(
        builder: (context, constraints) => constraints.maxWidth >= 1100.0 && SizerUtil.deviceType != DeviceType.mobile && SizerUtil.deviceType != DeviceType.tablet
            ? desktop
            : constraints.maxWidth < 1100.0 && constraints.maxWidth >= 600.0 && SizerUtil.deviceType == DeviceType.tablet
                ? tablet
                : mobile,
      );
}

Feel free to leave a comment if you need more information.

How to make a responsive widget? I would appreciate any help. Thank you in advance!

My Car
  • 4,198
  • 5
  • 17
  • 50

4 Answers4

1

try to Do It like package official example. https://github.com/TechnoUrmish/Sizer/blob/master/example/lib/main.dart

Sizer(
      builder: (context, orientation, deviceType) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Sizer',
          theme: ThemeData.light(),
          home: HomeScreen() ,
        );
      },
    );
IonicFireBaseApp
  • 925
  • 3
  • 10
  • Hi thanks for your answer! Although this answer solved my error, my main question is how to make responsive widget – My Car Dec 06 '22 at 12:48
  • It is very easy try to write your code for three devices (Mobile/Tab/Desktop), with package like Sizer, you can check where your code is running (mobile, tab or desktop). you just need to adjust the your widget size (width/height) accordingly, Apart from Package you can use MediaQuery/ AspectRatio /LayoutBuilder to make your widget responsive so they look best on given device. Thank you. – IonicFireBaseApp Dec 06 '22 at 12:52
0

Use Sizer widget on top of MaterialApp to initialize it. Like the doc example,

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Sizer(//this
      builder: (context, orientation, deviceType) {
        return MaterialApp(

I will follow

class Responsive extends StatelessWidget {
  const Responsive(
      {required this.mobile,
      required this.tablet,
      required this.desktop,
      super.key});

  final Widget mobile;
  final Widget tablet;
  final Widget desktop;

  @override
  Widget build(BuildContext context) =>
      LayoutBuilder(builder: (context, constraints) {
        if (constraints.maxWidth > 600)
          return mobile;
        /// else if (constraints.maxWidth < 1100.0) return tablet; this can be
        else if (constraints.maxWidth >= 600.0 && constraints.maxWidth < 1100.0)
          return tablet;

        return desktop;
      });
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Hi thanks for your answer! Although this answer solved my error, my main question is how to make responsive widget – My Car Dec 06 '22 at 12:44
  • Actually it depends on the widget. Like sometime we will use wrap widget instead of Row. and when the UI is different, i may use different widget for this. For me layoutBuilder was enough. You can follow [adaptive-responsive](https://docs.flutter.dev/development/ui/layout/adaptive-responsive) – Md. Yeasin Sheikh Dec 06 '22 at 12:48
0

If you don't want to use any third party library then you can use LayoutBuilder. or you can use Sizer library.

Link

Aditya Patil
  • 1,287
  • 12
  • 19
  • Hi thanks for your answer! I'm using that package and getting an error. The error is included in the question – My Car Dec 06 '22 at 12:50
0

In order to use SizerUtil.deviceType you must wrap the parent widget of your app with the Sizer widget. To make your app responsive, you can use the w, h, and sp extensions functions provided with the sizer package. These functions allow you creation widget and fonts in proportion to the screen size. For example:

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Sizer(
      builder: (context, orientation, deviceType) => MaterialApp(
        home: Scaffold(
          body: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    if (SizerUtil.deviceType == DeviceType.tablet) {
      return Container(width: 100.w, height: 50.h, color: Colors.red);
    } else {
      return Container(width: 60.w, height: 40.h, color: Colors.red);
    }
  }
}

In the above, if the device is a tablet, the Container takes up 100% (100.w) of the widget and 50% (50.h) of the height. Else, the Container is sized differently.

The .sp extension works similarly except that it's for fonts.

user2233706
  • 6,148
  • 5
  • 44
  • 86
  • Hi thanks for your answer! Your answer solved my error, however, if I do this, when I rotate the mobile device, it shows the tablet style screen even though I'm using a mobile device. – My Car Dec 12 '22 at 01:56
  • Sizer uses the device width to determine whether the device tablet or mobile.Use `Device.orientation` to determine the device orientation in combination with `SizerUtil.deviceType` to render your widgets. – user2233706 Dec 12 '22 at 02:59
  • Hi, how to use `Device.orientation`? Can you update your answer and show how to use it? – My Car Dec 12 '22 at 03:33