Answer
Flutter: Size.width vs double.infinity
Understanding the difference between
text
Size.widthtext
double.infinitydouble.infinity
double.infinity is a Dart constant representing an infinitely large value. In Flutter layout, it tells a widget to expand to fill all available space in a given direction.
dart// Using double.infinity Container( width: double.infinity, height: 100, color: Colors.blue, child: Text('Full width container'), )
Size.width
Size.width represents the actual width dimension of a specific
text
Sizedart// Using Size.width from MediaQuery Container( width: MediaQuery.of(context).size.width, height: 100, color: Colors.green, child: Text('Screen width container'), )
Key Differences
| Feature | double.infinity | Size.width |
|---|---|---|
| Type | Constant (∞) | Actual value (e.g., 375.0) |
| Context | No context needed | Requires context |
| Behavior | Fills available space | Uses specific dimension |
| Constraints | Respects parent constraints | Fixed value |
| Responsive | Adapts to parent | Fixed to size source |
Practical Examples
Example 1: Container in Column
dartColumn( children: [ // double.infinity - fills column width Container( width: double.infinity, height: 100, color: Colors.blue, child: Text('Fills column width'), ), SizedBox(height: 10), // MediaQuery size - also fills screen width Container( width: MediaQuery.of(context).size.width, height: 100, color: Colors.green, child: Text('Screen width'), ), ], )
Example 2: Constrained Parent
dart// Parent with constraints SizedBox( width: 200, // Constrained width child: Column( children: [ // double.infinity respects parent constraint (200) Container( width: double.infinity, height: 50, color: Colors.blue, child: Text('200 pixels wide'), ), SizedBox(height: 10), // MediaQuery ignores parent constraint Container( width: MediaQuery.of(context).size.width, height: 50, color: Colors.red, child: Text('Full screen width (overflow!)'), ), ], ), )
Example 3: Responsive Design
dartclass ResponsiveContainer extends StatelessWidget { Widget build(BuildContext context) { final screenWidth = MediaQuery.of(context).size.width; return Container( // Use double.infinity for constrained parent width: double.infinity, height: 200, color: Colors.blue, child: Center( child: Container( // Use percentage of screen width width: screenWidth * 0.8, // 80% of screen height: 150, color: Colors.white, child: Text('80% screen width'), ), ), ); } }
When to Use Each
Use double.infinity when:
- You want a widget to fill available space
- Working with constrained parents (Column, Row, ListView)
- Creating flexible layouts
- You don't need the exact pixel value
dart// Good use of double.infinity Container( width: double.infinity, padding: EdgeInsets.all(16), child: ElevatedButton( onPressed: () {}, child: Text('Full Width Button'), ), )
Use Size.width when:
- You need exact screen dimensions
- Calculating responsive sizes (percentages)
- Positioning widgets absolutely
- Creating overlays or modals
dart// Good use of MediaQuery.size.width Widget build(BuildContext context) { final screenWidth = MediaQuery.of(context).size.width; return Container( width: screenWidth * 0.5, // 50% of screen height: 100, child: Text('Half screen width'), ); }
Common Pitfalls
Pitfall 1: Unbounded Constraints
dart// ❌ Error: RenderBox layout exception Row( children: [ Container( width: double.infinity, // Infinite width in unbounded Row child: Text('Error!'), ), ], ) // ✅ Solution: Use Expanded Row( children: [ Expanded( child: Container( child: Text('Works!'), ), ), ], )
Pitfall 2: Unnecessary MediaQuery
dart// ❌ Verbose and unnecessary Container( width: MediaQuery.of(context).size.width, child: Text('Full width'), ) // ✅ Better: Use double.infinity Container( width: double.infinity, child: Text('Full width'), )
Layout Behavior Comparison
dartclass ComparisonDemo extends StatelessWidget { Widget build(BuildContext context) { return Scaffold( body: Padding( padding: EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Case 1: double.infinity in Column Container( width: double.infinity, height: 100, color: Colors.blue, child: Text('double.infinity'), ), SizedBox(height: 10), // Case 2: MediaQuery.size.width Container( width: MediaQuery.of(context).size.width - 32, // Account for padding height: 100, color: Colors.green, child: Text('MediaQuery.size.width'), ), SizedBox(height: 10), // Case 3: No width specified (shrinks to child) Container( height: 100, color: Colors.orange, child: Text('No width specified'), ), ], ), ), ); } }
Best Practices
- Prefer for flexible layoutstext
double.infinity - Use for responsive calculationstext
MediaQuery.of(context).size.width - Avoid mixing unbounded constraints with text
double.infinity - Use ortext
Expandedin Row/Column instead oftextFlexibletextdouble.infinity - Cache if used multiple timestext
MediaQuery.of(context).size
Remember:
adapts to constraints, whiletextdouble.infinitygives you an exact value. Choose based on whether you need flexible or fixed sizing.textSize.width
Learn more at Flutter Layout Constraints.