Ai Lab
Ai Lab
AIM:
This application is to create a simple Flutter app that displays a list of items. This will help in
understanding the basic concepts of Flutter, including widget structure, state management,
and layout design. By following this guide, you will learn how to set up a Flutter
environment, create a new Flutter project, and implement a basic user interface.
Algorithm
1. Setup Environment:
o Install Flutter SDK and Android Studio.
o Install Flutter and Dart plugins in Android Studio.
2. Create New Flutter Project:
o Open Android Studio and create a new Flutter project named flutter_basic_app.
3. Design Application Structure:
o Create a main widget that runs the application.
o Define a stateless widget for the home screen that includes a ListView to display
items.
4. Implement the User Interface:
o Use a Scaffold widget for the app layout, including an AppBar.
o Create a list of items to display using ListView.builder().
5. Run the Application:
o Connect a physical device or start an Android emulator.
o Build and run the application to see the output.
Implementation
Replace the contents of the lib/main.dart file with the following code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
return MaterialApp(
title: 'Flutter Basic App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Basic Flutter App'),
),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(items[index]),
);
},
),
);
}
}
1. Connect a Device:
o Connect an Android device or start an emulator.
2. Run the App:
o Click the green "Run" button in Android Studio or press Shift + F10.
o Wait for the application to build and launch.
Output:
Basic Flutter App
Creating this simple application, you have laid the groundwork for further exploration into Flutter's
capabilities, such as handling user input, navigation, animations, and more complex state
management solutions. This foundational knowledge will be beneficial as you progress in developing
more advanced Flutter applications.
6. Develop a Flutter App by applying the Widgets, layouts and user management
Aim
To develop a Flutter application that demonstrates the use of various widgets, layout management
techniques, and basic user management (e.g., login functionality).
Algorithm
Code Overview
1. Login Page:
o Text fields for username and password.
o A button to simulate login functionality.
2. Home Page:
o Displays a welcome message for the user.
3. Navigation:
o Switch from the login page to the home page upon successful login.
Code Implementation
lib/main.dart:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
return MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginPage(),
);
}
@override
void _login() {
Navigator.push(
context,
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
);
@override
return Scaffold(
body: Padding(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: _usernameController,
),
TextField(
controller: _passwordController,
obscureText: true,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _login,
child: Text('Login'),
),
],
),
),
);
HomePage({required this.username});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(
'Welcome, $username!',
),
),
);
OUTPUT:
Result
Aim
To develop a Flutter application that demonstrates navigation between multiple screens using
Flutter's Navigator widget.
Algorithm
1. Main Screen:
2. Second Screen:
lib/main.dart:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
Result
Aim
To create a Flutter application that imports and uses an external library (intl) to format and display
the current date and time in a user-friendly format.
Algorithm
dependencies:
flutter:
sdk: flutter
The application will display the current date and time formatted using the intl package.
lib/main.dart:
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
return MaterialApp(
debugShowCheckedModeBanner: false,
home: DateTimeScreen(),
);
@override
return Scaffold(
appBar: AppBar(
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Current Date:',
),
Text(
formattedDate,
),
SizedBox(height: 20),
Text(
'Current Time:',
),
Text(
formattedTime,
),
],
),
),
);
Initial Screen:
Displays the current date and time formatted using the intl library.
Example Output :
Current Date:
Current Time:
2:45 PM
Result
Aim
To develop a Flutter application that fetches JSON data from an API, parses it, and displays the data
in a list.
Algorithm
1. Test the app on an emulator or device to verify the data is displayed correctly.
Code
This app fetches and displays a list of posts from a public JSON API.
pubspec.yaml:
dependencies:
flutter:
sdk: flutter
lib/main.dart:
import 'package:flutter/material.dart';
import 'dart:convert';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
return MaterialApp(
debugShowCheckedModeBanner: false,
home: PostListScreen(),
);
@override
@override
void initState() {
super.initState();
fetchPosts();
try {
if (response.statusCode == 200) {
setState(() {
posts = json.decode(response.body);
isLoading = false;
});
} else {
} catch (error) {
setState(() {
isLoading = false;
});
print('Error: $error');
@override
return Scaffold(
appBar: AppBar(
),
body: isLoading
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: posts.length,
return ListTile(
title: Text(post['title']),
subtitle: Text(post['body']),
);
},
),
);
Output
Initial State:
After Fetching:
A list of posts is displayed, showing the title and body of each post.
Example:
Result
The application successfully fetches JSON data from the API, processes it, and displays it in
a scrollable list.
To create a Flutter application that allows users to log in and persists their login state using session
management techniques with the shared_preferences package.
Algorithm
1. On the Home Screen, display a "Log out" button that clears the session (removes the login
flag).
Code
pubspec.yaml:
dependencies:
flutter:
sdk: flutter
lib/main.dart:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(MyApp());
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: LoginPage(),
);
@override
// Sample credentials
Navigator.pushReplacement(
context,
);
} else {
_showErrorDialog();
void _showErrorDialog() {
showDialog(
context: context,
title: Text('Error'),
actions: [
TextButton(
onPressed: () {
Navigator.of(ctx).pop();
},
child: Text('Okay'),
),
],
),
);
}
@override
return Scaffold(
body: Padding(
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: _usernameController,
validator: (value) {
return null;
},
),
TextFormField(
controller: _passwordController,
obscureText: true,
validator: (value) {
return null;
},
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _login,
child: Text('Login'),
),
],
),
),
),
);
Navigator.pushReplacement(
context,
);
}
@override
return Scaffold(
body: Center(
child: ElevatedButton(
child: Text('Logout'),
),
),
);
@override
return FutureBuilder(
future: _checkLoginStatus(context),
if (snapshot.connectionState == ConnectionState.waiting) {
return Scaffold(
);
if (snapshot.data == true) {
},
);
Explanation of Code
1. LoginPage: Handles user authentication. If the login credentials are correct, the login status
is saved in shared_preferences as a boolean value (isLoggedIn), and the user is redirected to
the HomePage.
2. HomePage: Displays a logout button that clears the login status and redirects back to the
LoginPage.
3. SharedPreferences: Used for persistent storage of the login state (isLoggedIn). This allows
the app to remember whether the user is logged in after the app is restarted.
4. SplashPage: This page checks if the user is already logged in before navigating to the
appropriate screen.
Output
1. Login Screen:
o The user enters their username and password.
o If valid, they are redirected to the Home Page.
2. Home Screen:
o After successful login, the Home Page will be displayed with a "Logout" button.
o Pressing "Logout" will clear the session and navigate back to the Login Screen.
Result
The app uses session management through shared_preferences to store the user's login
state.
After logging in successfully, the user is redirected to the Home Screen. If the app is
restarted, the Splash Screen checks whether the user is logged in or not and navigates
accordingly.
The user can log out, which will clear the session and navigate back to the Login Screen.