Chapter 1: Getting Started with Flutter
* Introduction to Flutter and its benefits.
* Setting up Flutter on your system (Mac/Windows/Linux).
* Creating a new Flutter project and understanding its structure.
* Running a Flutter app on a mobile device.
Example:
Create a simple Flutter app that displays a greeting on the screen.
```dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text('Hello Flutter!'),
),
),
);
}
}
```
Chapter 2: User Interface Building Blocks
* Working with widgets as the fundamental building blocks of Flutter apps.
* Understanding different types of widgets (e.g., Text, Container, Row, Column).
* Laying out widgets using the Flex and Stack widgets.
* Styling widgets with colors, fonts, and decorations.
Example:
Create a button with a custom style and handle button clicks.
```dart
import 'package:flutter/material.dart';
class MyButton extends StatelessWidget {
final String label;
final Function() onClick;
const MyButton({required this.label, required this.onClick});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: onClick,
child: Text(label),
style: ElevatedButton.styleFrom(
primary: Colors.blue,
onPrimary: Colors.white,
),
);
}
}
```
Chapter 3: State Management with Stateful Widgets
* Introduction to state management and its importance in Flutter.
* Creating stateful widgets that maintain internal state.
* Using the StatefulWidget and State classes to manage state.
* Reacting to user interactions and updating the UI based on state changes.
Example:
Create a counter app that allows users to increment and decrement a count.
```dart
import 'package:flutter/material.dart';
class CounterApp extends StatefulWidget {
@override
_CounterAppState createState() => _CounterAppState();
}
class _CounterAppState extends State {
int count = 0;
void incrementCount() => setState(() => count++);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Count: $count'),
ElevatedButton(
onPressed: incrementCount,
child: Text('Increment'),
),
],
),
),
),
);
}
}
```
Chapter 4: Asynchronous Programming with Futures
* Introduction to asynchronous programming and its importance in Flutter.
* Using the Future class to represent asynchronous operations.
* Handling asynchronous operations using the then() and catchError() methods.
* Working with multiple asynchronous operations using Future.wait() and Future.any().
Example:
Create an app that fetches data from a remote server asynchronously.
```dart
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class NetworkApp extends StatefulWidget {
@override
_NetworkAppState createState() => _NetworkAppState();
}
class _NetworkAppState extends State {
String data = 'Loading...';
Future fetchData() async {
try {
var response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts/1'));
if (response.statusCode == 200) {
setState(() => data = response.body);
} else {
throw Exception('Failed to fetch data!');
}
} catch (e) {
setState(() => data = e.toString());
}
}
@override
void initState() {
super.initState();
fetchData();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text(data),
),
),
);
}
}
```
Chapter 5: Navigation and Routing
* Introduction to navigation in Flutter.
* Using the Navigator class to push and pop routes.
* Understanding different types of routes (e.g., MaterialPageRoute, CupertinoPageRoute).
* Passing data between routes.
Example:
Create an app with multiple screens and navigate between them using a button.
```dart
import 'package:flutter/material.dart';
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Details()),
);
},
child: Text('Go to Details'),
),
),
);
}
}
class Details extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Details'),
),
body: Center(
child: Text('This is the details page'),
),
);
}
}
```