This document serves as a guide for the first day of Flutter training. It includes an overview of the topics covered, possible questions with answers, and necessary commands or instructions.
- Setting up a Flutter project
- Understanding the
main.dart
file - Widgets (Stateless & Stateful)
- AppBar with icons
- Buttons (Elevated, Text, Outlined, Icon Buttons)
- Containers and styling
- ListView and basic layout widgets
- Floating Action Button (FAB)
- Displaying images from the internet
Run the following command in your terminal or command prompt:
flutter create my_project_name
Then, navigate into the project folder:
cd my_project_name
Make sure you have a device/emulator running, then execute:
flutter run
- The
main.dart
file is the entry point of a Flutter application. - It contains the
main()
function, which callsrunApp()
to start the app. MyApp
(aStatelessWidget
) defines the app’s theme and the home screen.
- StatelessWidget: Does not change over time (e.g., a simple text display).
- StatefulWidget: Can change dynamically (e.g., a counter that increments when a button is clicked).
Scaffold
provides a framework for UI components likeAppBar
,FloatingActionButton
,Drawer
, etc.
Use the AppBar
widget inside Scaffold
:
appBar: AppBar(
title: Text("My App"),
actions: [
IconButton(
icon: Icon(Icons.notifications),
onPressed: () {
print("Notification clicked");
},
),
],
)
ElevatedButton(
onPressed: () {
print("Elevated button clicked");
},
child: Text("Elevated Button"),
),
Other types:
TextButton
for minimal styling.OutlinedButton
for a bordered look.ElevatedButton.icon()
for buttons with icons.
Use ListView
to display multiple widgets in a scrollable format:
ListView(
padding: EdgeInsets.all(16),
children: [
Text("Hello, Flutter!"),
Icon(Icons.star, size: 50, color: Colors.yellow),
ElevatedButton(onPressed: () {}, child: Text("Click Me")),
],
)
Image.network(
"https://example.com/image.jpg",
)
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
child: Icon(Icons.add),
),
flutter doctor
flutter pub get
Press r
in the terminal while running the app or click the Hot Reload button in your IDE.
Press Ctrl + C
in the terminal.
This section provides detailed information on a To-Do list application implemented in Flutter.
- Add new tasks
- Mark tasks as completed
- Reorder tasks
- Save and retrieve tasks using Shared Preferences
This is the main screen of the application which includes:
- A header displaying "Simple list"
- A
TodoListScreen
widget containing the list of tasks
This screen is responsible for:
- Displaying a list of tasks
- Allowing tasks to be marked as completed
- Reordering tasks within their respective sections (Pending and Completed)
- Adding new tasks
- Saving and retrieving tasks from
SharedPreferences
The Task
class represents a task in the To-Do app. It contains:
title
: The task descriptionisCompleted
: A boolean value indicating completion statusindex
: Task order in the listid
: A unique identifier for each task
class Task {
String id;
String title;
bool isCompleted;
int index;
Task({
required this.title,
this.isCompleted = false,
required this.index,
}) : id = const Uuid().v4();
Map<String, dynamic> toMap() {
return {
'id': id,
'title': title,
'isCompleted': isCompleted,
'index': index,
};
}
factory Task.fromMap(Map<String, dynamic> map) {
return Task(
title: map['title'],
isCompleted: map['isCompleted'],
index: map['index'],
);
}
}
Run the following command in your terminal or command prompt:
flutter create todo_app
cd todo_app
Ensure you have a device/emulator running, then execute:
flutter run
The app uses SharedPreferences
to persist tasks. To save a task:
void saveToPrefs() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
Task task1 = _tasks[0];
Map<String, dynamic> task1Map = task1.toMap();
String task1String = jsonEncode(task1Map);
prefs.setString("task", task1String);
}
To retrieve a saved task:
void getTaskFromPrefs() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
String? task = prefs.getString("task");
Map<String, dynamic> taskMap = jsonDecode(task!);
Task taskFromMap = Task.fromMap(taskMap);
print(taskFromMap);
}
Reordering is implemented using ReorderableListView
.
ReorderableListView(
onReorder: (oldIndex, newIndex) {
setState(() {
final Task item = _tasks.removeAt(oldIndex);
_tasks.insert(newIndex, item);
});
},
)