AMAD Answer
AMAD Answer
+---------------------+
| Framework (Dart) | ← Widgets, UI Logic
+---------------------+
| Engine (C++) | ← Skia, Dart Runtime
+---------------------+
| Embedder | ← Platform-Specific (Android/iOS)
+---------------------+
This layered design allows Flutter to bypass traditional native UI frameworks (e.g., UIKit,
Android Views), rendering directly to the screen for consistent, high-performance output.
Unit-2
2. List the types of widgets. Explain any one with suitable example:
Stateless Widgets: Immutable, no state changes after creation (e.g., Text, Icon).
Stateful Widgets: Mutable, re-render when state changes (e.g., Checkbox, TextField).
Example (Stateless Widget - Text):
class MyTextWidget extends StatelessWidget {
final String message;
MyTextWidget({required this.message});
@override
Widget build(BuildContext context) {
return Text(
message,
style: TextStyle(fontSize: 20, color: Colors.blue),
);
}
}
// Usage: MyTextWidget(message: 'Hello Flutter')
This widget displays static text and doesn’t change unless rebuilt with a new `message`.
Stateful Widgets:
Maintain mutable state that can change over time (e.g., user input, counters).
Rebuilds when `setState()` is called to reflect state changes.
Example: A counter widget that updates its display.
More complex, requiring a separate `State` class.
Key Difference: Stateless is for fixed UI; Stateful handles dynamic updates.
4. What are the categories of Flutter widgets?
Basic Widgets: Core UI elements like `Text`, `Image`, `Icon`.
Layout Widgets: Arrange other widgets, e.g., `Row`, `Column`, `Stack`, `Container`.
Input Widgets: Handle user input, e.g., `TextField`, `Checkbox`, `Radio`.
Navigation Widgets: Manage screen transitions, e.g., `Navigator`, `AppBar`.
Material Widgets: Follow Material Design (e.g., `ElevatedButton`, `Card`).
Cupertino Widgets: iOS-style widgets (e.g., `CupertinoButton`,
`CupertinoAlertDialog`).
Animation Widgets: Add motion, e.g., `AnimatedContainer`, `Hero`.
5. Enlist the commonly used widgets of Flutter. Explain any two with examples:
Commonly used: `Text`, `Image`, `Container`, `Row`, `Column`, `ElevatedButton`,
`Padding`, `Scaffold`, `AppBar`, `ListView`.
Text: Displays styled text.
Text(
'Welcome to Flutter',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.green,
),
textAlign: TextAlign.center,
)
Container: A versatile widget for styling, sizing, and positioning.
Container(
width: 100,
height: 100,
color: Colors.blue,
child: Center(child: Text('Box', style: TextStyle(color: Colors.white))),
margin: EdgeInsets.all(10),
)
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Top', style: TextStyle(fontSize: 20)),
SizedBox(height: 10),
Text('Middle', style: TextStyle(fontSize: 20)),
SizedBox(height: 10),
Text('Bottom', style: TextStyle(fontSize: 20)),
],
)
10. Create a simple UI design to demonstrate the Row and Column widgets:
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('Row & Column Demo')),
body: Padding(
padding: EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Name: Alice', style: TextStyle(fontSize: 18)),
Text('Age: 30', style: TextStyle(fontSize: 18)),
],
),
Icon(Icons.person, size: 50, color: Colors.blue),
],
),
),
),
);
}
}
12. Create a simple UI design to demonstrate the use of ElevatedButton and Container
Widgets:
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('Button & Container Demo')),
body: Center(
child: Container(
width: 200,
height: 100,
decoration: BoxDecoration(
color: Colors.green[100],
borderRadius: BorderRadius.circular(10),
),
child: ElevatedButton(
onPressed: () {
print('Button Pressed!');
},
child: Text('Press Me', style: TextStyle(fontSize: 20)),
style: ElevatedButton.styleFrom(primary: Colors.green),
),
),
),
),
);
}
}
Unit-3
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go Back'),
)
Replace the current screen:
ElevatedButton(
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => NewScreen()),
);
},
child: Text('Replace Screen'),
)
4. How data can be passed from one screen to another? Explain with suitable example:
Data is typically passed via constructor parameters when pushing a new screen.
Example:
// First Screen
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => SecondScreen(data: 'Hello from First!'),
),
);
},
child: Text('Pass Data'),
),
),
);
}
}
// Second Screen
class SecondScreen extends StatelessWidget {
final String data;
SecondScreen({required this.data});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text(data, style: TextStyle(fontSize: 20))),
);
}
}
setState(() {
counter++;
});
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {
'/': (context) => HomeScreen(),
'/second': (context) => SecondScreen(),
},
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/second');
},
child: Text('Go to Second Screen'),
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Second Screen')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Go Back'),
),
),
);
}
}