0% found this document useful (0 votes)
4 views7 pages

Flutter

The document contains a Flutter application that implements a simple login and home page functionality. It uses the Provider package for state management to handle a shopping cart feature, allowing users to add or remove items. The application includes various classes for managing items, displaying them in a list, and handling user input for login credentials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Flutter

The document contains a Flutter application that implements a simple login and home page functionality. It uses the Provider package for state management to handle a shopping cart feature, allowing users to add or remove items. The application includes various classes for managing items, displaying them in a list, and handling user input for login credentials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Main.

dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:temp/item-provider.dart';

import 'home-page.dart';

void main(){
runApp(ChangeNotifierProvider(child: MyApp(),create:
(context) => ProductsProvider(),));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "SUHAD",
home: LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
@override
State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {


TextEditingController emailController = new
TextEditingController();
TextEditingController passwordController = new
TextEditingController();
String? emailError;
String? passwordError;

void onSubmit() {
String email = emailController.text;
String password = passwordController.text;
setState(() {
if (!email.contains("@")) {
emailError = "Enter proper email!";
return;
}
emailError = null;
if (password.length < 6) {
passwordError = "Password must be at_least 6
characters";
return;
}
passwordError = null;
Navigator.push(context, MaterialPageRoute(builder:
(context) => HomePage(),));
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Login'),
leading: Icon(Icons.account_circle),
actions: [
Icon(Icons.shopping_bag),
],
),
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: 200,
),
TextField(
controller: emailController,
decoration: InputDecoration(
errorText: emailError,
border: OutlineInputBorder(),
hintText: "Email",
labelText: "Enter Email",
prefixIcon: Icon(Icons.email),
),
),
SizedBox(
height: 50,
),
TextField(
controller: passwordController,
decoration: InputDecoration(
border: OutlineInputBorder(),
errorText: passwordError,
hintText: "Password",
labelText: "Enter password",
prefixIcon:
Icon(Icons.lock_outline_rounded),
suffixIcon: Icon(Icons.remove_red_eye)
),
),
SizedBox(
height: 30,
),
ElevatedButton(onPressed: onSubmit, child:
Text('Login')),
],
),
),
);
}
}

home-page.dart

import 'package:flutter/material.dart';
import 'package:temp/item-widget.dart';
import 'package:temp/item.dart';

import 'dummy-item-provider.dart';

class HomePage extends StatefulWidget {


const HomePage({super.key});

@override
` State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {


List<Item> items = DummyItemProvider.getDummyItem();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home page'),
backgroundColor: Colors.yellow),
body: ListView.builder(
itemBuilder: (context, index) {
return ItemWidget(item: items[index]);
},
itemCount: items.length,
),
);
}
}

item-widget.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:temp/item.dart';

import 'item-provider.dart';

class ItemWidget extends StatefulWidget {


Item item;
ItemWidget({super.key,required this.item});

@override
State<ItemWidget> createState() => _ItemWidgetState();
}

class _ItemWidgetState extends State<ItemWidget> {


@override
Widget build(BuildContext context) {
final cart = Provider.of<ProductsProvider>(context);
final isInCart = cart.cartItems.contains(widget.item);
return Row(
children: [
Expanded(child: Image.network(widget.item.image,height:
150,width: 150,)),
Expanded(
child: Column(
children: [
Text(widget.item.name),
Text(widget.item.price),
ElevatedButton(
// style: ElevatedButton.styleFrom(
// shape: RoundedRectangleBorder(
// borderRadius:
BorderRadius.all(Radius.circular(10)),
// ),
// ),
onPressed: () => isInCart ?
cart.removeFromCart(widget.item) : cart.addToCart(widget.item),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceAround,
children: [
Icon(isInCart ? Icons.remove_shopping_cart
: Icons.add_shopping_cart,color: Colors.deepOrangeAccent,),
Text(isInCart ? 'Remove' : 'Add to
cart',style: TextStyle(color: Colors.deepOrangeAccent,fontWeight:
FontWeight.bold,fontSize: 11),)
],
)
),

],
),
)
],
);
}
}

item.dart

class Item{
String image;
String name;
String price;

Item(this.image, this.name, this.price);


}
dummy-item-provider.dart

import 'package:temp/item.dart';

class DummyItemProvider {
static List<Item> getDummyItem() {
return [
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
Item(
'data:image/jpeg;base64,/9j/4AAQSk',
'Deluxe Room',
'Rs. 10000',
),
];
}
}

item-provider.dart

import 'package:flutter/material.dart';
import 'package:temp/item.dart';

import 'dummy-item-provider.dart';

class ProductsProvider extends ChangeNotifier {


//initialization
final List<Item> _items = DummyItemProvider.getDummyItem();
final List<Item> _cartItems = [];
int get cartCount => cartItems.length;
//getter
List<Item> get items => _items;
List<Item> get cartItems => _cartItems;

void addToCart(Item item) {


if (!cartItems.contains(item)) {
cartItems.add(item);
notifyListeners();
}
}

void removeFromCart(Item item) {


cartItems.remove(item);
notifyListeners();
}
}

You might also like