Describe a strategy for ListView on phones and GridView on tablets
This tests Flutter adaptive layout skills. Use LayoutBuilder or MediaQuery to read screen width, pick a breakpoint near 600 dp, and return ListView or GridView conditionally.
What's really being asked
This question probes your ability to build adaptive UIs in Flutter using layout-aware widgets rather than device-type assumptions. Interviewers want to see that you separate layout logic from device identity and that you know which widgets expose constraint and size information.
The full answer
First, name LayoutBuilder as the preferred widget because it receives the parent's BoxConstraints, letting you decide based on the actual available space rather than the full screen size. Second, mention MediaQuery.of(context).size or MediaQuery.sizeOf(context) as an alternative when you need the full screen dimensions or want to react to system-level changes. Third, state a concrete breakpoint; the Material Design guidelines suggest 600 dp as the threshold between compact and medium layouts, so widths below 600 dp render a ListView and widths at or above 600 dp render a GridView. Fourth, explain that you return the appropriate widget from a conditional inside the build method or a dedicated builder method, keeping the data source identical so only the presentation changes. Fifth, note that OrientationBuilder is insufficient on its own because a phone in landscape can be wider than a small tablet in portrait, meaning width beats orientation for this decision.
The mistakes people make
Relying on Platform.isAndroid or Platform.isIOS to decide layout is a major anti-pattern because layout should depend on available space, not operating system. Using Orientation.portrait versus Orientation.landscape misses large phones and foldables in landscape. Hard-coding pixel values like 800 without referencing density-independent pixels shows a lack of understanding of Flutter's device-independent rendering. Finally, rebuilding the entire screen with setState when the user rotates is wasteful; LayoutBuilder already rebuilds automatically when constraints change.
What usually comes next
How would you handle a foldable device with two screens? The interviewer expects mention of MediaQuery.displayFeatures or the TwoPane widget from the flutter_adaptive_scaffold package. What if the list items have different aspect ratios in grid cells? You should discuss GridView.builder with a SliverGridDelegateWithMaxCrossAxisExtent or fixed cross-axis counts. How do you avoid rebuilding the list when switching layouts? You would keep the scroll controller and data source outside the conditional so state persists.
A concrete example
Suppose you wrap the body in a LayoutBuilder. Inside the builder, you check if constraints.maxWidth is greater than 600. If true, you return GridView.builder with crossAxisCount set to two and itemBuilder pointing to your existing card widget. If false, you return ListView.builder with the same itemBuilder and itemCount. Both widgets read from the same list variable, so no data duplication occurs, and the transition is instantaneous when the window resizes on desktop or tablet.
Interview question
What is the best practice for conditionally rendering ListView or GridView based on screen size in Flutter?
- a.Use OrientationBuilder to show ListView in portrait and GridView in landscape, handling all size variations automatically.
- b.Store the screen width in a state variable updated via setState on rotation, and use an 800-pixel breakpoint to choose between the two views.
- c.Wrap the body in a LayoutBuilder, check if constraints.maxWidth is at least 600, and return GridView.builder or ListView.builder with the same itemBuilder.Correct
- d.Use Platform.isAndroid to return GridView on tablets and ListView on phones, since Android tablets run the same OS.
Why? this is the answer
LayoutBuilder uses the parent’s BoxConstraints to choose based on actual available width near the 600 dp breakpoint while sharing the same data source. OrientationBuilder is a tempting distractor because a phone in landscape can be wider than a small tablet in portrait, so width—not orientation—is the correct criterion.
Just read this? Test yourself on what you have been reading.
Read the original → docs.flutter.dev
- #flutter
- #responsive-design
- #adaptive-layout
- #layoutbuilder
- #mediaquery
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.
See open roles