Android projects
Android projects
Code:-
void main() {
${performOperation(10, 5, divide)}');
1
A60205222153
Output:-
Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 1
Power: 8
2
A60205222153
Code:-
void main() {
Function makeCounter()
{ int count = 0;
return ()
{ count += 1;
return count;
};
}
Output:-
1
3
A60205222153
3. Write a Dart function sumList that takes a list of integers as a parameter and
returns the sum of all elements in the list. Call the function with different
lists and print the results.
Code:-
void main() {
int sumList(List<int> numbers)
{ int sum = 0;
for (int number in numbers)
{ sum += number;
}
return sum;
}
// Test cases
List<int> list1 = [1, 2, 3, 4, 5];
List<int> list2 = [10, 20, 30, 40];
List<int> list3 = [];
Output:-
15
100
0
4
A60205222153
4. Create a class ShoppingList that has a final list property called items. Initialize
the list with a set of grocery items. Write a method to add more items to the
list and display all items. What happens if you try to reassign the items
property to a new list?
Code:-
class ShoppingList
{ final List<String>
items;
void displayItems()
{ print('Shopping List:');
for (var item in items) {
print(item);
}
}
}
void main() {
var shoppingList = ShoppingList();
shoppingList.displayItems();
shoppingList.addItem('Cheese');
shoppingList.addItem('Tomatoes');
shoppingList.displayItems();
}
5
A60205222153
Output:-
Shopping List:
Milk
Eggs
Bread Butter
Shopping List:
Milk
Eggs
Bread
Butter
Cheese
Tomatoes
6
A60205222153
5. Create a class Playlist that has a list property song (list of strings).
Include methods to:
Get the total number of songs. Display all songs in the playlist.
Code:-
class Playlist
{ List<String> songs;
int totalSongs()
{ return
songs.length;
}
void displaySongs() {
if (songs.isEmpty) {
print('The playlist is empty.');
} else {
7
A60205222153
print('Playlist:');
for (var song in songs) {
print('- $song');
}
}
}
}
playlist.displaySongs();
playlist.displaySongs();
playlist.displaySongs();
print('Total Songs: ${playlist.totalSongs()}');
}
8
A60205222153
Output:-
9
A60205222153
10
A60205222153
11
A60205222153
Output:-
1. Positional + Optional Named
Value 1: 10
Value 2: 20
2. Positional + Optional
Value 1: 10
Value 2: Not Provided
3. Positional + Default
Value 1: 10
Value 2: 100
12
A60205222153
Code:-
class Employee
{ int _id;
String _name; double
_salary;
void displayInfo()
{ print('Employee Details:');
print('ID: $_id');
13
A60205222153
print('Name: $_name');
print('Salary: \$$_salary');
}
}
void main() {
var emp = Employee(1, 'John Doe', 50000);
emp.displayInfo();
emp.id = 2;
emp.name = 'Jane Smith';
emp.salary = 60000;
emp.displayInfo();
emp.displayInfo();
}
14
A60205222153
Output:-
Employee Details:
ID: 1
Salary: $50000.0
Salary: $60000.0
Salary: $60000.0
15
A60205222153
8. Create “Hello World” application. That will display “Hello World” in the
middle of the screen using TextView Widget in the red color, use style property
of Text Widget.
Code:-
import 'package:flutter/material.dart';
void main() {
runApp(HelloWorldApp());
@override
{ return MaterialApp(
home: Scaffold(
body:
Center( child:
Text( 'Hello
World', style:
TextStyle(
color: Colors.red,
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
16
A60205222153
)}});
17
A60205222153
Output:-
18
A60205222153
➢ A Text widget with the text "Top-left" aligned to the top left.
➢ A Text widget with the text "Bottom-right" aligned to the bottom right.
Code:-
import 'package:flutter/material.dart';
void main() {
runApp(ContainerApp());
@override
{ return MaterialApp(
home:
Scaffold( appBar:
AppBar(
),
19
A60205222153
Container( width:
11
0
A60205222153
color: Colors.green,
child: Stack(
children: [
Align(
alignment: Alignment.topLeft,
child: Text(
'Top-left',
),
),
Align(
alignment: Alignment.bottomRight,
child: Text(
'Bottom-right',
),
),
Icons.star,
color: Colors.white,
size: 50,
),),
],
11
1
A60205222153
),),),),);
Output:-
20
A60205222153
10. Create sample application with login module. (Check username and
password) On successful login, Change TextView “Login Successful”. And on
failing login, alert user using SnackBar “Login fail”.
Code:-
import 'package:flutter/material.dart';
void main() {
runApp(LoginApp());
@override
{ return MaterialApp(
home: LoginScreen(),
);
@override
21
A60205222153
void login() {
// Successful login
ScaffoldMessenger.of(context).showSnackBar( Sna
ckBar(
backgroundColor: Colors.green,
),
);
} else {
// Failed login
ScaffoldMessenger.of(context).showSnackBar( Sna
ckBar(
backgroundColor: Colors.red,
),
);
22
A60205222153
@override
{ return Scaffold(
appBar: AppBar(
),
body: Padding(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: usernameController,
decoration:
InputDecoration( labelText:
'Username',
border: OutlineInputBorder(),
),
),
controller: passwordController,
obscureText: true,
decoration:
23
A60205222153
InputDecoration( labelText:
'Password',
24
A60205222153
border: OutlineInputBorder(),
),
),
SizedBox(height: 20),
ElevatedButton( onPr
Text('Login'),
),
),
],
),
),
);
25
A60205222153
Output:-
26
A60205222153
11. Create login application where you will have to validate usename and
passwords till the username and password is not validated, login button should
remain disabled. Code:- import 'package:flutter/material.dart'; void main() {
runApp(LoginApp());
@override
{ return MaterialApp(
home: LoginScreen(),
);
@override
void _validateInputs() {
password = passwordController.text;
27
A60205222153
setState(() {
});
void login() {
// Successful login
ScaffoldMessenger.of(context).showSnackBar( Sna
ckBar(
content:
Text('Login Successful'),
backgroundColor: Colors.green,
),
);
} else {
// Failed login
ScaffoldMessenger.of(context).showSnackBar( Sna
ckBar(
backgroundColor: Colors.red,
),
28
A60205222153
);
@override
{ return Scaffold(
appBar: AppBar(
),
body: Padding(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: usernameController,
decoration:
InputDecoration( labelText:
'Username',
border: OutlineInputBorder(),
),
),
controller: passwordController,
obscureText: true,
decoration:
InputDecoration( labelText:
'Password',
border: OutlineInputBorder(),
),
),
SizedBox(height: 20),
ElevatedButton(
child: Text('Login'),
style: ElevatedButton.styleFrom(
),
),
],
),
21
0
A60205222153
Output:
30
A60205222153
➢ The app should display a list of items (e.g., a shopping list or task list) in the
main body.
➢ If the user taps "Add" after entering text, the new item should be added to
the list.
➢ If the user taps "Cancel" or dismisses the dialog, no changes should be made
to the list.
Code:-
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
{ return MaterialApp(
31
A60205222153
home: ShoppingListScreen(),
);
@override
{ setState(() { items.add(item);
});
{ setState(() {
items.removeAt(index);
});
void _showAddItemDialog() {
showDialog(
32
A60205222153
context: context,
{ return AlertDialog(
content:
TextField( controller:
textController,
),
actions: <Widget>[
TextButton( onPresse
d: () {
Navigator.of(context).pop();
},
child: Text('Cancel'),
),
TextButton( onPr
essed: () {
(newItem.isNotEmpty) {
_addItem(newItem);
Navigator.of(context).pop();
33
A60205222153
},
34
A60205222153
child: Text('Add'),
),
],
);
},
);
showDialog(
context: context,
{ return AlertDialog(
actions: <Widget>[
TextButton( onPr
essed: () {
Navigator.of(context).pop();
},
child: Text('Cancel'),
),
35
A60205222153
TextButton( onPr
essed: () {
_removeItem(index);
Navigator.of(context).pop();
},
child: Text('Delete'),
),
],
);
},
);
@override
{ return Scaffold(
appBar: AppBar(
),
body:
ListView.builder( itemCount:
items.length, itemBuilder:
ListTile(
36
A60205222153
title: Text(items[index]),
onLongPress: () {
_showDeleteItemDialog(index);
}, );
},
),
floatingActionButton: FloatingActionButton(
onPressed:
_showAddItemDialog,
child: Icon(Icons.add),
),);
Output:
37
A60205222153
13. Create a flutter application to add custom fonts, image (using image
network, image asset) and theme in your app (write the code of pubspec.yaml
also the steps included+ for fonts and images).
Code:-
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
{ return MaterialApp(
theme:
ThemeData( primaryColor:
Colors.blue, textTheme:
TextTheme( bodyText1:
TextStyle( fontFamily:
),
),
),
home: MyHomePage(),
);
38
A60205222153
@override
),
body:
Center( child:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.network(
'https://www.example.com/image.jpg',
width: 100,
height: 100,
),
Text(
TextStyle( fontFamily:
'CustomFont', fontSize:
24,
39
A60205222153
color: Colors.blue,
31
0
A60205222153
),
),
],
),
),
);
Output:-
31
1
A60205222153
14. Create an application that will pass two number using Text View to the next
screen, and on the next screen display sum of that number.
Code:-
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
{ return MaterialApp(
home: InputScreen(),
);
@override
@override
{ return Scaffold(
body: Padding(
child: Column(
children: [
TextField(
controller: num1Controller,
keyboardType: TextInputType.number,
decoration:
number',
border: OutlineInputBorder(),
),
),
SizedBox(height: 16),
TextField(
controller: num2Controller,
keyboardType: TextInputType.number,
decoration: InputDecoration(
41
A60205222153
border: OutlineInputBorder(),
),
),
SizedBox(height: 16),
ElevatedButton( onPres
sed: () {
Navigator.push(
context,
MaterialPageRoute(
),
);
},
),
],
),
),
);
42
A60205222153
@override
Scaffold(
body: Center(
child: Text(
textAlign: TextAlign.center,
),
),
);
43
A60205222153
Output:-
44
A60205222153
15. Create a Flutter app with two screens: Screen 1 has a list of three buttons
labeled "Button1", "Button2", and "Button3". When a button is clicked, the app
navigates to Screen 2 using Navigator.push() with MaterialPageRoute and
passes the item name (e.g., "name1") as a parameter. Screen 2: Displays the
item name passed from the first screen and has a back button to return to
Screen 1.
Code:-
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
{ return MaterialApp(
home: InputScreen(),
);
@override
45
A60205222153
@override
{ return Scaffold(
body: Padding(
child: Column(
children: [
TextField(
controller: num1Controller,
keyboardType: TextInputType.number,
decoration:
number',
border: OutlineInputBorder(),
),
),
SizedBox(height: 16),
TextField(
46
A60205222153
controller: num2Controller,
keyboardType:
TextInputType.number,
decoration:
InputDecoration( labelText:
OutlineInputBorder(),
),
),
SizedBox(height: 16),
ElevatedButton( onPres
sed: () {
Navigator.push(
context,
MaterialPageRoute(
),
);
},
),
],
47
A60205222153
),
),
);
@override
return Scaffold(
body: Center(
child: Text(
textAlign: TextAlign.center,
),
),);
48
A60205222153
Output:-
49
A60205222153
16. Create an application where users can add, delete, and mark tasks as
Code:-
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
runApp(MyApp());
@override
{ return MaterialApp(
home: TodoListScreen(),
);
@override
50
A60205222153
@override
void initState() {
super.initState();
_loadTasks();
if (tasksString != null)
{ setState(() {
});
51
A60205222153
{ setState(() {
});
_saveTasks();
{ setState(() {
_tasks.removeAt(index);
});
_saveTasks();
{ setState(() {
_tasks[index]['completed'] = !_tasks[index]['completed'];
});
_saveTasks();
@override
{ return Scaffold(
appBar: AppBar(
52
A60205222153
),
body:
Column( children
: [ Padding(
child: Row(
children: [
Expanded( child
: TextField(
controller: _taskController,
decoration:
InputDecoration( labelText:
OutlineInputBorder(),
),
),
),
onPressed: () {
if (_taskController.text.isNotEmpty) {
_addTask(_taskController.text);
_taskController.clear();
}
53
A60205222153
},
child: Text('Add'),
),
],
),
),
Expanded(
child:
ListView.builder( itemCount:
_tasks.length, itemBuilder:
title: Text(
task['name'],
style: TextStyle(
decoration: task['completed']
? TextDecoration.lineThrough
: TextDecoration.none,
),
),
leading:
Checkbox( value:
task['completed'],
54
A60205222153
_toggleTaskCompletion(index),
),
trailing:
IconButton( icon:
Icon(Icons.delete),
),);
},
),),],),);
Output:-
55
A60205222153
17. Write a program to fetch remote data from a News API using the http
package and display the news headlines in a ListView.
Code:-
import 'package:flutter/material.dart';
import 'dart:convert';
void main() {
runApp(MyApp());
@override
{ return MaterialApp(
home: NewsListScreen(),
);
@override
56
A60205222153
@override
_fetchNews();
'https://newsapi.org/v2/topheadlines?country=us&apiKey=YOUR_API_KEY'; //
Replace YOUR_API_KEY with a valid key
try {
if (response.statusCode == 200) {
setState(() {
_newsHeadlines = data['articles'];
_isLoading = false;
});
} else {
} catch (e) {
setState(() {
57
A60205222153
_isLoading = false;
});
print('Error: $e');
@override
{ return Scaffold(
appBar: AppBar(
),
body: _isLoading
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: _newsHeadlines.length,
return ListTile(
onTap: () {
if (newsItem['url'] != null) {
_openNewsUrl(context, newsItem['url']);
58
A60205222153
},
);
},
),
);
showDialog(
context: context,
Link'),
actions: [
TextButton(
child: Text('Close'),
),
],
)}});
59
A60205222153
Output:-
60