Junior — Middle
Can you explain the principle of responsive design and why it is needed?
sobes.tech AI
Answer from AI
Responsive design is an approach to creating interfaces where the layout and elements automatically adapt to different screen sizes and devices. This is necessary so that the application looks and functions well on both small phone screens and large tablet or desktop screens.
In Flutter, the following are often used for responsive design:
- MediaQuery to obtain screen dimensions.
- LayoutBuilder to build widgets based on available space.
- Responsive widgets that change their appearance or behavior depending on width/height.
Example:
Widget build(BuildContext context) {
var screenWidth = MediaQuery.of(context).size.width;
if (screenWidth < 600) {
return MobileLayout();
} else {
return TabletLayout();
}
}
This approach improves user experience across different devices without the need to write separate code for each.