Flutter For Beginners
Flutter For Beginners
b) Implement different layout structures using Row, Column, and Stack widgets.
TEXT BOOK:
1. Marco L. Napoli, Beginning Flutter: A Hands-on Guide to App Development
1.a) Install Flutter and Dart SDK.
Ans) Dart SDK is a pre-compiled version so we have to download and extract it
only. For this follow the below-given instructions: Step 1: Download Dart SDK.
Download Dart SDK from the Dart SDK archive page. The URL
is: https://dart.dev/tools/sdk/archive
Click on DART SDK to download SDK for Windows 64-Bit Architecture. The
download will start and a zip file will be downloaded. Note: To download SDK for
any other OS select OS of your choice. Step 2: Extract the downloaded zip file.
Extract the contents of downloaded zip file and after extracting contents of zip file
will be as shown:
Step 3: Running Dart. Now open bin folder and type “cmd” as given below:
Command Prompt will open with our desired path of bin folder and now type dart”.
And now we are ready to use dart through bin folder but setting up the path in environment variables
will ease our task of Step3 and we can run dart from anywhere in the file system using command
prompt.
Step 4: Setting up path in environment variables. Open Environment Variables from
advanced system settings and add Path in System Variables as depicted in image:
Now we are done to use Dart from anywhere in the file system.
void main() {
int num1 = 10; //declaring number1
int num2 = 3; //declaring number2
// Calculation
int sum = num1 + num2;
int diff = num1 - num2;
int mul = num1 * num2;
double div = num1 / num2; // It is double because it outputs number with
decimal.
Output:
The sum is 13
The diff is 7
The mul is 30
The div is 3.3333333333333335
Output: When we run this application in the emulator or device, we should get the UI similar to the
below screenshot:
Image Widget:
import 'package:flutter/material.dart';
Output:
Container Widget:
import 'package:flutter/material.dart';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyContainerWidget(),
);
}
}
Output:
Row widgets:
import 'package:flutter/material.dart';
Output:
When we run this app, we should get the UI as the below screenshot.
Column widgets:
import 'package:flutter/material.dart';
void main() { runApp(MyApp()); }
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage()
);
}
}
Stack Widget:
The below code explains how to use the stack widget in Flutter. In this code, we are going to try most
of the essential attributes of the stack widget.
import 'package:flutter/material.dart';
home: MyStackWidget(),
);
}
}
Positioned(
top: 30,
left: 20,
child: Container(
height: 100,
width: 150,
color: Colors.orange,
child: Center(
child: Text(
'Bottom Widget',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
)
),
],
),
)
),
);
}
}
Output:
When we run the app, we should get the UI of the screen similar to the below screenshot:
3.a) Implement Design a responsive UI that adapts to different screen sizes Flutter.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Responsive UI Example',
home: ResponsiveHome(),
);
}
}
Output:
Visual Layout
+---------------------+
| Responsive UI | <- App Bar
+---------------------+
| |
| [ Image ] | <- Smaller image
| |
| Welcome to | <- Smaller text
| Responsive Flutter |
| UI! |
| |
| [Click Me] | <- Smaller button
+---------------------+
+---------------------+
| Responsive UI | <- App Bar
+---------------------+
| |
| [ Image ] | <- Larger image
| |
| Welcome to | <- Larger text
| Responsive Flutter |
| UI! |
| |
| [ Click Me ] | <- Larger button
+---------------------+
return Scaffold(
appBar: AppBar(title: Text('Responsive UI')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// Header
Container(
width: double.infinity,
padding: EdgeInsets.all(16),
color: Colors.blueAccent,
child: Text(
'Responsive Header',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: isDesktop ? 28 : isTablet ? 24 : 20,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
SizedBox(height: 20),
// Content Area
Expanded(
child: Row(
children: [
// Left Column
Expanded(
child: Column(
children: [
Container(
margin: EdgeInsets.symmetric(vertical: 10),
height: isDesktop ? 200 : isTablet ? 160 : 120,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage('https://via.placeholder.com/300'),
fit: BoxFit.cover,
),
borderRadius: BorderRadius.circular(10),
),
),
Text(
'Image 1',
style: TextStyle(fontSize: isDesktop ? 18 : isTablet ? 16 : 14),
),
SizedBox(height: 20),
Container(
margin: EdgeInsets.symmetric(vertical: 10),
height: isDesktop ? 200 : isTablet ? 160 : 120,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage('https://via.placeholder.com/300'),
fit: BoxFit.cover,
),
borderRadius: BorderRadius.circular(10),
),
),
Text(
'Image 2',
style: TextStyle(fontSize: isDesktop ? 18 : isTablet ? 16 : 14),
),
],
),
),
+---------------------------+
+---------------------------+
+---------------------------+
| [ Image 1 ] |
| |
| [ Image 2 ] |
| Image 2 |
+---------------------------+
| [ Button 1 ]|
| [ Button 2 ]|
+---------------------------+
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
return MaterialApp(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
// Home Screen
@override
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
);
},
),
),
);
}
}
// Second Screen
@override
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
);
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
),
],
),
),
);
// Third Screen
@override
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
},
),
],
),
),
);
Home Screen:
+---------------------------+
| Home Screen | <- App Bar
+---------------------------+
| |
| [ Go to Second Screen ] | <- Center button
| |
+---------------------------+
Second Screen:
+---------------------------+
| Second Screen | <- App Bar
+---------------------------+
| |
| This is the Second Screen! |
| |
| [ Go to Third Screen ] | <- Button to Third Screen
| |
| [ Go Back to Home Screen ]| <- Button to Home Screen
| |
+---------------------------+
Third Screen:
+---------------------------+
| Third Screen | <- App Bar
+---------------------------+
| |
| This is the Third Screen! |
| |
| [ Go Back to Second Screen ]| <- Button to Second Screen
| |
| [ Go Back to Home Screen ]| <- Button to Home Screen
| |
+---------------------------+
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {
},
);
// Home Screen
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/second');
},
),
),
);
// Second Screen
@override
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/third');
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
),
],
),
),
);
// Third Screen
@override
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
},
),
],
),
),
);
+---------------------------+
| Second Screen | <- App Bar
+---------------------------+
| |
| This is the Second Screen! |
| |
| [ Go to Third Screen ] | <- Button to Third Screen
| |
| [ Go Back to Home Screen ]| <- Button to Home Screen
| |
+---------------------------+
Third Screen:
+---------------------------+
| Third Screen | <- App Bar
+---------------------------+
| |
| This is the Third Screen! |
| |
| [ Go Back to Second Screen ]| <- Button to Second Screen
| |
| [ Go Back to Home Screen ]| <- Button to Home Screen
| |
+---------------------------+
import 'package:flutter/material.dart';
class MyStatefulWidget extends StatefulWidget {
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stateful Widget Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Output for Stateful Widget:
+---------------------------+
| Stateful Widget Example | <- App Bar
+---------------------------+
| |
| You have pushed the button |
| this many times: |
| 0 | <- Counter
| |
+---------------------------+
| + | <- Floating Action Button
+---------------------------+
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SetState Example',
home: CounterScreen(),
);
}
}
class CounterScreen extends StatefulWidget {
@override
_CounterScreenState createState() => _CounterScreenState();
}
Output:
After pressing the button once:
Counter Value: 1
After pressing it three times:
Counter Value: 3
void increment() {
_counter++;
notifyListeners(); // Notifies listeners to rebuild
}
}
class CounterScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Counter using Provider')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Counter Value: ${Provider.of<Counter>(context).counter}'),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
Provider.of<Counter>(context, listen: false).increment();
},
child: Text('Increment'),
),
],
),
),
);
}
}
Output:
The Provider package is a powerful state management solution for larger applications.
First, you need to add the Provider package to your pubspec.yaml:
dependencies:
flutter:
sdk: flutter
provider: ^6.0.0 # Check for the latest version.