Common Widgets
Common Widgets Interview with follow-up questions
1. Can you explain the purpose of the MaterialApp widget in Flutter?
MaterialApp is the convenience widget that sits at (or near) the root of a Material Design app and wires up app-wide infrastructure. It configures things its descendants rely on: theming (theme, darkTheme, themeMode), navigation (the Navigator plus routing — home, routes, or onGenerateRoute), localization (localizationsDelegates, supportedLocales), and the default Directionality/overlay/media setup.
A 2026-relevant follow-up: with the Router API, you typically use MaterialApp.router together with a package like go_router for declarative, deep-link-friendly navigation rather than the imperative routes map. Also note CupertinoApp is the iOS-styled counterpart, and WidgetsApp is the lower-level base both build on.
Follow-up 1
How does MaterialApp handle themes?
MaterialApp allows you to define a theme for your entire app using the 'theme' property. This theme will be applied to all the widgets within the MaterialApp. You can customize various aspects of the theme, such as colors, typography, and shapes, to match your app's design.
Follow-up 2
What is the role of MaterialApp in routing?
MaterialApp provides a 'routes' property that allows you to define named routes for your app. These named routes can be used to navigate between different screens or pages in your app. MaterialApp also provides a 'onGenerateRoute' property that allows you to handle dynamic routing and generate routes on the fly.
Follow-up 3
Can you give an example of how to use MaterialApp?
Sure! Here's an example of how to use MaterialApp:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
routes: {
'/second': (context) => SecondPage(),
},
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Center(
child: RaisedButton(
child: Text('Go to Second Page'),
onPressed: () {
Navigator.pushNamed(context, '/second');
},
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: RaisedButton(
child: Text('Go Back'),
onPressed: () {
Navigator.pop(context);
},
),
),
);
}
}
2. What is the Scaffold widget and how is it used in Flutter?
Scaffold provides the standard Material Design page structure for a single screen. It lays out and coordinates the common visual slots so you don't position them manually: appBar, body, floatingActionButton, drawer/endDrawer, bottomNavigationBar, and bottomSheet. It also manages screen-level behaviors — showing SnackBars and bottom sheets, opening the drawer, and (via MediaQuery) keeping the body out from under the keyboard and notches.
You use one Scaffold per screen (it's typically the widget returned by a route's build, often wrapped by MaterialApp), e.g.:
Scaffold(
appBar: AppBar(title: const Text('Home')),
body: const Center(child: Text('Content')),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.add),
),
);
Gotcha interviewers probe: to show a SnackBar or open a drawer you need a BuildContext below the Scaffold (use ScaffoldMessenger.of(context) for SnackBars), and a Scaffold defines its own layout space — it isn't meant to be the app root by itself (that's MaterialApp).
Follow-up 1
What are some of the properties of the Scaffold widget?
Some of the properties of the Scaffold widget include:
appBar: Specifies the app bar to be displayed at the top of the screen.body: Specifies the main content of the screen.floatingActionButton: Specifies a floating action button to be displayed on the screen.drawer: Specifies a drawer widget to be displayed when the user swipes from the left edge of the screen.bottomNavigationBar: Specifies a bottom navigation bar to be displayed at the bottom of the screen.backgroundColor: Specifies the background color of the screen.
These are just a few examples of the properties available in the Scaffold widget. There are many more properties that allow you to customize the appearance and behavior of the widget.
Follow-up 2
How does Scaffold handle app bar?
The Scaffold widget handles the app bar by providing the appBar property. This property allows you to specify the app bar to be displayed at the top of the screen. The app bar can contain a title, actions, and other widgets.
By default, the app bar is displayed as a Material Design app bar with a back button and a title. You can customize the appearance and behavior of the app bar by setting its properties, such as title, actions, and backgroundColor.
If you don't want to display an app bar, you can set the appBar property to null.
Follow-up 3
Can you provide an example of a basic Scaffold widget?
Sure! Here's an example of a basic Scaffold widget:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: Center(
child: Text('Hello, World!'),
),
),
);
}
}
3. What is the InheritedWidget and how is it used in Flutter?
InheritedWidget is the low-level mechanism for propagating data down the widget tree so descendants can read it without passing it through every constructor. A descendant calls context.dependOnInheritedWidgetOfExactType() (usually wrapped in a static of(context) method) to read the data and register a dependency — so when the InheritedWidget is replaced with new data, only the widgets that actually depend on it rebuild. It's immutable: you "update" it by rebuilding the tree with a new instance, and updateShouldNotify decides whether dependents need to rebuild.
This is the foundation interviewers want you to recognize: Theme, MediaQuery, and Navigator are InheritedWidgets, and popular state-management solutions (Provider, and InheritedNotifier/InheritedModel) are built on top of it. In practice you rarely write one by hand in 2026 — you reach for Provider, Riverpod, or similar — but knowing that those all sit on InheritedWidget, and why it makes dependency-scoped rebuilds efficient, is the expected answer.
Follow-up 1
How does InheritedWidget handle data sharing?
InheritedWidget handles data sharing by creating a data dependency relationship between the InheritedWidget and its descendants. When the data held by the InheritedWidget is updated, it notifies its descendants and triggers a rebuild of the widgets that depend on the data. This allows the descendants to access the updated data without having to explicitly pass it through constructor parameters.
Follow-up 2
What are some use cases for InheritedWidget?
Some use cases for InheritedWidget include:
- Theme data: InheritedWidget can be used to share theme data, such as colors and fonts, throughout the widget tree.
- Localization: InheritedWidget can be used to share the current locale and translations throughout the widget tree.
- User authentication: InheritedWidget can be used to share the user authentication state, such as whether the user is logged in or not, throughout the widget tree.
- App state management: InheritedWidget can be used to share app state, such as the current screen or navigation stack, throughout the widget tree.
Follow-up 3
Can you give an example of how to use InheritedWidget?
Sure! Here's an example of how to use InheritedWidget to share theme data throughout the widget tree:
import 'package:flutter/material.dart';
class ThemeProvider extends InheritedWidget {
final ThemeData themeData;
ThemeProvider({required this.themeData, required Widget child}) : super(child: child);
static ThemeProvider? of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType();
}
@override
bool updateShouldNotify(ThemeProvider oldWidget) {
return themeData != oldWidget.themeData;
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ThemeProvider(
themeData: ThemeData(primaryColor: Colors.blue),
child: MaterialApp(
title: 'My App',
home: MyHomePage(),
),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final themeProvider = ThemeProvider.of(context);
final themeData = themeProvider?.themeData;
return Scaffold(
appBar: AppBar(
title: Text('My Home Page'),
),
body: Container(
color: themeData?.primaryColor,
child: Center(
child: Text(
'Hello, World!',
style: themeData?.textTheme.headline6,
),
),
),
);
}
}
4. Can you explain the difference between stateless and stateful widgets?
A StatelessWidget has no mutable state of its own. Its appearance is fully determined by its constructor parameters (its configuration), and the framework only rebuilds it when its parent passes new inputs. Use it for static or purely input-driven UI — icons, text labels, layout wrappers.
A StatefulWidget can hold state that changes over its lifetime. The widget itself is still immutable; the mutable data lives in a separate State object created by createState(). When that data changes you call setState(), which marks the element dirty and schedules a rebuild. Use it when the widget must respond to user input, animations, timers, or async results — anything that changes after the first build.
Key interview follow-ups:
- Why is the widget always immutable? Widgets are cheap, throwaway configuration. The framework holds the long-lived
Element(andState); separating immutable config from mutable state is what makes Flutter's diffing fast. - Don't reach for StatefulWidget by default. Prefer stateless plus a state-management solution (Riverpod, Bloc) for shared state; use
setStateonly for ephemeral local state. - Gotcha: never put expensive logic or network calls in
build()— it can run many times. Initialize ininitState(), clean up controllers and subscriptions indispose().
Follow-up 1
What are some examples of stateless and stateful widgets?
Some examples of stateless widgets in Flutter include:
- Text: Displays a static piece of text.
- Image: Displays a static image.
- Icon: Displays a static icon.
Some examples of stateful widgets in Flutter include:
- TextField: Allows the user to input text.
- Checkbox: Represents a boolean value that can be toggled.
- Slider: Allows the user to select a value from a range.
Follow-up 2
When would you use a stateless widget over a stateful widget?
You would use a stateless widget when you have content that does not need to change over time. Stateless widgets are more efficient and performant because they do not need to manage any internal state. They are also easier to reason about and test.
For example, if you have a static piece of text or an image that does not need to be updated based on user interactions, you can use a stateless widget.
On the other hand, you would use a stateful widget when you have content that needs to be updated based on user interactions or other events. Stateful widgets allow you to manage and update their internal state, triggering a rebuild of the user interface when necessary.
Follow-up 3
How does Flutter handle the state of stateful widgets?
In Flutter, the state of a stateful widget is managed by a separate object called a 'state'. The state object is created when the stateful widget is instantiated and is associated with that widget for its entire lifetime.
When the state of a stateful widget changes, Flutter calls the 'build' method of the state object to rebuild the user interface. This allows the widget to reflect the updated state.
To update the state of a stateful widget, you can call the 'setState' method provided by the state object. This method notifies Flutter that the state has changed and triggers a rebuild of the user interface.
It's important to note that the state object is persistent and can hold any data that needs to be preserved across rebuilds. This allows you to maintain the state of a widget even when it is rebuilt.
5. What is the role of the Container widget in Flutter?
Container is a convenience widget that bundles common painting, positioning, and sizing widgets into one. It has no behavior of its own — under the hood it composes Padding, Align, DecoratedBox, ConstrainedBox, Transform, and others depending on which arguments you pass. You use it to wrap a child and give it padding, a margin, a background color or decoration, a fixed size, alignment, or a transform.
Container(
margin: const EdgeInsets.all(8),
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: Colors.indigo,
borderRadius: BorderRadius.circular(12),
),
child: const Text('Hello'),
)
Interview follow-ups:
colorvsdecoration: you can't set both —coloris shorthand for aBoxDecoration. Passing both throws.- Sizing when empty: a
Containerwith no child, no size, and unbounded constraints expands to fill; with a child it sizes to the child. This trips people up. - Prefer the lighter widget when you only need one thing —
Padding,SizedBox,ColoredBox, orDecoratedBoxare cheaper and clearer than aContainerdoing one job.
Follow-up 1
What are some properties of the Container widget?
The Container widget in Flutter has several properties that can be used to customize its appearance and behavior. Some of the commonly used properties include:
color: Specifies the background color of the container.alignment: Determines how the child widget is aligned within the container.padding: Specifies the amount of space between the container's edges and its child widget.margin: Specifies the amount of space between the container and its parent widget.widthandheight: Determines the size of the container.decoration: Allows for more advanced styling options, such as borders, gradients, and shadows.constraints: Defines constraints on the container's size.transform: Applies transformations, such as rotation or scaling, to the container.
These are just a few examples, and there are many more properties available for customizing the Container widget.
Follow-up 2
How does the Container widget handle padding and margins?
The Container widget in Flutter provides properties for specifying padding and margins. The padding property allows you to define the amount of space between the container's edges and its child widget. The margin property, on the other hand, specifies the amount of space between the container and its parent widget.
Both padding and margin can be set using the EdgeInsets class, which provides convenient methods for creating padding and margin values. For example, you can use EdgeInsets.all(value) to set equal padding or margin on all sides, or you can use EdgeInsets.symmetric({vertical: value, horizontal: value}) to set different values for the vertical and horizontal directions.
Here's an example of how to use padding and margin in a Container widget:
Container(
padding: EdgeInsets.all(16.0),
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
child: Text('Hello, Flutter!'),
)
Follow-up 3
Can you provide an example of a basic Container widget?
Sure! Here's an example of a basic Container widget:
Container(
width: 200.0,
height: 200.0,
color: Colors.blue,
child: Text('Hello, Flutter!', style: TextStyle(color: Colors.white)),
)
Live mock interview
Mock interview: Common Widgets
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.