Flutter Tutorial
Flutter Tutorial
I’ll walk you through the basics of Flutter from the ground up. By the end, you'll
have a solid understanding of Flutter’s core concepts and be ready to start
building your own apps. Here’s a simple and structured guide:
1. What is Flutter?
Flutter is a UI toolkit developed by Google to build natively compiled
applications for mobile, web, and desktop from a single codebase. It uses the
Dart programming language.
2. Setting Up Flutter
Before coding, let’s set up the Flutter environment.
Add the Flutter bin directory to your system’s PATH to access the flutter
2. Set Up IDE:
Install the Flutter and Dart extensions in your IDE for a smooth coding
experience.
3. Check Setup:
bash
Copy code
flutter doctor
Flutter 1
This checks if Flutter is set up correctly and tells you what’s missing
(like SDKs, device setup, etc.).
Widgets
Flutter is all about widgets. Everything in Flutter is a widget, from the layout
(columns, rows) to UI elements (buttons, text).
There are two types of widgets:
dart
Copy code
import 'package:flutter/material.dart';
Flutter 2
void main() {
runApp(MyApp());
}
Center: Centers the child widget (in this case, the text).
Flutter 3
Step-by-step code for adding a button:
1. Replace the MyApp widget with this StatefulWidget:
dart
Copy code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
void _changeText() {
setState(() {
displayText = "You pressed the button!";
});
}
Flutter 4
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
displayText,
style: TextStyle(fontSize: 24),
),
SizedBox(height: 20), // Adds spacing between
widgets
ElevatedButton(
onPressed: _changeText,
child: Text('Press me'),
),
],
),
);
}
}
when pressed.
Flutter 5
Column: Arranges widgets vertically.
Example:
dart
Copy code
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Hello, World!'),
Text('Welcome to Flutter!'),
],
)
Example:
dart
Copy code
Container(
padding: EdgeInsets.all(16.0),
color: Colors.blue,
child: Text('Hello with Padding'),
)
SizedBox
SizedBox: Creates a box with a specific size.
Example:
dart
Copy code
Flutter 6
SizedBox(height: 20) // Adds vertical spacing
dart
Copy code
import 'package:flutter/material.dart';
dart
Copy code
import 'package:flutter/material.dart';
import 'second_screen.dart'; // Import the new screen
void main() {
runApp(MyApp());
}
Flutter 7
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Navigation Example'),
),
body: MyHomePage(),
),
);
}
}
Explanation:
Navigator.push(): Pushes a new route (or screen) on the navigation stack.
Flutter 8
9. Understanding pubspec.yaml and Dependencies
In pubspec.yaml , you can:
Define app name, version, and assets (like images and fonts).
yaml
Copy code
dependencies:
flutter:
sdk: flutter
http: latest_version
2. Run:
bash
Copy code
flutter pub get
2. Handling User Input: Learn about forms and user input using TextField and
Form widgets.
3. APIs and HTTP: Fetch data from the internet using the http package.
Flutter 9
That's a simple guide to get you started on Flutter. Let me know if you want to
dive deeper into any of these topics!
Now that you've mastered the basics of Flutter and have started building apps,
let's take your skills to the intermediate level. This guide will help you go
deeper into Flutter concepts, introduce important architectural patterns,
advanced widgets, and more. Here’s a detailed breakdown of next steps to
become an intermediate Flutter developer.
yaml
Copy code
Flutter 10
dependencies:
provider: latest_version
dart
Copy code
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(
ChangeNotifierProvider(
create: (context) => Counter(),
child: MyApp(),
),
);
}
Flutter 11
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many time
s:',
),
Text(
'${Provider.of<Counter>(context).count}',
style: Theme.of(context).textTheme.headlin
e4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Provider.of<Counter>(context, listen: false).i
ncrement();
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
void increment() {
_count++;
notifyListeners(); // Notifies widgets to rebuild
}
}
Flutter 12
Explanation:
Provider allows you to share state between widgets without passing the
state manually.
ChangeNotifier is a class that your model (state) can extend, allowing the
state to notify its listeners when something changes.
dart
Copy code
import 'package:flutter/material.dart';
Flutter 13
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Screen'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/second');
},
child: Text('Go to Second Screen'),
),
),
);
}
}
2. Explanation:
Flutter 14
Deep Linking:
To handle URLs that open your app at specific screens, you need to work with
deep links. Here’s a guide on deep linking in Flutter.
yaml
Copy code
dependencies:
http: latest_version
dart
Copy code
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
Flutter 15
@override
_JsonDataScreenState createState() => _JsonDataScreenS
tate();
}
if (response.statusCode == 200) {
setState(() {
data = json.decode(response.body);
});
} else {
throw Exception('Failed to load data');
}
}
@override
void initState() {
super.initState();
fetchData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Fetch JSON Data'),
),
body: data == null
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: data.length,
Flutter 16
itemBuilder: (context, index) {
return ListTile(
title: Text(data[index]['title']),
subtitle: Text(data[index]['body']),
);
},
),
);
}
}
Explanation:
The app fetches a list of posts from an API
( https://jsonplaceholder.typicode.com/posts ).
dart
Copy code
import 'package:flutter/material.dart';
Flutter 17
home: FormScreen(),
);
}
}
String _email;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Form Validation'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(labelText:
'Email'),
validator: (value) {
if (value.isEmpty) {
return 'Please enter an email';
} else if (!RegExp(r'^[^@]+@[^@]+\.[^
@]+').hasMatch(value)) {
return 'Enter a valid email';
}
return null;
Flutter 18
},
onSaved: (value) {
_email = value;
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (_formKey.currentState.validate())
{
_formKey.currentState.save();
ScaffoldMessenger.of(context).showSn
ackBar(SnackBar(
content: Text('Form Submitted: $_e
mail'),
));
}
},
child: Text('Submit'),
),
],
),
),
),
);
}
}
Explanation:
Form: Holds a group of form fields. It needs a GlobalKey to identify the form.
validator: Checks
Flutter 19