Flutter Revision With Comments
Flutter Revision With Comments
1. Flutter Basics
// Entry point of the Flutter application
void main() {
runApp(MyApp()); // Launches the app and starts rendering
}
2. Widget Types
// StatelessWidget example - used when no UI update is needed
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text('Hello'); // Just displays static text
}
}
3. Common Widgets
// Row and Column help arrange widgets horizontally or vertically
Row(children: [Text('Left'), Text('Right')])
Column(children: [Text('Top'), Text('Bottom')])
// Text styling
Text('Hello', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold))
4. State Management
// Use setState to update UI when variable changes
int count = 0;
ElevatedButton(onPressed: () {
setState(() { count++; }); // Triggers UI rebuild with new count
}, child: Text('Count: $count'))
7. HTTP Request
// Fetch data from API using HTTP package
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<void> fetchData() async {
final response = await http.get(Uri.parse('https://api.example.com/data'));
if (response.statusCode == 200) {
var data = jsonDecode(response.body); // Convert JSON to Dart object
print(data);
Flutter Complete Revision (With Common Syntax)
}
}
8. Async/Await
// Perform task asynchronously
Future<String> getData() async {
await Future.delayed(Duration(seconds: 1)); // Simulates delay
return 'Done';
}
11. Themes
// Define global styles using ThemeData
MaterialApp(theme: ThemeData(primarySwatch: Colors.blue, textTheme: TextTheme(bodyLarge:
TextStyle(fontSize: 18))))