Builder versus GlobalKey

The real reason we tend to avoid GlobalKey is not about performance. It is more related to the fact that it breaks a few patterns in flutter. Widgets by definition should not be able to access concrete information of other widgets (such as their size or position). And GlobalKey grant the ability to access such … Read more

Flutter Zoomable Widget

As of Flutter 1.20, InteractiveViewer widget supports pan and Zoom out of the box. To make any widget zoomable you need to simply wrap the child with InteractiveViewer. @override Widget build(BuildContext context) { return Center( child: InteractiveViewer( panEnabled: false, // Set it to false to prevent panning. boundaryMargin: EdgeInsets.all(80), minScale: 0.5, maxScale: 4, child: FlutterLogo(size: … Read more

How can I layout widgets based on the size of the parent?

You will want to use the LayoutBuilder widget which will build at layout time and provides the parent widget’s constraints. The LayoutBuilder takes in a build() function which has the the standard BuildContext along with the BoxConstraints as parameters that can be used to help dynamically render widgets based on size. Let’s build a simple … Read more

flutter ListView KeepAlive after some scroll

For automaticKeepAlive to work, each item that needs to be kept alive must send a specific notification. A typical way to fire such notification is using AutomaticKeepAliveClientMixin class Foo extends StatefulWidget { @override FooState createState() { return new FooState(); } } class FooState extends State<Foo> with AutomaticKeepAliveClientMixin { @override Widget build(BuildContext context) { return Container( … Read more

Custom AppBar Flutter

Screenshot: Code: Using flexibleSpace Scaffold( appBar: AppBar( toolbarHeight: 120, // Set this height flexibleSpace: Container( color: Colors.orange, child: Column( children: [ Text(‘One’), Text(‘Two’), Text(‘Three’), Text(‘Four’), ], ), ), ), ) Using PreferredSize Scaffold( appBar: PreferredSize( preferredSize: Size.fromHeight(120), // Set this height child: Container( color: Colors.orange, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(‘One’), Text(‘Two’), Text(‘Three’), Text(‘Four’), … Read more

How to check if scroll position is at top or bottom in ListView?

There are generally two ways of doing it. 1. Using ScrollController // Create a variable final _controller = ScrollController(); @override void initState() { super.initState(); // Setup the listener. _controller.addListener(() { if (_controller.position.atEdge) { bool isTop = _controller.position.pixels == 0; if (isTop) { print(‘At the top’); } else { print(‘At the bottom’); } } }); } … Read more

Layout: Text overflowing in Flutter

You should use Flexible or Expanded Widget like as below code : Card( color: Color(0xFF29ABE2), elevation: 4, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(4), ), child: Wrap( children: [ Container( height: 98, width: MediaQuery.of(context).size.width, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only( bottomRight: Radius.circular(4), topRight: Radius.circular(4), ), ), margin: EdgeInsets.only(left: 3), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Flexible( child: Padding( … Read more

How to Move bottomsheet along with keyboard which has textfield(autofocused is true)?

To fix this issue Add isScrollControlled = true to BottomSheetDialog it’ll allow the bottom sheet to take the full required height which gives more insurance that the keyboard does not cover TextField. Add Keyboard padding using MediaQuery.of(context).viewInsets.bottom Note If your BottomSheetModel is Column make sure you add mainAxisSize: MainAxisSize.min, otherwise the sheet will cover the … Read more

TextField inside of Row causes layout exception: Unable to calculate size

(I assume you’re using a Row because you want to put other widgets beside the TextField in the future.) The Row widget wants to determine the intrinsic size of its non-flexible children so it knows how much space that it has left for the flexible ones. However, TextField doesn’t have an intrinsic width; it only … Read more