Flutter Widgets Flutter Interview Questions
7 questions
What are Keys in Flutter and when do you need one?
A Key preserves a widget's state and identity across rebuilds; you need one whenever Flutter could otherwise confuse two widgets of the same type at the same position, e.g. when reordering a list.
What is InheritedWidget and how does it enable state propagation?
InheritedWidget lets data be efficiently passed down the widget tree without manually threading it through every constructor, and it's the low-level mechanism Provider, Theme, and MediaQuery are all built on.
What's the difference between LayoutBuilder and MediaQuery for responsive design?
MediaQuery gives you the entire screen/device's size and metrics; LayoutBuilder gives you the constraints of just the specific widget it wraps, which is what you actually want for responsive decisions inside nested widgets.
How do you implement form validation in Flutter?
Wrap your fields in a Form widget with a GlobalKey<FormState>, give each TextFormField a validator function, and call formKey.currentState!.validate() to trigger all validators at once.
What are Slivers in Flutter and when would you use CustomScrollView?
Slivers are scrollable areas that can be composed together inside a CustomScrollView, letting you mix different scroll behaviors — like a collapsing app bar, a grid, and a list — into one continuous scrollable, which a single ListView or GridView can't do.
Why does Flutter favor composition over inheritance for building widgets?
Flutter widgets are meant to be combined (composed) into new widgets rather than extended via subclassing, because composition keeps widgets small, reusable, and independently testable — Flutter's own framework widgets are built this way.
How do Hero animations work in Flutter?
A Hero widget with a matching tag on two different screens tells Flutter to automatically animate that widget flying from its position on the first screen to its position on the second during a navigation transition.