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) {
return Scaffold(
appBar: AppBar(
title: const Text('Responsive Debugger Demo'),
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Header section
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Welcome to Flutter Responsive Debugger!',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text(
'This example demonstrates how the debugger helps you test responsive layouts across different screen sizes.',
style: Theme.of(context).textTheme.bodyMedium,
),
],
),
),
),
const SizedBox(height: 24),
// Screen info section
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Current Screen Info',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 12),
_buildInfoRow('Screen Size',
'${MediaQuery.of(context).size.width.toInt()} × ${MediaQuery.of(context).size.height.toInt()}'),
_buildInfoRow(
'Device Pixel Ratio',
MediaQuery.of(context)
.devicePixelRatio
.toStringAsFixed(2)),
_buildInfoRow(
'Text Scale Factor',
MediaQuery.of(context)
.textScaleFactor
.toStringAsFixed(2)),
_buildInfoRow('Platform', Theme.of(context).platform.name),
_buildInfoRow(
'Orientation', MediaQuery.of(context).orientation.name),
],
),
),
),
const SizedBox(height: 24),
// Responsive layout examples
Text(
'Responsive Layout Examples',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
// Grid that adapts to screen size
LayoutBuilder(
builder: (context, constraints) {
final crossAxisCount = constraints.maxWidth > 600 ? 3 : 2;
return GridView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
crossAxisSpacing: 16,
mainAxisSpacing: 16,
childAspectRatio: 1.2,
),
itemCount: 6,
itemBuilder: (context, index) {
return Card(
elevation: 2,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
_getIconForIndex(index),
size: 32,
color: Theme.of(context).primaryColor,
),
const SizedBox(height: 8),
Text(
'Item ${index + 1}',
style: Theme.of(context).textTheme.titleMedium,
textAlign: TextAlign.center,
),
],
),
),
);
},
);
},
),
const SizedBox(height: 24),
// Instructions
Card(
color: Colors.blue.shade50,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(Icons.info_outline, color: Colors.blue.shade700),
const SizedBox(width: 8),
Text(
'How to Use',
style:
Theme.of(context).textTheme.titleMedium?.copyWith(
color: Colors.blue.shade700,
fontWeight: FontWeight.bold,
),
),
],
),
const SizedBox(height: 12),
const Text(
'1. Look for the floating blue button (📱) on the screen'),
const SizedBox(height: 4),
const Text(
'2. Tap it to open the responsive debugger panel'),
const SizedBox(height: 4),
const Text('3. Try different device presets and settings'),
const SizedBox(height: 4),
const Text('4. Watch how the layout adapts in real-time'),
const SizedBox(height: 4),
const Text(
'5. Enable "Show Layout Bounds" to visualize widget boundaries'),
],
),
),
),
const SizedBox(height: 24),
// Feature showcase
Text(
'Features to Try',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
_buildFeatureChip('📱 Device Presets', Colors.blue),
_buildFeatureChip('🔄 Orientation Toggle', Colors.green),
_buildFeatureChip('🔤 Font Scale', Colors.orange),
_buildFeatureChip('📐 Layout Bounds', Colors.red),
_buildFeatureChip('🛡️ Safe Area', Colors.purple),
_buildFeatureChip('🖥️ Platform Override', Colors.teal),
_buildFeatureChip('🔍 Zoom Control', Colors.indigo),
_buildFeatureChip('📏 Custom Size', Colors.pink),
],
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content:
Text('Use the responsive debugger button to test layouts!'),
duration: Duration(seconds: 2),
),
);
},
child: const Icon(Icons.help_outline),
),
);
}