build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
// Initialize ResponsiveMedia for the current screen
// This must be called before using any ResponsiveMedia utilities
ResponsiveMedia.init(context);
// Get an instance of ResponsiveMedia for convenience
final rm = ResponsiveMedia.instance;
return Scaffold(
appBar: AppBar(
title: const Text('Responsive Media Example'),
),
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Display the current device size category (XS, S, M, L, XL)
// This is useful for debugging and understanding device responsiveness
Text(
'Size Category: ${rm.sizeCategory}',
style: TextStyle(
fontSize: rm.h1, // Dynamically scaled heading size
),
),
// Add spacing between elements
SizedBox(height: rm.spacingS),
// Example: A responsive box that adjusts its width and height
// based on the device's orientation (portrait or landscape)
Container(
// Set the width of the container to 40% of the shortest side of the screen
// The shortest side is dynamically calculated based on the screen's dimensions
// (e.g., in portrait mode, the shortest side is the screen width, and in landscape mode, it's the screen height)
width: rm.shortestSide * 0.4,
// Set the height of the container to 20% of the shortest side of the screen
// This ensures the height is proportionate to the screen size, making the container responsive
height: rm.shortestSide * 0.2,
color: Colors.blueAccent, // Background color of the container
child: Center(
child: Text(
'Responsive Box',
style: TextStyle(
fontSize: rm.body, // Dynamically scaled body text size
color: Colors.white, // Text color
),
),
),
),
// Add spacing between elements
rm.gapS(),
// Example: A container demonstrating dynamic margins and padding
// Margins and paddings are set as percentages of the screen size
Container(
margin: rm.marginAll(
5, // Adds 5% margin on all sides
),
padding: rm.paddingAll(
5, // Adds 5% padding on all sides
),
decoration: BoxDecoration(
color:
Colors.greenAccent, // Background color of the container
borderRadius: BorderRadius.circular(10), // Rounded corners
),
child: Text(
'Margin & Padding Example',
style: TextStyle(
fontSize: rm.body, // Dynamically scaled body text size
),
),
),
],
),
),
),
);
}