Implement submitting reviews

Co-authored-by: Reimar <mail@reim.ar>
This commit is contained in:
Alexandertp 2024-09-05 18:11:35 +02:00
parent bd9a6dfa03
commit e03131a8aa
3 changed files with 40 additions and 9 deletions

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile/base/sidemenu.dart'; import 'package:mobile/base/sidemenu.dart';
import 'models.dart'; import 'models.dart';
import 'api.dart' as api;
class CreateReviewPage extends StatefulWidget { class CreateReviewPage extends StatefulWidget {
const CreateReviewPage({super.key}); const CreateReviewPage({super.key});
@ -13,15 +14,17 @@ class CreateReviewPage extends StatefulWidget {
class _CreateReviewState extends State<CreateReviewPage> { class _CreateReviewState extends State<CreateReviewPage> {
final titleInput = TextEditingController(); final titleInput = TextEditingController();
final contentInput = TextEditingController(); final contentInput = TextEditingController();
Place? place;
var rating = 0;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final place = ModalRoute.of(context)!.settings.arguments as Place; place = ModalRoute.of(context)!.settings.arguments as Place;
return SideMenu( return SideMenu(
selectedIndex: -1, selectedIndex: -1,
body: Scaffold( body: Scaffold(
backgroundColor: Color(0xFFF9F9F9), backgroundColor: const Color(0xFFF9F9F9),
body: SingleChildScrollView( body: SingleChildScrollView(
child: Center( child: Center(
child: Container( child: Container(
@ -29,8 +32,8 @@ class _CreateReviewState extends State<CreateReviewPage> {
padding: const EdgeInsets.all(40), padding: const EdgeInsets.all(40),
constraints: const BoxConstraints(maxWidth: 400), constraints: const BoxConstraints(maxWidth: 400),
child: Column(children: [ child: Column(children: [
Text(place.name, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)), Text(place!.name, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
Text(place.description, style: const TextStyle(color: Colors.grey)), Text(place!.description, style: const TextStyle(color: Colors.grey)),
const SizedBox(height: 50), const SizedBox(height: 50),
TextField( TextField(
controller: titleInput, controller: titleInput,
@ -48,7 +51,21 @@ class _CreateReviewState extends State<CreateReviewPage> {
decoration: const InputDecoration( decoration: const InputDecoration(
hintText: 'Write a review...', hintText: 'Write a review...',
) )
) ),
const SizedBox(height: 30),
// Review Stars
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (var i = 0; i < rating; i++) IconButton(onPressed: () => setState(() => rating = i+1), icon: const Icon(Icons.star, color: Colors.yellow)),
for (var i = rating; i < 5; i++) IconButton(onPressed: () => setState(() => rating = i+1), icon: const Icon(Icons.star_border)),
],
),
const SizedBox(height: 30),
ElevatedButton(onPressed: _submitReview, child: const Text('Submit Review')),
]), ]),
) )
) )
@ -59,7 +76,20 @@ class _CreateReviewState extends State<CreateReviewPage> {
@override @override
void dispose() { void dispose() {
super.dispose();
titleInput.dispose(); titleInput.dispose();
contentInput.dispose(); contentInput.dispose();
} }
Future<void> _submitReview() async {
final response = await api.request(context, api.ApiService.app, 'POST', '/reviews', {
'title': titleInput.text,
'content': contentInput.text,
'place_name': place!.name,
'place_description': place!.description,
'rating': rating,
'lat': place!.point.latitude,
'lng': place!.point.longitude,
});
}
} }

View File

@ -173,7 +173,7 @@ class _MyHomePageState extends State<MyHomePage> {
Navigator.pushReplacementNamed( Navigator.pushReplacementNamed(
context, context,
'/reviews', '/reviews',
arguments: ReviewList(_reviews.where((review) => review.lat == point.latitude && review.lng == point.longitude).toList(), Place(name, description)) arguments: ReviewList(_reviews.where((review) => review.lat == point.latitude && review.lng == point.longitude).toList(), Place(name, description, point))
), ),
), ),
]), ]),

View File

@ -53,8 +53,9 @@ class Review {
class Place { class Place {
String name; String name;
String description; String description;
LatLng point;
Place(this.name, this.description); Place(this.name, this.description, this.point);
} }
class ReviewList { class ReviewList {