Flutter Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. Explain the widget tree in Flutter.
The widget tree is a hierarchical structure of widgets in a Flutter application. Widgets are UI components, and the tree represents the user interface. Each widget has a parent and can have children. Flutter apps are built by combining multiple widgets.
Example:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: Text('Hello, Flutter!'),
),
);
}
}
Ques 2. Explain the difference between 'StatefulWidget' and 'StatelessWidget' in Flutter.
'StatefulWidget' is a widget that can change its state during runtime, while 'StatelessWidget' is a widget that remains immutable once it is built. 'StatefulWidget' is used for dynamic UI components.
Example:
class MyStatefulWidget extends StatefulWidget {
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State{
// Stateful logic here
}
Ques 3. What is a 'BuildContext' in Flutter?
A 'BuildContext' is an object that represents the location of a widget within the widget tree. It is required for various operations, such as building other widgets, navigating to a new screen, and accessing theme data.
Example:
Widget build(BuildContext context) {
return Container(
child: Text('Hello, Flutter!'),
);
}
Ques 4. How does Flutter handle orientation changes?
Flutter handles orientation changes by rebuilding the widget tree when the orientation of the device changes. Developers can use the 'OrientationBuilder' widget to respond to changes in device orientation.
Example:
OrientationBuilder(
builder: (BuildContext context, Orientation orientation) {
return Text('Orientation: $orientation');
},
)
Ques 5. How can you handle user input in Flutter?
User input in Flutter can be handled using widgets such as 'TextField' for text input, 'GestureDetector' for gestures, and 'InkWell' for handling taps. Additionally, you can use the 'onPressed' callback for buttons.
Example:
TextField(
onChanged: (text) {
print('User input: $text');
},
)
Ques 6. What is 'Flutter Packages' and how can they be used?
'Flutter Packages' are reusable pieces of code that can be added to a Flutter project to provide specific functionalities. They are managed using the 'pubspec.yaml' file, and developers can use the 'pub get' command to fetch and install packages.
Example:
dependencies:
http: ^0.13.3
Ques 7. What is the purpose of the 'Navigator' in Flutter?
The 'Navigator' in Flutter is used for managing the navigation stack. It allows developers to push and pop screens, navigate between different pages, and control the flow of the application.
Example:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
Ques 8. What is the purpose of the 'Key' in Flutter?
The 'Key' in Flutter is used to uniquely identify widgets. It helps Flutter differentiate between different instances of the same widget and is essential for efficient widget updates and state preservation.
Example:
Key('myUniqueKey')
Ques 9. How can you perform unit testing in Flutter?
Flutter provides the 'test' package for unit testing. Developers can write test cases using the 'test' package and run them using the 'flutter test' command. Testing involves validating the behavior of individual functions, widgets, or classes.
Example:
test('Addition test', () {
expect(add(1, 2), 3);
});
Ques 10. How can you handle responsive layouts in Flutter?
Responsive layouts in Flutter can be achieved using media queries, which allow developers to define different layouts based on the screen size or device orientation. Additionally, the 'LayoutBuilder' widget can be used to create responsive designs.
Example:
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
if (constraints.maxWidth > 600) {
return DesktopView();
} else {
return MobileView();
}
},
)
Ques 11. What is the purpose of the 'Cupertino' widgets in Flutter?
The 'Cupertino' widgets in Flutter are designed to mimic the look and feel of iOS applications. They provide iOS-specific UI components and are useful for creating a consistent user experience across different platforms.
Example:
CupertinoButton(
child: Text('Press me'),
onPressed: () {
// Handle button press
},
)
Most helpful rated by users: