ResponsiveBuilder
The ResponsiveBuilder component is a stateless Flutter widget that simplifies the creation of responsive layouts that adapt to different screen sizes. It uses LayoutBuilder to determine the current screen width and provides this information to a builder function, allowing you to construct different UI layouts based on the screen size.
Overview
ResponsiveBuilder is a utility widget that helps in building UIs that gracefully adjust to various devices, from small phones to large desktops. It categorizes screen widths into different LayoutTypes and allows you to define different UI structures for each LayoutType.
Key features of ResponsiveBuilder:
- Screen Size Detection: Uses Flutter's
LayoutBuilderto get the currentBoxConstraintsand determine the available screen width. - LayoutType Categorization: Defines a set of
LayoutTypeenums (compact,medium,expanded,large,extraLarge) representing different screen size categories based on predefined width breakpoints. - Builder Function: Takes a
builderfunction that receives theBuildContext,BoxConstraints, and the determinedLayoutTypeas parameters. - Conditional UI Building: Allows you to build different widget trees within the
builderfunction based on theLayoutType, enabling responsive UI variations. - Integration with LayoutConfig: Often used in conjunction with components like
MiColumnandlayoutConfigto create flexible grid and column layouts that adapt responsively.
Component Name: responsiveBuilder
Note:
- Layout Types (
LayoutTypeenum):ResponsiveBuildercategorizes screen widths into the followingLayoutTypeenums:compact: For very small screens, typically phones in portrait mode (width < 600dp).medium: For smaller tablets and larger phones in landscape mode (width < 840dp).expanded: For larger tablets and smaller desktops (width < 1200dp).large: For standard desktop screens (width < 1600dp).extraLarge: For very large desktop screens (width >= 1600dp).
- Breakpoints: The screen width breakpoints that define these
LayoutTypes are fixed withinResponsiveBuilder:[600, 840, 1200, 1600]. - Usage with
layoutConfigandMiColumn:ResponsiveBuilderis often used internally by layout components likeMiColumn. TheMiColumncomponent utilizesResponsiveBuilderto determine the currentLayoutTypeand then uses itslayoutConfigparameter to decide how many columns to render for thatLayoutType. This combination enables highly flexible and responsive grid layouts. getCrossAxisCountMethod: The static methodResponsiveBuilder.getCrossAxisCount(layoutConfig, layout)is used to parse thelayoutConfigstring and determine the number of columns (crossAxisCount) for a givenLayoutType. ThelayoutConfigstring supports different formats to specify column counts for various screen sizes:"max": Always returns 1 column, regardless ofLayoutType."X-Y-Z-A-B"(5 values): Explicitly setscrossAxisCountforextraLarge-large-expanded-medium-compactLayoutTypes."X-Y-Z"(3 values): SetscrossAxisCountforextraLarge/large,expanded, andmedium/compactLayoutTypes."X-Y"(2 values): SetscrossAxisCountforextraLarge/large/expandedandmedium/compactLayoutTypes."X"(1 value): Sets the samecrossAxisCountfor allLayoutTypes.
- Direct Code Usage: While not directly configured via JSON itself,
ResponsiveBuilderis used extensively within the codebase of UI components that need to be responsive. You typically wrap parts of your UI withResponsiveBuilderand use thelayoutparameter in thebuilderfunction to conditionally render different widgets or adjust layouts.
Example Usage (Code Snippet)
import 'package:flutter/material.dart';
import 'package:your_project/responsive_builder.dart'; // Assuming ResponsiveBuilder is in this path
class MyResponsiveWidget extends StatelessWidget {
const MyResponsiveWidget({super.key});
@override
Widget build(BuildContext context) {
return ResponsiveBuilder(
builder: (BuildContext context, BoxConstraints constraints, LayoutType layout) {
return Container(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
Text(
'Current Layout Type: ${layout.name}',
style: const TextStyle(fontSize: 18),
),
const SizedBox(height: 20),
if (layout == LayoutType.compact) ...[
// UI for compact screens (e.g., phone portrait)
const Text('Compact Layout: Single Column'),
ElevatedButton(onPressed: () {}, child: const Text('Button 1')),
ElevatedButton(onPressed: () {}, child: const Text('Button 2')),
] else if (layout == LayoutType.medium || layout == LayoutType.expanded) ...[
// UI for medium and expanded screens (e.g., tablets)
const Text('Medium/Expanded Layout: Two Columns'),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(onPressed: () {}, child: const Text('Button 1')),
ElevatedButton(onPressed: () {}, child: const Text('Button 2')),
],
),
] else ...[
// UI for large and extraLarge screens (e.g., desktops)
const Text('Large/ExtraLarge Layout: Three Columns'),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(onPressed: () {}, child: const Text('Button 1')),
ElevatedButton(onPressed: () {}, child: const Text('Button 2')),
ElevatedButton(onPressed: () {}, child: const Text('Button 3')),
],
),
],
],
),
);
},
);
}
}
Explanation of Example:
- This code snippet shows how to use
ResponsiveBuilderto create a widget (MyResponsiveWidget) that adapts its layout based on the screen size. - The
builderfunction receives theLayoutType. - Conditional rendering using
if (layout == LayoutType.compact) ...blocks creates different UI structures for differentLayoutTypes. Forcompactscreens, buttons are arranged in a column. Formediumandexpanded, they are in a two-column row. ForlargeandextraLarge, they are in a three-column row. - The example demonstrates how to access the
LayoutType(usinglayout) within thebuilderto customize the UI responsively.