responsive_builder

repository·master·Indexed 19 days ago

https://github.com/filledstacks/responsive_builder

A Flutter package for creating responsive UIs with widgets and utilities to handle device screen types (Mobile, Tablet, Desktop, Watch) and orientations (Portrait, Landscape). It features layout builders like ScreenTypeLayout, OrientationLayoutBuilder, and RefinedLayoutBuilder, as well as responsive sizing extensions (.sw and .sh) via the ResponsiveApp widget. It provides SizingInformation for fine-grained control and allows for custom global or local screen breakpoints.

Tokens
4.7K
Snippets
19
Records
20
Agent score
68%

What's inside responsive_builder

  1. Customize iOS launch screen assets

    master

    To change the launch screen image for your iOS application, you can either replace the image files directly in the LaunchImage.imageset directory or use Xcode for a more visual approach.

    Method 1: Manual File Replacement

    Replace the existing image files within the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.

    Method 2: Using Xcode

    1. Open your Flutter project's iOS workspace using the command: open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch images.
    open ios/Runner.xcworkspace
  2. Wrap your app with ResponsiveApp to enable responsive sizing

    master

    To use the responsive sizing extensions (like .sw and .sh) throughout your application, you must wrap your root widget with the ResponsiveApp widget. This widget initializes the global screen dimensions and orientation settings required by the utility class.

    Use the preferDesktop parameter to specify whether the application should default to desktop layouts when a specific layout is not provided.

    ResponsiveApp(
      preferDesktop: true, // Set to true if you want to prefer desktop layouts
      builder: (context) => MyApp(),
    )
  3. Configure Global Screen Breakpoints

    master

    To set breakpoints globally for the entire application, call ResponsiveSizingConfig.instance.setCustomBreakpoints() before runApp(). This will affect all ResponsiveBuilder and ScreenTypeLayout widgets unless they are provided with their own local breakpoints argument.

    void main() {
      ResponsiveSizingConfig.instance.setCustomBreakpoints(
        ScreenBreakpoints(desktop: 800, tablet: 550, watch: 200),
      );
      runApp(MyApp());
    }
  4. Get specific values based on Screen Type

    master

    Instead of rebuilding entire layouts, use getValueForScreenType<T> to retrieve specific values (like padding, font size, or booleans) based on the current DeviceScreenType. This is useful for fine-tuning properties without conditional logic in your widget tree.

    // Get a double value based on screen type
    Container(
      padding: EdgeInsets.all(getValueForScreenType<double>(
                    context: context,
                    mobile: 10,
                    tablet: 30,
                    desktop: 60,
                  )),
      child: Text('Best Responsive Package'),
    )
    
    // Use for conditional visibility
    getValueForScreenType<bool>(
        context: context,
        mobile: false,
        tablet: true,
      ) ? MyWidget() : Container()
  5. Use OrientationLayoutBuilder for orientation-based UI

    master

    OrientationLayoutBuilder allows you to define separate UIs for portrait and landscape orientations without manual MediaQuery checks.

    You can also use the mode property to enforce a specific orientation. The default mode is OrientationLayoutBuilderMode.auto.

    // Return a widget function per orientation
    OrientationLayoutBuilder(
      portrait: (context) => Container(color: Colors.green),
      landscape: (context) => Container(color: Colors.pink),
    ),
    
    // Enforcing orientation mode
    OrientationLayoutBuilder(
      /// default mode is 'auto'
      mode: info.isMobile
        ? OrientationLayoutBuilderMode.portrait
        : OrientationLayoutBuilderMode.auto,
      ...
    ),
  6. Use ResponsiveBuilder for fine-grained control

    master

    The ResponsiveBuilder widget provides a SizingInformation object via its builder function. SizingInformation contains the deviceScreenType, screenSize, and localWidgetSize, allowing you to return different UIs based on the current device type (e.g., DeviceScreenType.desktop, DeviceScreenType.tablet, DeviceScreenType.watch, or DeviceScreenType.mobile).

    // import the package
    import 'package:responsive_builder/responsive_builder.dart';
    
    // Use the widget
    ResponsiveBuilder(
        builder: (context, sizingInformation) {
          // Check the sizing information here and return your UI
              if (sizingInformation.deviceScreenType == DeviceScreenType.desktop) {
              return Container(color:Colors.blue);
            }
    
            if (sizingInformation.deviceScreenType == DeviceScreenType.tablet) {
              return Container(color:Colors.red);
            }
    
            if (sizingInformation.deviceScreenType == DeviceScreenType.watch) {
              return Container(color:Colors.yellow);
            }
    
            return Container(color:Colors.purple);
          },
        },
      );
  7. Use Responsive Sizing extensions

    master

    Once ResponsiveApp is configured, you can use extensions on numbers to define sizes as a percentage of the screen width or height.

    • screenHeight or sh for screen height percentage.
    • screenWidth or sw for screen width percentage.
    import 'package:responsive_builder/responsive_builder.dart';
    
    SizedBox(height: 30.screenHeight); // Or 30.sh
    Text('respond to width', style: TextStyle(fontSize: 10.sw));
  8. Use ScreenTypeLayout for device-specific layouts

    master

    ScreenTypeLayout allows you to pass in widgets for different screen types (mobile, tablet, desktop, watch).

    There are two ways to use it:

    1. Directly passing widgets: All widgets are constructed immediately.
    2. Using .builder: Uses widget builders so that a widget for the correct screen type is only created when needed (lazy loading).

    You can also provide custom breakpoints using a ScreenBreakpoints object to override default device detection.

    // Construct and pass in a widget per screen type
    ScreenTypeLayout(
      mobile: Container(color:Colors.blue),
      tablet: Container(color: Colors.yellow),
      desktop: Container(color: Colors.red),
      watch: Container(color: Colors.purple),
    );
    
    // Construct and pass in a widget builder per screen type (Lazy)
    ScreenTypeLayout.builder(
      mobile: (BuildContext context) => Container(color:Colors.blue),
      tablet: (BuildContext context) => Container(color:Colors.yellow),
      desktop: (BuildContext context) => Container(color:Colors.red),
      watch: (BuildContext context) => Container(color:Colors.purple),
    );
    
    // ScreenTypeLayout with custom breakpoints
    ScreenTypeLayout(
      breakpoints: ScreenBreakpoints(
        tablet: 600,
        desktop: 950,
        watch: 300
      ),
      mobile: Container(color:Colors.blue),
      tablet: Container(color: Colors.yellow),
      desktop: Container(color: Colors.red),
      watch: Container(color: Colors.purple),
    );
  9. Use responsive sizing extensions for width and height

    master

    Once ResponsiveApp is wrapping your application, you can use extensions on num types to calculate dimensions as a percentage of the current screen size. This allows for fluid layouts that scale based on the device dimensions.

    Available extensions:

    • screenHeight / sh: Returns (value / 100) * currentScreenHeight.
    • screenWidth / sw: Returns (value / 100) * currentScreenWidth.

    Example usage:

    Container(
      width: 50.sw, // 50% of screen width
      height: 20.sh, // 20% of screen height
    )
    // Example of using the extensions
    Container(
      width: 50.sw, 
      height: 20.sh,
    )
  10. Switch layouts based on refined sizes with RefinedLayoutBuilder

    master

    The RefinedLayoutBuilder provides more granular layout control than ScreenTypeLayout by using RefinedSize categories. This is useful for adjusting layouts within the same device type (e.g., a large tablet vs. a small tablet).

    Refined Sizes and Default Breakpoints:

    • extraLarge: width > 2160 (Desktop), > 1280 (Tablet), or > 600 (Mobile).
    • large: width > 1440 (Desktop), > 1024 (Tablet), or > 414 (Mobile).
    • normal: width > 1080 (Desktop), > 768 (Tablet), or > 375 (Mobile).
    • small: width < 720 (Desktop), < 600 (Tablet), or < 320 (Mobile).

    Fallback Logic:

    • If extraLarge is active but no extraLarge builder is provided, it falls back to large.
    • If large is active but no large builder is provided, it falls back to normal.
    • If no specific size matches or only normal is provided, it defaults to the normal builder.
    RefinedLayoutBuilder(
      extraLarge: (context) => ExtraLargeWidget(),
      large: (context) => LargeWidget(),
      normal: (context) => NormalWidget(),
      small: (context) => SmallWidget(),
    );