Flutter Prgs
Flutter Prgs
1. Once the ZIP file is downloaded, extract it to a location of your choice. For example:
○ C:\src\flutter
2. Make sure the extraction path doesn’t have spaces (e.g., avoid paths like C:\Program Files\).
You need to add the flutter/bin directory to your system PATH environment variable so that you can run Flutter commands from any terminal
window.
1. Open a Command Prompt (press Win + R, type cmd, and press Enter).
2. This will check your environment and display any missing dependencies or issues. If everything is installed correctly, it should show
no issues or suggest fixes (such as installing Android Studio or configuring Android licenses).
7. Install Android Studio (For Android Development)
To develop for Android, you need to install Android Studio and the required SDKs:
Once you have Android Studio and all required components set up, run flutter doctor again to verify that the setup is complete. The output should
show that the Flutter SDK and Android tools are correctly configured.
2.
3.
4.
If you plan to use iOS development, you’ll need a macOS machine with Xcode installed. On Windows, however, you can only develop for
Android.
4o mini
Basic Dart Program:
import 'dart:io';
void main() {
print("enter two numbers: ");
int? x=int.parse(stdin.readLineSync()!);
int? y=int.parse(stdin.readLineSync()!);
int result;
double res;
print("MAIN MENU");
print("1. ADD");
print("2. SUBTRACTION");
print("3. MULTIPLICATION");
print("4. DIVISION");
print("5. EXIT");
print("enter your choice");
int? ch=int.parse(stdin.readLineSync()!);
while(ch!=5) {
switch (ch) {
case 1:
result = x + y;
print("the sum is $result");
break;
case 2:
result = x - y;
print("the difference is $result");
break;
case 3:
result = x * y;
print("the product is $result");
break;
case 4:
res = (x / y);
print("the division is $res");
break;
case 5:
break;
}
print("enter your choice");
ch=int.parse(stdin.readLineSync()!);
}
}
2) Explore various flutter widgets
Text Widget:
import 'package:flutter/material.dart';
void main()
{
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
//const ({super.key});
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
backgroundColor: Colors.cyan,
body: new Container(
child: Padding(
padding: EdgeInsets.all(15.0),
child: Center(
child: Text(
'This is a text widget demo',
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.pink,
fontSize: 50,
),
),
),
),
),
),
);
}
}
Image Widget:
import 'package:flutter/material.dart';
void main()
{
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar:AppBar(
title: const Text(' Image Widget Demo',),
),
body: Center(
child: Column(
children:<Widget>[
Image.asset('assets/image/flutter_image.jpg', height: 400,scale:
2.5)], ), //Column
),
),
);
}
}
Container Widget:
import 'package:flutter/material.dart';
void main()
{
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Container(
padding: const EdgeInsets.all(150.0),
margin: const EdgeInsets.all(30.0),
decoration: BoxDecoration(
color: Colors.cyanAccent,
border: Border.all(width: 20, color: Colors.amber),
borderRadius: const BorderRadius.all(Radius.circular(10)),
),
child: const Text('Container Widget Demo'),
),
),
);
}
}
Row Widget:
import 'package:flutter/material.dart';
void main()
{
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(
"Row Widget Example"
),
),
body: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children:<Widget> [
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.cyan,
),
child: Text(
"Flutter",
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
),
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.cyan,
),
child: Text(
"Devops",
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
),
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.cyan,
),
child: Text(
"Azure",
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
),
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.cyan,
),
child: Text(
"Cyber Security",
style: TextStyle(
color: Colors.white,
fontSize: 30,
),
),
),
],
),
),
);
}
}
Column Widget:
void main()
{
runApp(new MyApp());
}
MyApp.dart-----------------file
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(
"Column Widget Example"
),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children:<Widget> [
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.amber,
),
child: Text(
"Flutter",
style: TextStyle(
color: Colors.blue,
fontSize: 30,
),
),
),
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.amber,
),
child: Text(
"Devops",
style: TextStyle(
color: Colors.blue,
fontSize: 30,
),
),
),
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.amber,
),
child: Text(
"Azure",
style: TextStyle(
color: Colors.blue,
fontSize: 30,
),
),
),
Container(
margin: EdgeInsets.all(20.0),
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
color: Colors.amber,
),
child: Text(
"Cyber Security",
style: TextStyle(
color: Colors.blue,
fontSize: 30,
),
),
),
],
),
),
);
}
}
Stack Widget
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Stack Widget'),
backgroundColor: Colors.greenAccent[400],
), //AppBar
body: Center(
child: SizedBox(
width: 300,
height: 300,
child: Center(
child: Stack(
children: <Widget>[
Container(
width: 300,
height: 300,
color: Colors.red,
), //Container
Container(
width: 250,
height: 250,
color: Colors.black,
), //Container
Container(
height: 200,
width: 200,
color: Colors.purple,
),
Container(
width: 150,
height: 150,
color: Colors.amber,
),
Container(
width: 100,
height: 100,
color: Colors.indigoAccent,
)//Container
], //<Widget>[]
), //Stack
), //Center
), //SizedBox
) //Center
) //Scaffold
) //MaterialApp
);
}
3. a) Design a responsive UI that adapts to different screen sizes
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
void main() {
runApp(MyApp());
}
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
1. Stateless Widgets: Widgets that do not have any mutable state. Once
built, they do not change their state during the lifetime of the widget.
2. Stateful Widgets: Widgets that have mutable state. The state of the
widget can change during its lifetime, and the widget can be rebuilt to
reflect those changes.
Stateless Widgets
A StatelessWidget is a widget that doesn’t maintain any state or change over
time. Once created, the state of a StatelessWidget remains constant and is
immutable. A StatelessWidget can only depend on the information it receives
during its creation (i.e., parameters).
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
Stateful Widgets
A StatefulWidget is a widget that can change its state over time. Stateful
widgets are used when the UI needs to update based on user interactions, data
changes, or other factors. A StatefulWidget consists of two classes:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
return MaterialApp(
home: CounterPage(),
);
@override
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
@override
return Scaffold(
appBar: AppBar(
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Counter Value:',
),
Text(
'$_counter',
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _incrementCounter,
),
],
),
),
);
}
b) implement state management using set state and provider
The setState() method is used to notify Flutter that the state of a widget has
changed and it needs to rebuild itself. This is the simplest state management
method and is ideal for situations where the state is only used within one
widget.
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: CounterPage(),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('State Management with setState()')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Counter: $_counter',
style: TextStyle(fontSize: 36),
),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Increment'),
),
],
),
),
);
}
}
Explanation:
● The counter's state is stored in the _counter variable, and when the user
clicks the "Increment" button, the _incrementCounter() method is called.
● setState() triggers a rebuild of the CounterPage widget and updates the
displayed counter value.
● This approach works well for simple scenarios with localized state but
can become cumbersome for larger apps with more complex state changes.
2. State Management with Provider
Provider is a package that allows you to manage state across your entire app or
in specific parts of the widget tree. It separates the business logic from UI
code and makes it easier to manage app-wide state.
dependencies:
flutter:
sdk: flutter
provider: ^6.0.0
ChangeNotifier is the base class provided by the provider package to hold and
manage state. Any widget that listens to it will be rebuilt when the state
changes.
import 'package:flutter/material.dart';
int _counter = 0;
void increment() {
_counter++;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(
ChangeNotifierProvider(
child: MyApp(),
),
);
@override
return MaterialApp(
home: CounterPage(),
);
Use Consumer or Provider.of to access and listen to the CounterModel. This will
allow you to rebuild the widget when the state changes.
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Consumer<CounterModel>(
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Counter: ${counterModel.counter}',
),
ElevatedButton(
onPressed: () {
counterModel.increment();
},
child: Text('Increment'),
),
],
);
},
),
),
);
}
Explanation:
State Management Suitable for small apps Suitable for large apps with
Complexity or simple state complex state management
Conclusion:
Both methods have their use cases, and you can choose the one that best fits
the complexity and scale of your app.
Here, I’ll show how to create some custom widgets for specific UI
elements in Flutter, such as a custom button, card, text field, and a
loading spinner.
import 'package:flutter/material.dart';
@override
return ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
),
),
child: Text(
text,
),
);
Usage:
CustomButton(
text: "Click Me",
onPressed: () {
print("Button Pressed!");
},
CustomCard({
required this.title,
required this.description,
required this.icon,
});
@override
return Card(
elevation: 4.0,
shape: RoundedRectangleBorder(
),
child: Padding(
child: Row(
children: [
Icon(
icon,
size: 40,
color: Colors.blue,
),
SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title,
),
SizedBox(height: 8),
Text(
description,
),
],
),
),
],
),
),
);
Usage:
CustomCard(
icon: Icons.info,
CustomTextField({
required this.hintText,
required this.controller,
this.inputType = TextInputType.text,
this.obscureText = false,
});
@override
return TextField(
controller: controller,
keyboardType: inputType,
obscureText: obscureText,
decoration: InputDecoration(
labelText: hintText,
border: OutlineInputBorder(
),
),
);
Usage:
CustomTextField(
hintText: "Enter your username",
controller: _controller,
CustomLoader({this.message = "Loading..."});
@override
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 16),
Text(
message,
),
],
),
);
}
Usage:
@override
return CircleAvatar(
radius: radius,
},
);
}
Usage:
CustomAvatar(imageUrl: "https://example.com/avatar.jpg")