You are on page 1of 11

MealRater App:

MealRater App Code:

MealRaterHomeView page code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:tip_calculator/rating_page.dart';

class MealRaterHomeView extends StatefulWidget {


const MealRaterHomeView({super.key});

@override
State<MealRaterHomeView> createState() => _MealRaterHomeViewState();
}

class _MealRaterHomeViewState extends State<MealRaterHomeView> {


final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
int rating = 0;
final TextEditingController dishNameController = TextEditingController();
final TextEditingController restaurantNameController =
TextEditingController();
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
key: _scaffoldKey,
appBar: AppBar(
backgroundColor: const Color(0xff1E3C64),
title: const Text(
'MealRater',
),
),
body: Padding(
padding: const EdgeInsets.symmetric(vertical: 26, horizontal: 22),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
children: [
Expanded(
child: Text(
'Restaurant:',
style: style1.copyWith(fontSize: 22),
textAlign: TextAlign.right,
),
),
const SizedBox(width: 4),
Expanded(
flex: 2,
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 0.1,
),
),
child: TextField(
controller: restaurantNameController,
keyboardType: TextInputType.text,
decoration: const InputDecoration(
hintText: 'Enter Restaurant Name',
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 10),
),
),
),
),
],
),
const SizedBox(height: 10),
Row(
children: [
Expanded(
child: Text(
'Dish:',
style: style1.copyWith(fontSize: 22),
textAlign: TextAlign.right,
),
),
const SizedBox(width: 4),
Expanded(
flex: 2,
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
width: 0.1,
),
),
child: TextField(
controller: dishNameController,
keyboardType: TextInputType.text,
decoration: const InputDecoration(
hintText: 'Enter Dish Name',
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(horizontal: 10),
),
),
),
),
],
),
const SizedBox(height: 20),
Center(
child: Text(
'Rating: $rating',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: SizedBox(
height: 50,
child: ElevatedButton(
onPressed: () {
try {
if (restaurantNameController.text.isEmpty ||
dishNameController.text.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Please fill all the fields.'),
duration: Duration(seconds: 2),
),
);
} else {}
FirebaseFirestore.instance
.collection('MealRating')
.add({
'Restaurant': restaurantNameController.text,
'Dish': dishNameController.text,
'Rating': rating
}).then((value) {
restaurantNameController.clear();
dishNameController.clear();
rating = 0;
setState(() {

});
});
} on FirebaseException catch (e) {
print(e.code);
}
},
child: const Text(
'Save',
style: TextStyle(
color: Color(0xffEC9E37),
fontWeight: FontWeight.w500,
fontSize: 24,
),
),
),
),
),
const SizedBox(width: 20),
Expanded(
child: SizedBox(
height: 50,
width: 250,
child: ElevatedButton(
onPressed: () {
navigateToRatingScreen();
},
child: const Text(
'Rate Meal',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w500,
color: Color(0xffEC9E37),
),
),
),
),
),
],
),
],
),
),
),
);
}

TextStyle style1 = const TextStyle(


fontSize: 18,
fontWeight: FontWeight.w600,
);
void navigateToRatingScreen() {
final restaurantName = restaurantNameController.text;
final dishName = dishNameController.text;
if (restaurantName.isEmpty || dishName.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Please fill all the fields.'),
duration: Duration(seconds: 2),
),
);
} else {
// dishController.clear();
// restaurantController.clear();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RatingPage(
onRatingChanged: (newRating) {
setState(() {
rating = newRating;
});
},
),
),
);
}
}

Rating page code:

import 'package:flutter/material.dart';

class RatingPage extends StatefulWidget {


final Function(int) onRatingChanged;
const RatingPage({super.key, required this.onRatingChanged});
@override
State<RatingPage> createState() => _RatingPageState();
}

class _RatingPageState extends State<RatingPage> {


int _currentRating = 0;

@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
leading: InkWell(
onTap: () {
Navigator.pop(context);
},
child: const Icon(
Icons.arrow_back,
color: Color(0xffEC9E37),
)),
backgroundColor: const Color(0xff1E3C64),
title: const Text('Rate this meal'),
),
body: Column(
children: <Widget>[
const SizedBox(height: 40),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
for (int i = 1; i <= 5; i++)
GestureDetector(
onTap: () {
_handleRatingChanged(i);
},
child: Container(
width: 70,
height: 50,
decoration: BoxDecoration(
border: Border(
top: const BorderSide(
color: Colors.blue,
width: 2.0,
),
left: i != 2 && i != 4 && i != 3 && i != 5
? const BorderSide(
color: Colors.blue,
width: 2.0,
)
: BorderSide.none,
right:
const BorderSide(color: Colors.blue, width: 2.0),
bottom:
const BorderSide(color: Colors.blue, width: 2.0),
),
color: _currentRating >= i ? Colors.blue : Colors.white,
),
child: Center(
child: Text(
'$i',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: _currentRating >= i
? Colors.white
: Colors.blue),
),
),
),
),
],
),
const SizedBox(height: 20),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 15),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: SizedBox(
height: 50,
child: ElevatedButton(
onPressed: () {
setState(() {
_currentRating = 1;
});
},
child: const Text(
' Cancel',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w500,
color: Color(0xffEC9E37)),
),
),
),
),
const SizedBox(width: 10),
Expanded(
child: SizedBox(
height: 50,
child: ElevatedButton(
onPressed: () {
Navigator.pop(context, _currentRating);
},
child: const Text(
'Save',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w500,
color: Color(0xffEC9E37)),
),
),
),
),
],
),
),
],
),
),
);
}

void _handleRatingChanged(int newRating) {


setState(() {
_currentRating = newRating;
});
widget.onRatingChanged(newRating);
}
}

You might also like