Widget Layout Tricks in Flutter

This widget layout tricks is posted by seven.srikanth at 5/2/2025 11:40:55 PM



<h1 id="widget-layout-tricks-in-flutter">Widget Layout Tricks in Flutter</h1> <p>Layout is a crucial aspect of Flutter development. Let's explore various layout techniques and tricks to create responsive and flexible user interfaces.</p> <h2 id="responsive-layouts">1. Responsive Layouts</h2> <h3 id="using-layoutbuilder">Using LayoutBuilder</h3> <pre>class ResponsiveLayout extends StatelessWidget { @override Widget build(BuildContext context) { return LayoutBuilder( builder: (context, constraints) { if (constraints.maxWidth &gt; 1200) { return DesktopLayout(); } else if (constraints.maxWidth &gt; 600) { return TabletLayout(); } else { return MobileLayout(); } }, ); } } </pre> <h3 id="using-mediaquery">Using MediaQuery</h3> <pre>class MediaQueryLayout extends StatelessWidget { @override Widget build(BuildContext context) { final size = MediaQuery.of(context).size; final isLandscape = size.width &gt; size.height;

return isLandscape ? LandscapeLayout() : PortraitLayout();

} } </pre> <h2 id="flexible-layouts">2. Flexible Layouts</h2> <h3 id="using-flexible">Using Flexible</h3> <pre>class FlexibleLayout extends StatelessWidget { @override Widget build(BuildContext context) { return Row( children: [ Flexible( flex: 2, child: Container( color: Colors.blue, child: Text(&#39;Flexible 2&#39;), ), ), Flexible( flex: 1, child: Container( color: Colors.green, child: Text(&#39;Flexible 1&#39;), ), ), ], ); } } </pre> <h3 id="using-expanded">Using Expanded</h3> <pre>class ExpandedLayout extends StatelessWidget { @override Widget build(BuildContext context) { return Column( children: [ Expanded( flex: 2, child: Container( color: Colors.blue, child: Text(&#39;Expanded 2&#39;), ), ), Expanded( flex: 1, child: Container( color: Colors.green, child: Text(&#39;Expanded 1&#39;), ), ), ], ); } } </pre> <h2 id="stack-layouts">3. Stack Layouts</h2> <h3 id="using-stack-with-positioned">Using Stack with Positioned</h3> <pre>class StackLayout extends StatelessWidget { @override Widget build(BuildContext context) { return Stack( children: [ Container( color: Colors.blue, height: 200, width: double.infinity, ), Positioned( top: 20, left: 20, child: Container( color: Colors.red, height: 100, width: 100, ), ), Positioned( bottom: 20, right: 20, child: Container( color: Colors.green, height: 100, width: 100, ), ), ], ); } } </pre> <h3 id="using-stack-with-align">Using Stack with Align</h3> <pre>class AlignLayout extends StatelessWidget { @override Widget build(BuildContext context) { return Stack( children: [ Container( color: Colors.blue, height: 200, width: double.infinity, ), Align( alignment: Alignment.topLeft, child: Container( color: Colors.red, height: 100, width: 100, ), ), Align( alignment: Alignment.bottomRight, child: Container( color: Colors.green, height: 100, width: 100, ), ), ], ); } } </pre> <h2 id="custom-layouts">4. Custom Layouts</h2> <h3 id="using-custommultichildlayout">Using CustomMultiChildLayout</h3> <pre>class CustomLayout extends StatelessWidget { @override Widget build(BuildContext context) { return CustomMultiChildLayout( delegate: CustomLayoutDelegate(), children: [ LayoutId( id: &#39;header&#39;, child: Container( color: Colors.blue, height: 100, ), ), LayoutId( id: &#39;content&#39;, child: Container( color: Colors.green, height: 200, ), ), LayoutId( id: &#39;footer&#39;, child: Container( color: Colors.red, height: 100, ), ), ], ); } }

class CustomLayoutDelegate extends MultiChildLayoutDelegate { @override void performLayout(Size size) { final headerSize = layoutChild(&#39;header&#39;, BoxConstraints.loose(size)); positionChild(&#39;header&#39;, Offset.zero);

final footerSize = layoutChild(&amp;#39;footer&amp;#39;, BoxConstraints.loose(size));
positionChild(&amp;#39;footer&amp;#39;, Offset(0, size.height - footerSize.height));

final contentConstraints = BoxConstraints(
  maxWidth: size.width,
  maxHeight: size.height - headerSize.height - footerSize.height,
);
layoutChild(&amp;#39;content&amp;#39;, contentConstraints);
positionChild(&amp;#39;content&amp;#39;, Offset(0, headerSize.height));

}

@override bool shouldRelayout(CustomLayoutDelegate oldDelegate) =&gt; false; } </pre> <h3 id="using-customsinglechildlayout">Using CustomSingleChildLayout</h3> <pre>class CustomSingleLayout extends StatelessWidget { @override Widget build(BuildContext context) { return CustomSingleChildLayout( delegate: CustomSingleLayoutDelegate(), child: Container( color: Colors.blue, height: 100, width: 100, ), ); } }

class CustomSingleLayoutDelegate extends SingleChildLayoutDelegate { @override Offset getPositionForChild(Size size, Size childSize) { return Offset( (size.width - childSize.width) / 2, (size.height - childSize.height) / 2, ); }

@override bool shouldRelayout(CustomSingleLayoutDelegate oldDelegate) =&gt; false; } </pre> <h2 id="layout-performance-tips">5. Layout Performance Tips</h2> <ol> <li><p><strong>Optimize layout calculations</strong></p> <ul> <li>Use appropriate widgets</li> <li>Minimize rebuilds</li> <li>Cache expensive calculations</li> </ul> </li> <li><p><strong>Handle overflow efficiently</strong></p> <ul> <li>Use appropriate overflow widgets</li> <li>Implement proper constraints</li> <li>Clean up resources</li> </ul> </li> <li><p><strong>Test thoroughly</strong></p> <ul> <li>Test different screen sizes</li> <li>Test performance impact</li> <li>Test edge cases</li> </ul> </li> </ol> <h2 id="layout-best-practices">6. Layout Best Practices</h2> <ol> <li><p><strong>Keep layouts simple</strong></p> <ul> <li>Use appropriate widgets</li> <li>Minimize nesting</li> <li>Follow platform conventions</li> </ul> </li> <li><p><strong>Create responsive designs</strong></p> <ul> <li>Use LayoutBuilder</li> <li>Implement proper scaling</li> <li>Handle different screen sizes</li> </ul> </li> <li><p><strong>Document solutions</strong></p> <ul> <li>Document usage</li> <li>Provide examples</li> <li>Include guidelines</li> </ul> </li> </ol> <p>By implementing these layout techniques and following best practices, you can create Flutter applications that are:</p> <ul> <li>More responsive</li> <li>More flexible</li> <li>More maintainable</li> <li>More user-friendly</li> </ul>


Tags: flutter,markdown,generated








0 Comments
Login to comment.
Recent Comments