fix new version applocalization
This commit is contained in:
335
covas_mobile_old/lib/pages/AddProfile.dart
Normal file
335
covas_mobile_old/lib/pages/AddProfile.dart
Normal file
@@ -0,0 +1,335 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import '../pages/LoginDemo.dart';
|
||||
|
||||
import '../classes/alert.dart';
|
||||
|
||||
import '../variable/globals.dart' as globals;
|
||||
|
||||
import '../classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; // Créé plus loin
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await MobileAds.instance.initialize();
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: AddProfile(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class AddProfile extends StatefulWidget {
|
||||
const AddProfile({super.key});
|
||||
|
||||
@override
|
||||
_AddProfileState createState() => _AddProfileState();
|
||||
}
|
||||
|
||||
class _AddProfileState extends State<AddProfile> with ShowAlertDialog {
|
||||
BannerAd? _bannerAd;
|
||||
|
||||
TextEditingController inputUserName = TextEditingController();
|
||||
|
||||
TextEditingController inputName = TextEditingController();
|
||||
|
||||
TextEditingController inputFirstName = TextEditingController();
|
||||
TextEditingController inputEmail = TextEditingController();
|
||||
TextEditingController inputBirth = TextEditingController();
|
||||
TextEditingController inputPassword = TextEditingController();
|
||||
TextEditingController inputPasswordConfirmed = TextEditingController();
|
||||
|
||||
onTapFunctionDatePicker({required BuildContext context}) async {
|
||||
DateTime? pickedDate = await showDatePicker(
|
||||
context: context,
|
||||
firstDate: DateTime(1900),
|
||||
initialDate: DateTime.now(),
|
||||
lastDate: DateTime(2104));
|
||||
if (pickedDate == null) return;
|
||||
inputBirth.text = DateFormat("dd/MM/yyyy").format(pickedDate);
|
||||
}
|
||||
|
||||
convertNulltoEmptyString(var check) {
|
||||
if (check == null) {
|
||||
return "";
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
convertNulltoArray(List<String> check) {
|
||||
if (check == null) {
|
||||
return [];
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
String formatDate(String date) {
|
||||
var splitedDate = date.split("/");
|
||||
|
||||
var day = splitedDate[0];
|
||||
var month = splitedDate[1];
|
||||
var year = splitedDate[2];
|
||||
|
||||
return "${year}-${month}-${day}";
|
||||
}
|
||||
|
||||
Future<void> _createProfile(BuildContext context) async {
|
||||
var username = inputUserName.text;
|
||||
var firstName = inputFirstName.text;
|
||||
var name = inputName.text;
|
||||
var email = inputEmail.text;
|
||||
var password = inputPassword.text;
|
||||
var confirmedPassword = inputPasswordConfirmed.text;
|
||||
var birth = DateTime.parse(formatDate(inputBirth.text));
|
||||
|
||||
if ((password.isNotEmpty) && (confirmedPassword.isNotEmpty)) {
|
||||
if (password != confirmedPassword) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.password_different ??
|
||||
"Must write a different password");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var urlPut = Uri.parse("${globals.api}/mail");
|
||||
|
||||
var responsePost = await http.post(urlPut,
|
||||
headers: {
|
||||
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
|
||||
HttpHeaders.contentTypeHeader: 'application/json'
|
||||
},
|
||||
body: jsonEncode({
|
||||
'name': name,
|
||||
'username': username,
|
||||
'firstName': firstName,
|
||||
'password': password,
|
||||
'email': email,
|
||||
'birth': birth.toString()
|
||||
}));
|
||||
print(responsePost.statusCode);
|
||||
if (responsePost.statusCode == 200) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.create ?? "Creation",
|
||||
AppLocalizations.of(context)?.user_create ?? "Your user created");
|
||||
Navigator.pushReplacement(
|
||||
context, MaterialPageRoute(builder: (_) => LoginDemo()));
|
||||
return;
|
||||
}
|
||||
|
||||
final errorMessages = {
|
||||
400: AppLocalizations.of(context)?.request_error ??
|
||||
"Poorly constructed query",
|
||||
406: AppLocalizations.of(context)?.incorrect_password ??
|
||||
"Incorrect password",
|
||||
404: AppLocalizations.of(context)?.unknown_user ?? "Unknown user",
|
||||
403: AppLocalizations.of(context)?.disabled_user ?? "Disabled user",
|
||||
410: AppLocalizations.of(context)?.invalid_token ?? "Invalid token",
|
||||
500: AppLocalizations.of(context)?.internal_error_server ??
|
||||
"Internal error server"
|
||||
};
|
||||
|
||||
final text = errorMessages[responsePost.statusCode] ??
|
||||
AppLocalizations.of(context)?.unknown_error_auth ??
|
||||
"Unknown error auth";
|
||||
showAlertDialog(
|
||||
context, AppLocalizations.of(context)?.error ?? "Error", text);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String? _validateField(String? value) {
|
||||
return value!.isEmpty
|
||||
? AppLocalizations.of(context)?.required_input ?? 'Required input'
|
||||
: null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
AppLocalizations.of(context)?.create_profile ?? "Create profile"),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!)),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputUserName,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: 'Pseudo',
|
||||
hintText: AppLocalizations.of(context)?.edit_pseudo ??
|
||||
'Edit pseudo'),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputPassword,
|
||||
validator: (value) => _validateField(value),
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.password ??
|
||||
'Password',
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.enter_password ??
|
||||
'Enter the password'),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputPasswordConfirmed,
|
||||
validator: (value) => _validateField(value),
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.password_confirmed ??
|
||||
'Password confirmed',
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.password_confirmed ??
|
||||
'Password confirmed',
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputName,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.last_name ??
|
||||
'Last name',
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.edit_last_name ??
|
||||
'Edit last name'),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputFirstName,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.first_name ??
|
||||
'First name',
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.edit_first_name ??
|
||||
'Edit first name'),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputEmail,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.email ?? 'Email',
|
||||
hintText: AppLocalizations.of(context)?.edit_email ??
|
||||
'Edit email'),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputBirth,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.birth_date ??
|
||||
'Birth date',
|
||||
hintText: AppLocalizations.of(context)?.edit_birth ??
|
||||
'Edit birth date'),
|
||||
onTap: () => onTapFunctionDatePicker(context: context)),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Container(
|
||||
height: 50,
|
||||
width: 250,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(20)),
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_createProfile(context);
|
||||
}
|
||||
},
|
||||
child: Text(
|
||||
AppLocalizations.of(context)?.create_profile_button ??
|
||||
"Create profile",
|
||||
style: TextStyle(color: Colors.white, fontSize: 25),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
163
covas_mobile_old/lib/pages/Camera.dart
Normal file
163
covas_mobile_old/lib/pages/Camera.dart
Normal file
@@ -0,0 +1,163 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import '../classes/MyDrawer.dart';
|
||||
|
||||
import 'DisplayPictureScreen.dart';
|
||||
import 'package:camera/camera.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; // Créé plus loin
|
||||
|
||||
Future<void> main() async {
|
||||
// Ensure that plugin services are initialized so that `availableCameras()`
|
||||
// can be called before `runApp()`
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// Obtain a list of the available cameras on the device.
|
||||
final cameras = await availableCameras();
|
||||
|
||||
// Get a specific camera from the list of available cameras.
|
||||
final firstCamera = cameras.first;
|
||||
|
||||
runApp(
|
||||
MaterialApp(
|
||||
theme: ThemeData.dark(),
|
||||
home: Camera(
|
||||
// Pass the appropriate camera to the TakePictureScreen widget.
|
||||
camera: firstCamera,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// A screen that allows users to take a picture using a given camera.
|
||||
class Camera extends StatefulWidget {
|
||||
const Camera({
|
||||
super.key,
|
||||
required this.camera,
|
||||
});
|
||||
|
||||
final CameraDescription camera;
|
||||
|
||||
@override
|
||||
CameraState createState() => CameraState();
|
||||
}
|
||||
|
||||
class CameraState extends State<Camera> {
|
||||
late CameraController _controller;
|
||||
late Future<void> _initializeControllerFuture;
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// To display the current output from the Camera,
|
||||
// create a CameraController.
|
||||
_authService.checkTokenStatus(context);
|
||||
|
||||
_controller = CameraController(
|
||||
// Get a specific camera from the list of available cameras.
|
||||
widget.camera,
|
||||
// Define the resolution to use.
|
||||
ResolutionPreset.medium,
|
||||
);
|
||||
|
||||
// Next, initialize the controller. This returns a Future.
|
||||
_initializeControllerFuture = _controller.initialize();
|
||||
}
|
||||
|
||||
Future<void> pickImage() async {
|
||||
final imagePicker = ImagePicker();
|
||||
final pickedFile = await imagePicker.pickImage(source: ImageSource.gallery);
|
||||
if (pickedFile != null) {
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => DisplayPictureScreen(
|
||||
// Pass the automatically generated path to
|
||||
// the DisplayPictureScreen widget.
|
||||
imagePath: pickedFile.path,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// Dispose of the controller when the widget is disposed.
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)?.take_picture ??
|
||||
"Take a picture")),
|
||||
// You must wait until the controller is initialized before displaying the
|
||||
// camera preview. Use a FutureBuilder to display a loading spinner until the
|
||||
// controller has finished initializing.
|
||||
drawer: MyDrawer(),
|
||||
body: FutureBuilder<void>(
|
||||
future: _initializeControllerFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.done) {
|
||||
// If the Future is complete, display the preview.
|
||||
return CameraPreview(_controller);
|
||||
} else {
|
||||
// Otherwise, display a loading indicator.
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
},
|
||||
),
|
||||
floatingActionButton: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
FloatingActionButton(
|
||||
onPressed: pickImage,
|
||||
child: Icon(Icons.photo_library),
|
||||
),
|
||||
SizedBox(width: 40),
|
||||
FloatingActionButton(
|
||||
// Provide an onPressed callback.
|
||||
onPressed: () async {
|
||||
// Take the Picture in a try / catch block. If anything goes wrong,
|
||||
// catch the error.
|
||||
try {
|
||||
// Ensure that the camera is initialized.
|
||||
await _initializeControllerFuture;
|
||||
|
||||
// Attempt to take a picture and get the file `image`
|
||||
// where it was saved.
|
||||
final image = await _controller.takePicture();
|
||||
|
||||
if (!context.mounted) return;
|
||||
|
||||
// If the picture was taken, display it on a new screen.
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => DisplayPictureScreen(
|
||||
// Pass the automatically generated path to
|
||||
// the DisplayPictureScreen widget.
|
||||
imagePath: image.path,
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
// If an error occurs, log the error to the console.
|
||||
print(e);
|
||||
}
|
||||
},
|
||||
child: const Icon(Icons.camera_alt),
|
||||
)
|
||||
],
|
||||
)));
|
||||
}
|
||||
}
|
166
covas_mobile_old/lib/pages/CameraEdit.dart
Normal file
166
covas_mobile_old/lib/pages/CameraEdit.dart
Normal file
@@ -0,0 +1,166 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import '../classes/events.dart';
|
||||
import '../classes/MyDrawer.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'EditEvent.dart';
|
||||
import 'package:camera/camera.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; // Créé
|
||||
|
||||
Future<void> main() async {
|
||||
// Ensure that plugin services are initialized so that `availableCameras()`
|
||||
// can be called before `runApp()`
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// Obtain a list of the available cameras on the device.
|
||||
final cameras = await availableCameras();
|
||||
|
||||
// Get a specific camera from the list of available cameras.
|
||||
final firstCamera = cameras.first;
|
||||
Events? events;
|
||||
|
||||
runApp(
|
||||
MaterialApp(
|
||||
theme: ThemeData.dark(),
|
||||
home: CameraEdit(
|
||||
// Pass the appropriate camera to the TakePictureScreen widget.
|
||||
camera: firstCamera,
|
||||
events: events),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// A screen that allows users to take a picture using a given camera.
|
||||
class CameraEdit extends StatefulWidget {
|
||||
const CameraEdit({super.key, required this.camera, required this.events});
|
||||
final Events? events;
|
||||
|
||||
final CameraDescription camera;
|
||||
|
||||
@override
|
||||
CameraEditState createState() => CameraEditState();
|
||||
}
|
||||
|
||||
class CameraEditState extends State<CameraEdit> {
|
||||
late CameraController _controller;
|
||||
late Future<void> _initializeControllerFuture;
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
|
||||
// To display the current output from the Camera,
|
||||
// create a CameraController.
|
||||
|
||||
_controller = CameraController(
|
||||
// Get a specific camera from the list of available cameras.
|
||||
widget.camera,
|
||||
// Define the resolution to use.
|
||||
ResolutionPreset.medium,
|
||||
);
|
||||
|
||||
// Next, initialize the controller. This returns a Future.
|
||||
_initializeControllerFuture = _controller.initialize();
|
||||
}
|
||||
|
||||
Future<void> pickImage() async {
|
||||
final imagePicker = ImagePicker();
|
||||
final pickedFile = await imagePicker.pickImage(source: ImageSource.gallery);
|
||||
if (pickedFile != null) {
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => EditEvent(
|
||||
// Pass the automatically generated path to
|
||||
// the DisplayPictureScreen widget.
|
||||
events: widget.events,
|
||||
imgPath: pickedFile.path,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
// Dispose of the controller when the widget is disposed.
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)?.take_picture ??
|
||||
'Take a picture')),
|
||||
// You must wait until the controller is initialized before displaying the
|
||||
// camera preview. Use a FutureBuilder to display a loading spinner until the
|
||||
// controller has finished initializing.
|
||||
drawer: MyDrawer(),
|
||||
body: FutureBuilder<void>(
|
||||
future: _initializeControllerFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.done) {
|
||||
// If the Future is complete, display the preview.
|
||||
return CameraPreview(_controller);
|
||||
} else {
|
||||
// Otherwise, display a loading indicator.
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
},
|
||||
),
|
||||
floatingActionButton: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
FloatingActionButton(
|
||||
onPressed: pickImage,
|
||||
child: Icon(Icons.photo_library),
|
||||
),
|
||||
SizedBox(width: 40),
|
||||
FloatingActionButton(
|
||||
// Provide an onPressed callback.
|
||||
onPressed: () async {
|
||||
// Take the Picture in a try / catch block. If anything goes wrong,
|
||||
// catch the error.
|
||||
try {
|
||||
// Ensure that the camera is initialized.
|
||||
await _initializeControllerFuture;
|
||||
|
||||
// Attempt to take a picture and get the file `image`
|
||||
// where it was saved.
|
||||
final image = await _controller.takePicture();
|
||||
|
||||
if (!context.mounted) return;
|
||||
|
||||
// If the picture was taken, display it on a new screen.
|
||||
await Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => EditEvent(
|
||||
// Pass the automatically generated path to
|
||||
// the DisplayPictureScreen widget.
|
||||
events: widget.events,
|
||||
imgPath: image.path,
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
// If an error occurs, log the error to the console.
|
||||
print(e);
|
||||
}
|
||||
},
|
||||
child: const Icon(Icons.camera_alt),
|
||||
)
|
||||
],
|
||||
)));
|
||||
}
|
||||
}
|
268
covas_mobile_old/lib/pages/DisplayPictureScreen.dart
Normal file
268
covas_mobile_old/lib/pages/DisplayPictureScreen.dart
Normal file
@@ -0,0 +1,268 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'dart:io';
|
||||
import '../classes/addEventImage.dart';
|
||||
import '../classes/alert.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:flutter_gemini/flutter_gemini.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import "ItemMenu.dart";
|
||||
import 'UpdateEventImage.dart';
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import '../variable/globals.dart' as globals;
|
||||
import '../classes/MyDrawer.dart';
|
||||
|
||||
import '../classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; // Créé
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await MobileAds.instance.initialize();
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
// This widget is the root of your application.
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
// This is the theme of your application.
|
||||
//
|
||||
// Try running your application with "flutter run". You'll see the
|
||||
// application has a blue toolbar. Then, without quitting the app, try
|
||||
// changing the primarySwatch below to Colors.green and then invoke
|
||||
// "hot reload" (press "r" in the console where you ran "flutter run",
|
||||
// or simply save your changes to "hot reload" in a Flutter IDE).
|
||||
// Notice that the counter didn't reset back to zero; the application
|
||||
// is not restarted.
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// A screen that allows users to take a picture using a given camera.
|
||||
class DisplayPictureScreen extends StatefulWidget {
|
||||
final String imagePath;
|
||||
|
||||
const DisplayPictureScreen({super.key, required this.imagePath});
|
||||
|
||||
@override
|
||||
DisplayPictureScreenState createState() => DisplayPictureScreenState();
|
||||
}
|
||||
|
||||
// A widget that displays the picture taken by the user.
|
||||
class DisplayPictureScreenState extends State<DisplayPictureScreen>
|
||||
with ShowDescImageAdd, ShowAlertDialog, TickerProviderStateMixin {
|
||||
BannerAd? _bannerAd;
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
late AnimationController controller;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
|
||||
controller = AnimationController(
|
||||
/// [AnimationController]s can be created with `vsync: this` because of
|
||||
/// [TickerProviderStateMixin].
|
||||
vsync: this,
|
||||
duration: const Duration(seconds: 5),
|
||||
)..addListener(() {
|
||||
setState(() {});
|
||||
});
|
||||
controller.repeat(reverse: false);
|
||||
|
||||
_getEventInfosFromImage();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> displayError(String e) async {
|
||||
print("problem gemini : ${e}");
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? 'Error',
|
||||
AppLocalizations.of(context)?.error_ia ??
|
||||
'Google AI failed to analyze picture. Retry with another one');
|
||||
}
|
||||
|
||||
void _showErrorDialog(BuildContext context, String title, String message) {
|
||||
showAlertDialog(context, title, message);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> _fetchGeolocation(String place) async {
|
||||
final apiKey = dotenv.env['PLACE_API_KEY'] ?? '';
|
||||
final response = await http.get(Uri.parse(
|
||||
'https://maps.googleapis.com/maps/api/place/textsearch/json?query=${place}}&key=$apiKey'));
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
if (data['results'].isNotEmpty) {
|
||||
return data['results'][0]['geometry']['location'];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<bool> _isDuplicateEvent(String accessToken,
|
||||
Map<String, dynamic> jsonData, Map<String, dynamic> location) async {
|
||||
final url = Uri.parse(
|
||||
"${globals.api}/events/search?item=${jsonData["name"]}&date_event=${jsonData["start_date"]}"
|
||||
"&min_lat=${location['lat']}&max_lat=${location['lat']}"
|
||||
"&min_lon=${location['lng']}&max_lon=${location['lng']}");
|
||||
|
||||
final response = await http.get(url,
|
||||
headers: {HttpHeaders.cookieHeader: 'access_token=$accessToken'});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final events = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
return events.isNotEmpty;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<void> searchEvents(String json, String imagePath) async {
|
||||
print(json.replaceAll("'''json", '').replaceAll("'''", ""));
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
try {
|
||||
Map<String, dynamic> jsonData =
|
||||
jsonDecode(json.replaceAll("```json", '').replaceAll("```", ""));
|
||||
print("json : ${jsonData}");
|
||||
var name = jsonData["name"];
|
||||
print("name : ${name}");
|
||||
var place = jsonData["place"];
|
||||
var date = jsonData["start_date"];
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
|
||||
if (accessToken.isNotEmpty) {
|
||||
final location = await _fetchGeolocation(place);
|
||||
if (location == null) {
|
||||
_showErrorDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? 'Error',
|
||||
AppLocalizations.of(context)?.no_data_geo ??
|
||||
'No geographical data');
|
||||
return;
|
||||
}
|
||||
|
||||
final url = Uri.parse(
|
||||
"${globals.api}/events/search?item=${name}&date_event=${date}"
|
||||
"&min_lat=${location['lat']}&max_lat=${location['lat']}"
|
||||
"&min_lon=${location['lng']}&max_lon=${location['lng']}");
|
||||
|
||||
final response = await http.get(url,
|
||||
headers: {HttpHeaders.cookieHeader: 'access_token=$accessToken'});
|
||||
if (response.statusCode == 200) {
|
||||
var events = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
print("reponse http : ${events.length}");
|
||||
if (events.length == 0) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => UpdateeventImage(
|
||||
events: jsonData, imagePath: imagePath)));
|
||||
} else {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ItemMenu(title: events[0]["id"])));
|
||||
}
|
||||
} else {
|
||||
String error = AppLocalizations.of(context)?.response_status_update ??
|
||||
'Response status update : ${response.statusCode}';
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? 'Error',
|
||||
"${error} : ${response.statusCode}");
|
||||
}
|
||||
} else {
|
||||
showAlertDialog(context, AppLocalizations.of(context)?.error ?? 'Error',
|
||||
AppLocalizations.of(context)?.error_token ?? "Token error");
|
||||
}
|
||||
} catch (e) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.error_format ??
|
||||
"Data format error given by AI");
|
||||
}
|
||||
|
||||
//showDescImageAddDialog(context, message);
|
||||
}
|
||||
|
||||
Future<void> _getEventInfosFromImage() async {
|
||||
await dotenv.load();
|
||||
|
||||
final gemini = Gemini.init(
|
||||
apiKey: dotenv.env['GEMINI_API_KEY']!, enableDebugging: true);
|
||||
|
||||
final file = File(widget.imagePath);
|
||||
|
||||
gemini
|
||||
.textAndImage(
|
||||
text:
|
||||
"Peux-tu donner le nom, la date (si l'année n'est pas précisé, mettez l'année actuelle ou future) et le lieu de l'évènement sous format JSON (sans le caratère json au début de la chaine de caractère) avec les valeurs suivantes : name, place, description, tags (tableau sans espace), organizers (tableau), start_date et end_date (si le end_date est vide, alors donnez une valeur de six de plus par rapport à start_date) sous le format en YYYY-MM-DD HH:mm:ssZ",
|
||||
images: [file.readAsBytesSync()],
|
||||
modelName: "models/gemini-1.5-pro-latest")
|
||||
.then((value) => searchEvents(
|
||||
value?.content?.parts?.last.text ?? '', widget.imagePath))
|
||||
.catchError((e) => displayError);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)?.display_picture ??
|
||||
"Display The Picture")),
|
||||
// The image is stored as a file on the device. Use the `Image.file`
|
||||
// constructor with the given path to display the image.
|
||||
drawer: MyDrawer(),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: <Widget>[
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!)),
|
||||
Text(
|
||||
AppLocalizations.of(context)?.analyze_image ??
|
||||
'Image analyze in progress',
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
CircularProgressIndicator(
|
||||
value: controller.value,
|
||||
semanticsLabel:
|
||||
AppLocalizations.of(context)?.loading_progress ??
|
||||
'Loading progress',
|
||||
),
|
||||
])));
|
||||
}
|
||||
}
|
921
covas_mobile_old/lib/pages/EditEvent.dart
Normal file
921
covas_mobile_old/lib/pages/EditEvent.dart
Normal file
@@ -0,0 +1,921 @@
|
||||
import 'package:covas_mobile/pages/CameraEdit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:textfield_tags/textfield_tags.dart';
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import '../classes/events.dart';
|
||||
import '../classes/MyDrawer.dart';
|
||||
|
||||
import 'package:camera/camera.dart';
|
||||
|
||||
import '../classes/alert.dart';
|
||||
import '../classes/eventAdded.dart';
|
||||
|
||||
import '../variable/globals.dart' as globals;
|
||||
|
||||
import '../classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; // Créé
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await MobileAds.instance.initialize();
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
Events? events;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: EditEvent(
|
||||
events: events,
|
||||
imgPath: "",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EditEvent extends StatefulWidget {
|
||||
const EditEvent({Key? key, required this.events, required this.imgPath})
|
||||
: super(key: key);
|
||||
final Events? events;
|
||||
|
||||
final String imgPath;
|
||||
|
||||
@override
|
||||
_EditEventState createState() => _EditEventState();
|
||||
}
|
||||
|
||||
class _EditEventState extends State<EditEvent>
|
||||
with ShowAlertDialog, ShowEventDialog {
|
||||
BannerAd? _bannerAd;
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
TextEditingController inputName = TextEditingController();
|
||||
|
||||
TextEditingController inputTicket = TextEditingController();
|
||||
TextEditingController inputLink = TextEditingController();
|
||||
TextEditingController inputDate = TextEditingController();
|
||||
TextEditingController inputDesc = TextEditingController();
|
||||
|
||||
TextEditingController inputGeo = TextEditingController();
|
||||
|
||||
TextEditingController startDatepicker = TextEditingController();
|
||||
TextEditingController startTimepicker = TextEditingController();
|
||||
TextEditingController endDatepicker = TextEditingController();
|
||||
TextEditingController endTimepicker = TextEditingController();
|
||||
final _stringTagController = StringTagController();
|
||||
|
||||
DateTime startDate = DateTime.now();
|
||||
DateTime endDate = DateTime.now();
|
||||
List<Map<String, dynamic>> suggestions = [];
|
||||
String geographicalZone = "";
|
||||
String imgUrl = "";
|
||||
|
||||
List<String> initialTags = [];
|
||||
|
||||
final _stringOrgaController = StringTagController();
|
||||
List<String> initialOrga = [];
|
||||
|
||||
onTapFunctionDatePicker(
|
||||
{required BuildContext context, required String position}) async {
|
||||
DateTime date;
|
||||
if ((startDatepicker.text.isEmpty) || (endDatepicker.text.isEmpty)) {
|
||||
date = DateTime.now();
|
||||
} else {
|
||||
date = DateTime.parse(formatDate(startDatepicker.text));
|
||||
if (position == "end") {
|
||||
date = DateTime.parse(formatDate(endDatepicker.text));
|
||||
}
|
||||
}
|
||||
DateTime? pickedDate = await showDatePicker(
|
||||
context: context,
|
||||
firstDate: date,
|
||||
initialDate: date,
|
||||
lastDate: DateTime(2104));
|
||||
if (pickedDate == null) return;
|
||||
if (position == "start") {
|
||||
startDatepicker.text = DateFormat("dd/MM/yyyy").format(pickedDate);
|
||||
}
|
||||
if (position == "end") {
|
||||
endDatepicker.text = DateFormat("dd/MM/yyyy").format(pickedDate);
|
||||
}
|
||||
}
|
||||
|
||||
onTapFunctionTimePicker(
|
||||
{required BuildContext context, required String position}) async {
|
||||
TimeOfDay time;
|
||||
|
||||
if ((startTimepicker.text.isEmpty) || (endTimepicker.text.isEmpty)) {
|
||||
time = TimeOfDay.now();
|
||||
} else {
|
||||
DateTime date = new DateTime.now();
|
||||
date = date.copyWith(
|
||||
hour: int.parse(startTimepicker.text.split(":")[0]),
|
||||
minute: int.parse(startTimepicker.text.split(":")[1]));
|
||||
time = TimeOfDay.fromDateTime(date);
|
||||
if (position == "end") {
|
||||
date = date.copyWith(
|
||||
hour: int.parse(endTimepicker.text.split(":")[0]),
|
||||
minute: int.parse(endTimepicker.text.split(":")[1]));
|
||||
time = TimeOfDay.fromDateTime(date);
|
||||
}
|
||||
}
|
||||
|
||||
TimeOfDay? pickedDate =
|
||||
await showTimePicker(context: context, initialTime: time);
|
||||
if (pickedDate == null) return;
|
||||
if (position == "start") {
|
||||
startTimepicker.text = pickedDate.format(context);
|
||||
}
|
||||
if (position == "end") {
|
||||
endTimepicker.text = pickedDate.format(context);
|
||||
}
|
||||
}
|
||||
|
||||
convertNulltoEmptyString(var check) {
|
||||
if (check == null) {
|
||||
return "";
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
convertNulltoArray(List<String> check) {
|
||||
if (check == null) {
|
||||
return [];
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
String formatDate(String date) {
|
||||
var splitedDate = date.split("/");
|
||||
|
||||
var day = splitedDate[0];
|
||||
var month = splitedDate[1];
|
||||
var year = splitedDate[2];
|
||||
|
||||
return "${year}-${month}-${day}";
|
||||
}
|
||||
|
||||
Future<void> _updateEvent(BuildContext context) async {
|
||||
if (!_isEventInFuture()) {
|
||||
_showErrorDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error_event ?? "Event error",
|
||||
AppLocalizations.of(context)?.no_future_event ?? "No future event");
|
||||
return;
|
||||
}
|
||||
|
||||
final accessToken = await _getAccessToken();
|
||||
if (accessToken.isEmpty) {
|
||||
_showErrorDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error_user ?? "User error",
|
||||
AppLocalizations.of(context)?.empty_input ?? "Empty input");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await dotenv.load();
|
||||
final geolocation = await _fetchGeolocation();
|
||||
if (geolocation == null) {
|
||||
_showErrorDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.no_data_geo ??
|
||||
"No geographical data");
|
||||
return;
|
||||
}
|
||||
|
||||
if (await _isDuplicateEvent(accessToken, geolocation)) {
|
||||
_showErrorDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.info_event ?? "Event info",
|
||||
AppLocalizations.of(context)?.event_already ??
|
||||
"Event already exists");
|
||||
return;
|
||||
}
|
||||
|
||||
if (widget.imgPath.isNotEmpty) {
|
||||
imgUrl = await _uploadImage(widget.imgPath);
|
||||
if (imgUrl.isEmpty) {
|
||||
_showErrorDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.picture_error ?? "Error picture",
|
||||
AppLocalizations.of(context)?.no_picture_published ??
|
||||
"No picture published");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await _updateEventData(accessToken, geolocation);
|
||||
String message =
|
||||
AppLocalizations.of(context)?.event_update ?? "Event updated";
|
||||
showEventDialog(context, "${message} : ${inputName.text}");
|
||||
} catch (e) {
|
||||
_showErrorDialog(
|
||||
context, AppLocalizations.of(context)?.error ?? "Error", "$e");
|
||||
}
|
||||
}
|
||||
|
||||
bool _isEventInFuture() {
|
||||
DateTime startDateCompare = DateTime.parse(
|
||||
"${formatDate(startDatepicker.text)}T${startTimepicker.text.replaceAll('-', ':')}");
|
||||
return startDateCompare.isAfter(DateTime.now());
|
||||
}
|
||||
|
||||
Future<String> _getAccessToken() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getString("access_token") ?? "";
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> _fetchGeolocation() async {
|
||||
final apiKey = dotenv.env['PLACE_API_KEY'] ?? '';
|
||||
final response = await http.get(Uri.parse(
|
||||
'https://maps.googleapis.com/maps/api/place/textsearch/json?query=${inputGeo.text}&key=$apiKey'));
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
if (data['results'].isNotEmpty) {
|
||||
return data['results'][0]['geometry']['location'];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<bool> _isDuplicateEvent(
|
||||
String accessToken, Map<String, dynamic> location) async {
|
||||
final url = Uri.parse(
|
||||
"${globals.api}/events/search?item=${inputName.text}&date_event=${formatDate(startDatepicker.text)}"
|
||||
"&min_lat=${location['lat']}&max_lat=${location['lat']}"
|
||||
"&min_lon=${location['lng']}&max_lon=${location['lng']}");
|
||||
|
||||
final response = await http.get(url,
|
||||
headers: {HttpHeaders.cookieHeader: 'access_token=$accessToken'});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final events = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
return events.isNotEmpty && events[0]["id"] != widget.events!.id;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<String> _uploadImage(String imagePath) async {
|
||||
final params = {
|
||||
'expiration': '15552000',
|
||||
'key': dotenv.env["IMGBB_API_KEY"]
|
||||
};
|
||||
final url = Uri.parse('https://api.imgbb.com/1/upload')
|
||||
.replace(queryParameters: params);
|
||||
|
||||
final image = File(imagePath);
|
||||
final req = http.MultipartRequest('POST', url)
|
||||
..fields['image'] = base64.encode(await image.readAsBytes());
|
||||
|
||||
final response = await http.Response.fromStream(await req.send());
|
||||
return response.statusCode == 200
|
||||
? json.decode(response.body)["data"]["url"]
|
||||
: "";
|
||||
}
|
||||
|
||||
Future<void> _updateEventData(
|
||||
String accessToken, Map<String, dynamic> location) async {
|
||||
final url = Uri.parse("${globals.api}/events/${widget.events!.id}");
|
||||
final response = await http.put(url,
|
||||
headers: {
|
||||
HttpHeaders.cookieHeader: 'access_token=$accessToken',
|
||||
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
|
||||
HttpHeaders.contentTypeHeader: 'application/json'
|
||||
},
|
||||
body: jsonEncode({
|
||||
'name': inputName.text,
|
||||
'place': inputGeo.text,
|
||||
'start_date':
|
||||
"${formatDate(startDatepicker.text)}T${startTimepicker.text.replaceAll('-', ':')}",
|
||||
'end_date':
|
||||
"${formatDate(endDatepicker.text)}T${endTimepicker.text.replaceAll('-', ':')}",
|
||||
'organizers':
|
||||
List<String>.from(_stringOrgaController.getTags as List),
|
||||
'latitude': location['lat'],
|
||||
'longitude': location['lng'],
|
||||
'description': inputDesc.text,
|
||||
"imgUrl": imgUrl,
|
||||
'link': inputLink.text,
|
||||
'ticket': inputTicket.text,
|
||||
"tags": List<String>.from(_stringTagController.getTags as List)
|
||||
}));
|
||||
|
||||
if (response.statusCode != 200 && response.statusCode != 201) {
|
||||
_handleErrorResponse(context, response.statusCode);
|
||||
}
|
||||
}
|
||||
|
||||
void _handleErrorResponse(BuildContext context, int statusCode) {
|
||||
final messages = {
|
||||
400: AppLocalizations.of(context)?.request_error ??
|
||||
"Poorly constructed query",
|
||||
406: AppLocalizations.of(context)?.incorrect_password ??
|
||||
"Incorrect password",
|
||||
404: AppLocalizations.of(context)?.unknown_user ?? "Unknown user",
|
||||
403: AppLocalizations.of(context)?.disabled_user ?? "Disabled user",
|
||||
410: AppLocalizations.of(context)?.invalid_token ?? "Invalid token",
|
||||
500: AppLocalizations.of(context)?.internal_error_server ??
|
||||
"Internal error server"
|
||||
};
|
||||
_showErrorDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
messages[statusCode] ??
|
||||
AppLocalizations.of(context)?.unknown_error_auth ??
|
||||
"Unknown error auth");
|
||||
}
|
||||
|
||||
void _showErrorDialog(BuildContext context, String title, String message) {
|
||||
showAlertDialog(context, title, message);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
inputName.text = widget.events!.name ?? "";
|
||||
inputTicket.text = widget.events!.ticket ?? "";
|
||||
inputLink.text = widget.events!.link ?? "";
|
||||
startDatepicker.text = DateFormat("dd/MM/yyyy")
|
||||
.format(DateTime.parse(
|
||||
widget.events!.startDate ?? DateTime.now().toString()))
|
||||
.toString();
|
||||
startTimepicker.text = DateFormat("HH:mm")
|
||||
.format(DateTime.parse(
|
||||
widget.events!.startDate ?? DateTime.now().toString()))
|
||||
.toString();
|
||||
endDatepicker.text = DateFormat("dd/MM/yyyy")
|
||||
.format(
|
||||
DateTime.parse(widget.events!.endDate ?? DateTime.now().toString()))
|
||||
.toString();
|
||||
endTimepicker.text = DateFormat("HH:mm")
|
||||
.format(
|
||||
DateTime.parse(widget.events!.endDate ?? DateTime.now().toString()))
|
||||
.toString();
|
||||
inputGeo.text = widget.events!.place ?? "";
|
||||
|
||||
imgUrl = widget.events!.imgUrl ?? "";
|
||||
inputDesc.text = widget.events!.description ?? "";
|
||||
initialTags = List<String>.from(widget.events!.tags as List);
|
||||
initialOrga = List<String>.from(widget.events!.organizers as List);
|
||||
}
|
||||
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String? _validateField(String? value) {
|
||||
return value!.isEmpty
|
||||
? AppLocalizations.of(context)?.required_input ?? "Required input"
|
||||
: null;
|
||||
}
|
||||
|
||||
Future<void> searchSuggestions(String input) async {
|
||||
await dotenv.load(fileName: ".env"); // Load .env file
|
||||
|
||||
final mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? '';
|
||||
final url =
|
||||
'https://api.mapbox.com/geocoding/v5/mapbox.places/${input}.json?access_token=${mapboxAccessToken}&types=poi,address,place';
|
||||
var encoded = Uri.encodeFull(url);
|
||||
final response = await http.get(Uri.parse(encoded));
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
setState(() {
|
||||
suggestions = (data['features'] as List)
|
||||
.map((feature) => {
|
||||
'place_name': feature['place_name'],
|
||||
'text': feature['text'],
|
||||
'geometry': feature[
|
||||
'geometry'], // Include geometry for latitude/longitude
|
||||
})
|
||||
.toList();
|
||||
});
|
||||
} else {
|
||||
throw Exception('Failed to load suggestions');
|
||||
}
|
||||
}
|
||||
|
||||
Padding _buildGeographicalZoneSearchField() {
|
||||
return Padding(
|
||||
padding:
|
||||
const EdgeInsets.only(left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
child: Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: inputGeo,
|
||||
decoration: InputDecoration(
|
||||
labelText: AppLocalizations.of(context)?.location ?? "Location",
|
||||
border: OutlineInputBorder(),
|
||||
suffixIcon: IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
inputGeo.clear(); // Clear the text field
|
||||
geographicalZone = ''; // Reset the geographical zone state
|
||||
suggestions.clear(); // Optionally clear suggestions
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
geographicalZone = value;
|
||||
searchSuggestions(value);
|
||||
});
|
||||
},
|
||||
),
|
||||
if (suggestions.isNotEmpty)
|
||||
Container(
|
||||
height: 200,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.blue),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: suggestions.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Text(suggestions[index]['text']),
|
||||
subtitle: Text(suggestions[index]['place_name']),
|
||||
onTap: () async {
|
||||
print("suggestion tapped : ${suggestions[index]}");
|
||||
final latitude =
|
||||
suggestions[index]['geometry']['coordinates'][1];
|
||||
final longitude =
|
||||
suggestions[index]['geometry']['coordinates'][0];
|
||||
|
||||
setState(() {
|
||||
geographicalZone = suggestions[index]['text'];
|
||||
inputGeo.text = geographicalZone;
|
||||
suggestions.clear();
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> popCamera() async {
|
||||
await availableCameras().then((value) => Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) =>
|
||||
CameraEdit(camera: value.first, events: widget.events))));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
drawer: MyDrawer(),
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)?.add_event ??
|
||||
"Add or Update a event"),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!)),
|
||||
if (widget.imgPath.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 60.0),
|
||||
child: Center(
|
||||
child: Container(
|
||||
width: 200,
|
||||
height: 150,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(100.0)),
|
||||
child: Image.file(File(widget.imgPath))),
|
||||
),
|
||||
),
|
||||
if (widget.imgPath.isEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 60.0),
|
||||
child: Image.network(
|
||||
imgUrl,
|
||||
width: MediaQuery.of(context).size.width *
|
||||
0.5, // 50% of screen width
|
||||
height: MediaQuery.of(context).size.height * 0.5,
|
||||
loadingBuilder: (BuildContext context, Widget child,
|
||||
ImageChunkEvent? loadingProgress) {
|
||||
if (loadingProgress == null) {
|
||||
return child; // The image has finished loading
|
||||
}
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
},
|
||||
errorBuilder: (BuildContext context, Object error,
|
||||
StackTrace? stackTrace) {
|
||||
return Center(
|
||||
child: Icon(Icons.error,
|
||||
size: MediaQuery.of(context).size.width * 0.1),
|
||||
);
|
||||
},
|
||||
)),
|
||||
Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: popCamera,
|
||||
icon: Icon(Icons.edit, size: 16), // Edit icon
|
||||
label: Text(AppLocalizations.of(context)?.edit_image ??
|
||||
"Edit pictures"), // Button text
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.blue, // Button color
|
||||
foregroundColor: Colors.white, // Text color
|
||||
padding:
|
||||
EdgeInsets.symmetric(horizontal: 10, vertical: 5),
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
//padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
|
||||
padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputName,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.name ?? "Name",
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.edit_event_name ??
|
||||
"Edit event name"),
|
||||
),
|
||||
),
|
||||
_buildGeographicalZoneSearchField(),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: startDatepicker,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.start_date ??
|
||||
"Start date",
|
||||
hintText: AppLocalizations.of(context)?.select_date ??
|
||||
"Click to select a date"),
|
||||
onTap: () => onTapFunctionDatePicker(
|
||||
context: context, position: "start")),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: startTimepicker,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.start_time ??
|
||||
"Start time",
|
||||
hintText: AppLocalizations.of(context)?.select_time ??
|
||||
"Click to select a time"),
|
||||
onTap: () => onTapFunctionTimePicker(
|
||||
context: context, position: "start")),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: endDatepicker,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.end_date ??
|
||||
"End date",
|
||||
hintText: AppLocalizations.of(context)?.select_time ??
|
||||
"Click to select a date"),
|
||||
onTap: () => onTapFunctionDatePicker(
|
||||
context: context, position: "end")),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: endTimepicker,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.end_time ??
|
||||
"End time",
|
||||
hintText: AppLocalizations.of(context)?.select_time ??
|
||||
"Click to select a time"),
|
||||
onTap: () => onTapFunctionTimePicker(
|
||||
context: context, position: "end")),
|
||||
),
|
||||
Padding(
|
||||
//padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
child: TextFormField(
|
||||
controller: inputLink,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.link ?? "Link",
|
||||
hintText: AppLocalizations.of(context)?.edit_link ??
|
||||
"Edit link event"),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
//padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
child: TextFormField(
|
||||
controller: inputTicket,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.ticket ?? "Ticket",
|
||||
hintText: AppLocalizations.of(context)?.edit_ticket ??
|
||||
"Edit ticket link"),
|
||||
),
|
||||
),
|
||||
TextFieldTags<String>(
|
||||
textfieldTagsController: _stringTagController,
|
||||
initialTags: initialTags,
|
||||
textSeparators: const [' ', ','],
|
||||
validator: (String tag) {
|
||||
if (_stringTagController.getTags!.contains(tag)) {
|
||||
return AppLocalizations.of(context)?.already_tag ??
|
||||
"You have already entered this tag";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
inputFieldBuilder: (context, inputFieldValues) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
child: TextField(
|
||||
controller: inputFieldValues.textEditingController,
|
||||
focusNode: inputFieldValues.focusNode,
|
||||
onChanged: inputFieldValues.onTagChanged,
|
||||
onSubmitted: inputFieldValues.onTagSubmitted,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.tag ?? 'Tags',
|
||||
hintText: inputFieldValues.tags.isNotEmpty
|
||||
? ''
|
||||
: AppLocalizations.of(context)?.enter_tag ??
|
||||
"Enter tag...",
|
||||
errorText: inputFieldValues.error,
|
||||
prefixIcon: inputFieldValues.tags.isNotEmpty
|
||||
? SingleChildScrollView(
|
||||
controller:
|
||||
inputFieldValues.tagScrollController,
|
||||
scrollDirection: Axis.vertical,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 8,
|
||||
bottom: 8,
|
||||
left: 8,
|
||||
),
|
||||
child: Wrap(
|
||||
runSpacing: 4.0,
|
||||
spacing: 4.0,
|
||||
children: inputFieldValues.tags
|
||||
.map((String tag) {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
borderRadius: BorderRadius.all(
|
||||
Radius.circular(20.0),
|
||||
),
|
||||
color: Colors.blue,
|
||||
),
|
||||
margin:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 5.0),
|
||||
padding:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 10.0,
|
||||
vertical: 5.0),
|
||||
child: Row(
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
InkWell(
|
||||
child: Text(
|
||||
'$tag',
|
||||
style: const TextStyle(
|
||||
color: Colors.white),
|
||||
),
|
||||
onTap: () {
|
||||
//print("$tag selected");
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 4.0),
|
||||
InkWell(
|
||||
child: const Icon(
|
||||
Icons.cancel,
|
||||
size: 14.0,
|
||||
color: Color.fromARGB(
|
||||
255, 233, 233, 233),
|
||||
),
|
||||
onTap: () {
|
||||
inputFieldValues
|
||||
.onTagRemoved(tag);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList()),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
TextFieldTags<String>(
|
||||
textfieldTagsController: _stringOrgaController,
|
||||
initialTags: initialOrga,
|
||||
textSeparators: const [','],
|
||||
validator: (String tag) {
|
||||
if (_stringOrgaController.getTags!.contains(tag)) {
|
||||
return AppLocalizations.of(context)
|
||||
?.already_organiser ??
|
||||
"You have already entered this organizer";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
inputFieldBuilder: (context, inputFieldValues) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
child: TextField(
|
||||
controller: inputFieldValues.textEditingController,
|
||||
focusNode: inputFieldValues.focusNode,
|
||||
onChanged: inputFieldValues.onTagChanged,
|
||||
onSubmitted: inputFieldValues.onTagSubmitted,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.organizer ??
|
||||
"Organizer",
|
||||
hintText: inputFieldValues.tags.isNotEmpty
|
||||
? ''
|
||||
: AppLocalizations.of(context)
|
||||
?.enter_organizer ??
|
||||
"Enter a organizer",
|
||||
errorText: inputFieldValues.error,
|
||||
prefixIcon: inputFieldValues.tags.isNotEmpty
|
||||
? SingleChildScrollView(
|
||||
controller:
|
||||
inputFieldValues.tagScrollController,
|
||||
scrollDirection: Axis.vertical,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 8,
|
||||
bottom: 8,
|
||||
left: 8,
|
||||
),
|
||||
child: Wrap(
|
||||
runSpacing: 4.0,
|
||||
spacing: 4.0,
|
||||
children: inputFieldValues.tags
|
||||
.map((String tag) {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
borderRadius: BorderRadius.all(
|
||||
Radius.circular(20.0),
|
||||
),
|
||||
color: Colors.blue,
|
||||
),
|
||||
margin:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 5.0),
|
||||
padding:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 10.0,
|
||||
vertical: 5.0),
|
||||
child: Row(
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
InkWell(
|
||||
child: Text(
|
||||
'$tag',
|
||||
style: const TextStyle(
|
||||
color: Colors.white),
|
||||
),
|
||||
onTap: () {
|
||||
//print("$tag selected");
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 4.0),
|
||||
InkWell(
|
||||
child: const Icon(
|
||||
Icons.cancel,
|
||||
size: 14.0,
|
||||
color: Color.fromARGB(
|
||||
255, 233, 233, 233),
|
||||
),
|
||||
onTap: () {
|
||||
inputFieldValues
|
||||
.onTagRemoved(tag);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList()),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextField(
|
||||
controller: inputDesc,
|
||||
keyboardType: TextInputType.multiline,
|
||||
maxLines: 10,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.description ??
|
||||
'Description',
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.describe_event ??
|
||||
'Describe event'),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Container(
|
||||
height: 50,
|
||||
width: 250,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(20)),
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_updateEvent(context);
|
||||
}
|
||||
},
|
||||
child: Text(
|
||||
AppLocalizations.of(context)?.add ?? 'Add',
|
||||
style: TextStyle(color: Colors.white, fontSize: 25),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
396
covas_mobile_old/lib/pages/EditProfile.dart
Normal file
396
covas_mobile_old/lib/pages/EditProfile.dart
Normal file
@@ -0,0 +1,396 @@
|
||||
import 'package:covas_mobile/classes/MyDrawer.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import '../classes/MyDrawer.dart';
|
||||
import '../pages/LoginDemo.dart';
|
||||
|
||||
import '../classes/alert.dart';
|
||||
import '../classes/eventAdded.dart';
|
||||
|
||||
import '../variable/globals.dart' as globals;
|
||||
|
||||
import '../classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; // Créé
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await MobileAds.instance.initialize();
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: EditProfile(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EditProfile extends StatefulWidget {
|
||||
const EditProfile({super.key});
|
||||
|
||||
@override
|
||||
_EditProfileState createState() => _EditProfileState();
|
||||
}
|
||||
|
||||
class _EditProfileState extends State<EditProfile>
|
||||
with ShowAlertDialog, ShowEventDialog {
|
||||
BannerAd? _bannerAd;
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
TextEditingController inputUserName = TextEditingController();
|
||||
|
||||
TextEditingController inputName = TextEditingController();
|
||||
|
||||
TextEditingController inputFirstName = TextEditingController();
|
||||
TextEditingController inputEmail = TextEditingController();
|
||||
TextEditingController inputBirth = TextEditingController();
|
||||
TextEditingController inputPassword = TextEditingController();
|
||||
TextEditingController inputPasswordConfirmed = TextEditingController();
|
||||
|
||||
onTapFunctionDatePicker({required BuildContext context}) async {
|
||||
DateTime initialDate = DateTime.parse(formatDate(inputBirth.text));
|
||||
|
||||
DateTime? pickedDate = await showDatePicker(
|
||||
context: context,
|
||||
firstDate: DateTime(1900),
|
||||
initialDate: initialDate,
|
||||
lastDate: DateTime(2104));
|
||||
if (pickedDate == null) return;
|
||||
inputBirth.text = DateFormat("dd/MM/yyyy").format(pickedDate);
|
||||
}
|
||||
|
||||
convertNulltoEmptyString(var check) {
|
||||
if (check == null) {
|
||||
return "";
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
convertNulltoArray(List<String> check) {
|
||||
if (check == null) {
|
||||
return [];
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
String formatDate(String date) {
|
||||
var splitedDate = date.split("/");
|
||||
|
||||
var day = splitedDate[0];
|
||||
var month = splitedDate[1];
|
||||
var year = splitedDate[2];
|
||||
|
||||
return "${year}-${month}-${day}";
|
||||
}
|
||||
|
||||
Future<void> _updateProfile(BuildContext context) async {
|
||||
var username = inputUserName.text;
|
||||
var firstName = inputFirstName.text;
|
||||
var name = inputName.text;
|
||||
var email = inputEmail.text;
|
||||
var password = inputPassword.text;
|
||||
var confirmedPassword = inputPasswordConfirmed.text;
|
||||
var birth = DateTime.parse(formatDate(inputBirth.text));
|
||||
|
||||
if ((password.isNotEmpty) && (confirmedPassword.isNotEmpty)) {
|
||||
if (password != confirmedPassword) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.different_password_error ??
|
||||
"Different password");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var urlPut = Uri.parse("${globals.api}/users/me");
|
||||
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
if (accessToken.isNotEmpty) {
|
||||
var responsePut = await http.put(urlPut,
|
||||
headers: {
|
||||
HttpHeaders.cookieHeader: 'access_token=${accessToken}',
|
||||
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
|
||||
HttpHeaders.contentTypeHeader: 'application/json'
|
||||
},
|
||||
body: jsonEncode({
|
||||
'name': name,
|
||||
'username': username,
|
||||
'firstName': firstName,
|
||||
'password': password,
|
||||
'email': email,
|
||||
'roles': '',
|
||||
'birth': birth.toString()
|
||||
}));
|
||||
print(responsePut.statusCode);
|
||||
if (responsePut.statusCode == 200) {
|
||||
showEventDialog(context,
|
||||
AppLocalizations.of(context)?.user_update ?? "Your user updated");
|
||||
Navigator.pushReplacement(
|
||||
context, MaterialPageRoute(builder: (_) => EditProfile()));
|
||||
return;
|
||||
}
|
||||
|
||||
final messages = {
|
||||
400: AppLocalizations.of(context)?.request_error ??
|
||||
"Poorly constructed query",
|
||||
406: AppLocalizations.of(context)?.incorrect_password ??
|
||||
"Incorrect password",
|
||||
404: AppLocalizations.of(context)?.unknown_user ?? "Unknown user",
|
||||
403: AppLocalizations.of(context)?.disabled_user ?? "Disabled user",
|
||||
410: AppLocalizations.of(context)?.invalid_token ?? "Invalid token",
|
||||
500: AppLocalizations.of(context)?.internal_error_server ??
|
||||
"Internal error server"
|
||||
};
|
||||
final text = messages[responsePut.statusCode] ??
|
||||
AppLocalizations.of(context)?.unknown_error_auth ??
|
||||
"Unknown error auth";
|
||||
showAlertDialog(
|
||||
context, AppLocalizations.of(context)?.error ?? "Error", text);
|
||||
} else {
|
||||
Navigator.pushReplacement(
|
||||
context, MaterialPageRoute(builder: (_) => LoginDemo()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _getInfoProfile() async {
|
||||
var urlGet = Uri.parse("${globals.api}/users/me");
|
||||
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
if (accessToken.isNotEmpty) {
|
||||
var responseGet = await http.get(urlGet, headers: {
|
||||
HttpHeaders.cookieHeader: 'access_token=${accessToken}',
|
||||
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
|
||||
HttpHeaders.contentTypeHeader: 'application/json'
|
||||
});
|
||||
print(responseGet.statusCode);
|
||||
if (responseGet.statusCode == 200) {
|
||||
var body = json.decode(utf8.decode(responseGet.bodyBytes));
|
||||
setState(() {
|
||||
inputName.text = body["name"];
|
||||
inputFirstName.text = body["firstName"];
|
||||
inputUserName.text = body["username"];
|
||||
inputEmail.text = body["email"];
|
||||
inputBirth.text =
|
||||
DateFormat("dd/MM/yyyy").format(DateTime.parse(body["birth"]));
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
final messages = {
|
||||
400: AppLocalizations.of(context)?.request_error ??
|
||||
"Poorly constructed query",
|
||||
406: AppLocalizations.of(context)?.incorrect_password ??
|
||||
"Incorrect password",
|
||||
404: AppLocalizations.of(context)?.unknown_user ?? "Unknown user",
|
||||
403: AppLocalizations.of(context)?.disabled_user ?? "Disabled user",
|
||||
410: AppLocalizations.of(context)?.invalid_token ?? "Invalid token",
|
||||
500: AppLocalizations.of(context)?.internal_error_server ??
|
||||
"Internal error server"
|
||||
};
|
||||
final text = messages[responseGet.statusCode] ??
|
||||
AppLocalizations.of(context)?.unknown_error_auth ??
|
||||
"Unknown error auth";
|
||||
showAlertDialog(
|
||||
context, AppLocalizations.of(context)?.error ?? "Error", text);
|
||||
} else {
|
||||
Navigator.pushReplacement(
|
||||
context, MaterialPageRoute(builder: (_) => LoginDemo()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
_getInfoProfile();
|
||||
}
|
||||
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String? _validateField(String? value) {
|
||||
return value!.isEmpty
|
||||
? AppLocalizations.of(context)?.required_input ?? "Required input"
|
||||
: null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
AppLocalizations.of(context)?.update_profile ?? "Update profile"),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
drawer: MyDrawer(),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!)),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputUserName,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.name,
|
||||
hintText: AppLocalizations.of(context)?.edit_pseudo ??
|
||||
"Edit pseudo"),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputPassword,
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.password ??
|
||||
"Password",
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.enter_password ??
|
||||
"Enter a password"),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputPasswordConfirmed,
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.password_confirmed ??
|
||||
"Must confirm password",
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.password_confirmed ??
|
||||
"Must confirm password"),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputName,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.last_name ??
|
||||
"Last name",
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.edit_last_name ??
|
||||
"Edit last name"),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputFirstName,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.first_name ??
|
||||
"First name",
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.edit_first_name ??
|
||||
"Edit first name"),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputEmail,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.email ?? "Email",
|
||||
hintText: AppLocalizations.of(context)?.edit_email ??
|
||||
"Edit email"),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputBirth,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.birth_date,
|
||||
hintText: AppLocalizations.of(context)?.edit_birth ??
|
||||
"Click to select a birth date"),
|
||||
onTap: () => onTapFunctionDatePicker(context: context)),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Container(
|
||||
height: 50,
|
||||
width: 250,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(20)),
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_updateProfile(context);
|
||||
}
|
||||
},
|
||||
child: Text(
|
||||
AppLocalizations.of(context)?.update_profile ??
|
||||
"Update profile ",
|
||||
style: TextStyle(color: Colors.white, fontSize: 25),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
170
covas_mobile_old/lib/pages/EditSettings.dart
Normal file
170
covas_mobile_old/lib/pages/EditSettings.dart
Normal file
@@ -0,0 +1,170 @@
|
||||
import 'package:covas_mobile/classes/MyDrawer.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import '../classes/MyDrawer.dart';
|
||||
|
||||
import '../classes/alert.dart';
|
||||
import '../classes/eventAdded.dart';
|
||||
|
||||
import '../classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; // Créé
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await MobileAds.instance.initialize();
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: EditSettings(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class EditSettings extends StatefulWidget {
|
||||
const EditSettings({super.key});
|
||||
|
||||
@override
|
||||
_EditProfileState createState() => _EditProfileState();
|
||||
}
|
||||
|
||||
class _EditProfileState extends State<EditSettings>
|
||||
with ShowAlertDialog, ShowEventDialog {
|
||||
BannerAd? _bannerAd;
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
TextEditingController inputUserName = TextEditingController();
|
||||
int? kilometer;
|
||||
|
||||
Future<void> getParameter() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
setState(() {
|
||||
var kilometer = prefs.getDouble("kilometer")?.toInt() ?? null;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> setParameter() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
if (kilometer != null) {
|
||||
prefs.setDouble("kilometer", kilometer?.toDouble() ?? 50);
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.updated ?? "Updated",
|
||||
AppLocalizations.of(context)?.settings_updated ?? "Settings updated");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
getParameter();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)?.settings ?? "Settings"),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
drawer: MyDrawer(),
|
||||
body: Form(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!)),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0,
|
||||
right: 15.0,
|
||||
top: 15.0,
|
||||
bottom: 0.0,
|
||||
),
|
||||
child: DropdownButtonFormField<int>(
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.define_kilometer ??
|
||||
'Define kilometer',
|
||||
),
|
||||
value:
|
||||
kilometer, // Set the initial selected value here, or leave as `null` if unselected.
|
||||
items: [
|
||||
DropdownMenuItem(
|
||||
value: 5,
|
||||
child: Text('5km'),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
value: 25,
|
||||
child: Text('25km'),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
value: 50,
|
||||
child: Text('50km'),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
value: 75,
|
||||
child: Text('75km'),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
value: 100,
|
||||
child: Text('100km'),
|
||||
),
|
||||
],
|
||||
onChanged: (int? newValue) {
|
||||
// Handle selection
|
||||
kilometer = newValue;
|
||||
},
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Container(
|
||||
height: 50,
|
||||
width: 250,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(20)),
|
||||
child: TextButton(
|
||||
onPressed: () {},
|
||||
child: Text(
|
||||
AppLocalizations.of(context)?.update ?? "Update",
|
||||
style: TextStyle(color: Colors.white, fontSize: 25),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
176
covas_mobile_old/lib/pages/ForgotPassword.dart
Normal file
176
covas_mobile_old/lib/pages/ForgotPassword.dart
Normal file
@@ -0,0 +1,176 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import '../main.dart';
|
||||
|
||||
import '../classes/alert.dart';
|
||||
|
||||
import '../variable/globals.dart' as globals;
|
||||
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; //
|
||||
|
||||
void main() {
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: PasswordForgot(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class PasswordForgot extends StatefulWidget {
|
||||
const PasswordForgot({super.key});
|
||||
|
||||
@override
|
||||
_PasswordForgotState createState() => _PasswordForgotState();
|
||||
}
|
||||
|
||||
class _PasswordForgotState extends State<PasswordForgot> with ShowAlertDialog {
|
||||
TextEditingController inputEmail = TextEditingController();
|
||||
|
||||
convertNulltoEmptyString(var check) {
|
||||
if (check == null) {
|
||||
return "";
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
convertNulltoArray(List<String> check) {
|
||||
if (check == null) {
|
||||
return [];
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
String formatDate(String date) {
|
||||
var splitedDate = date.split("/");
|
||||
|
||||
var day = splitedDate[0];
|
||||
var month = splitedDate[1];
|
||||
var year = splitedDate[2];
|
||||
|
||||
return "${year}-${month}-${day}";
|
||||
}
|
||||
|
||||
Future<void> _forgotPassword(BuildContext context) async {
|
||||
var email = inputEmail.text;
|
||||
|
||||
var urlPut = Uri.parse("${globals.api}/password/forgot");
|
||||
|
||||
var responsePost = await http.post(urlPut,
|
||||
headers: {
|
||||
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
|
||||
HttpHeaders.contentTypeHeader: 'application/json'
|
||||
},
|
||||
body: jsonEncode({
|
||||
'email': email,
|
||||
}));
|
||||
print(responsePost.statusCode);
|
||||
if (responsePost.statusCode == 200) {
|
||||
String message =
|
||||
AppLocalizations.of(context)?.email_sent ?? "Email has been sent";
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.create ?? "Creation",
|
||||
"${message} : ${email}");
|
||||
return;
|
||||
}
|
||||
|
||||
final messages = {
|
||||
400: AppLocalizations.of(context)?.request_error ??
|
||||
"Poorly constructed query",
|
||||
406: AppLocalizations.of(context)?.incorrect_password ??
|
||||
"Incorrect password",
|
||||
404: AppLocalizations.of(context)?.unknown_user ?? "Unknown user",
|
||||
403: AppLocalizations.of(context)?.disabled_user ?? "Disabled user",
|
||||
410: AppLocalizations.of(context)?.invalid_token ?? "Invalid token",
|
||||
500: AppLocalizations.of(context)?.internal_error_server ??
|
||||
"Internal error server"
|
||||
};
|
||||
|
||||
final text = messages[responsePost.statusCode] ??
|
||||
AppLocalizations.of(context)?.unknown_error_auth ??
|
||||
"Unknown error auth";
|
||||
showAlertDialog(
|
||||
context, AppLocalizations.of(context)?.error ?? "Error", text);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
}
|
||||
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String? _validateField(String? value) {
|
||||
return value!.isEmpty ? 'Champ requis' : null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)?.forgot_password ??
|
||||
"Forgot password"),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputEmail,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.email ?? 'Email',
|
||||
hintText: AppLocalizations.of(context)?.enter_email ??
|
||||
'Enter the email'),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Container(
|
||||
height: 50,
|
||||
width: 250,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(20)),
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_forgotPassword(context);
|
||||
}
|
||||
},
|
||||
child: Text(
|
||||
AppLocalizations.of(context)?.send_email ?? 'Send email',
|
||||
style: TextStyle(color: Colors.white, fontSize: 25),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
455
covas_mobile_old/lib/pages/ItemMenu.dart
Normal file
455
covas_mobile_old/lib/pages/ItemMenu.dart
Normal file
@@ -0,0 +1,455 @@
|
||||
// ignore_for_file: unnecessary_brace_in_string_interps
|
||||
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:covas_mobile/classes/alert.dart';
|
||||
import 'package:covas_mobile/pages/ListItemByTags.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:intl/date_symbol_data_local.dart';
|
||||
|
||||
import '../variable/globals.dart' as globals;
|
||||
|
||||
import '../classes/events.dart';
|
||||
import '../classes/MyDrawer.dart';
|
||||
|
||||
import 'ListItemMenu.dart';
|
||||
import 'MapboxPages.dart';
|
||||
import 'ListItemByOrganizers.dart';
|
||||
import 'EditEvent.dart';
|
||||
|
||||
import '../classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; //
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await MobileAds.instance.initialize();
|
||||
initializeDateFormatting("fr_FR", null).then((_) => (const MyApp()));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
// This widget is the root of your application.
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
// This is the theme of your application.
|
||||
//
|
||||
// Try running your application with "flutter run". You'll see the
|
||||
// application has a blue toolbar. Then, without quitting the app, try
|
||||
// changing the primarySwatch below to Colors.green and then invoke
|
||||
// "hot reload" (press "r" in the console where you ran "flutter run",
|
||||
// or simply save your changes to "hot reload" in a Flutter IDE).
|
||||
// Notice that the counter didn't reset back to zero; the application
|
||||
// is not restarted.
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: const ItemMenu(title: 'Flutter Demo Home Page'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ItemMenu extends StatefulWidget {
|
||||
const ItemMenu({Key? key, required this.title}) : super(key: key);
|
||||
|
||||
// This widget is the home page of your application. It is stateful, meaning
|
||||
// that it has a State object (defined below) that contains fields that affect
|
||||
// how it looks.
|
||||
|
||||
// This class is the configuration for the state. It holds the values (in this
|
||||
// case the title) provided by the parent (in this case the App widget) and
|
||||
// used by the build method of the State. Fields in a Widget subclass are
|
||||
// always marked "final".
|
||||
|
||||
final String title;
|
||||
@override
|
||||
State<ItemMenu> createState() => _ItemMenuState();
|
||||
}
|
||||
|
||||
class _ItemMenuState extends State<ItemMenu> with ShowAlertDialog {
|
||||
BannerAd? _bannerAd;
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
String listUser = "";
|
||||
String eventName = "";
|
||||
String eventStartDate = "";
|
||||
String eventDescription = "";
|
||||
String eventTicket = "";
|
||||
String eventLink = "";
|
||||
String place = "";
|
||||
String imgUrl = "";
|
||||
List<String> tags = [];
|
||||
List<String> organizers = [];
|
||||
|
||||
String id = "";
|
||||
|
||||
Events? events;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
|
||||
_getEventInfos();
|
||||
}
|
||||
|
||||
Future<void> _getEventInfos() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final accessToken = prefs.getString("access_token") ?? "";
|
||||
|
||||
if (accessToken.isEmpty) {
|
||||
showAlertDialog(context, AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.invalid_cache ?? "Invalid cache");
|
||||
return;
|
||||
}
|
||||
|
||||
final urlGet = Uri.parse("${globals.api}/events/${widget.title}");
|
||||
final responseGet = await http.get(
|
||||
urlGet,
|
||||
headers: {HttpHeaders.cookieHeader: 'access_token=$accessToken'},
|
||||
);
|
||||
|
||||
if (responseGet.statusCode == 200) {
|
||||
final responseBody = utf8.decode(responseGet.bodyBytes);
|
||||
|
||||
events = Events.fromJson(jsonDecode(responseBody));
|
||||
final locale = Provider.of<LocaleProvider>(context, listen: false)
|
||||
.locale
|
||||
?.toString() ??
|
||||
'en_US';
|
||||
final startDate =
|
||||
DateTime.parse(events?.startDate ?? DateTime.now().toString());
|
||||
//final date = DateFormat.yMd().format(startDate);
|
||||
//final time = DateFormat.Hm().format(startDate);
|
||||
final endDate =
|
||||
DateTime.parse(events?.endDate ?? DateTime.now().toString());
|
||||
String separator = AppLocalizations.of(context)?.at ?? "at";
|
||||
final formattedStartDate =
|
||||
DateFormat("EEEE d MMMM y '${separator}' HH:mm", locale)
|
||||
.format(startDate);
|
||||
|
||||
final formattedEndDate =
|
||||
DateFormat("EEEE d MMMM y '${separator}' HH:mm", locale)
|
||||
.format(endDate);
|
||||
|
||||
String link = AppLocalizations.of(context)?.to_date ?? "to";
|
||||
|
||||
setState(() {
|
||||
eventName = events?.name ?? "";
|
||||
|
||||
eventStartDate = "$formattedStartDate ${link} $formattedEndDate";
|
||||
organizers = List<String>.from(events?.organizers ?? []);
|
||||
place = events?.place ?? "";
|
||||
imgUrl = events?.imgUrl ?? "";
|
||||
eventLink = events?.link ?? "";
|
||||
eventTicket = events?.ticket ?? "";
|
||||
eventDescription = events?.description ?? "";
|
||||
tags = List<String>.from(events?.tags ?? []);
|
||||
});
|
||||
} else {
|
||||
final messages = {
|
||||
400: AppLocalizations.of(context)?.request_error ??
|
||||
"Poorly constructed query",
|
||||
406: AppLocalizations.of(context)?.incorrect_password ??
|
||||
"Incorrect password",
|
||||
404: AppLocalizations.of(context)?.unknown_user ?? "Unknown user",
|
||||
403: AppLocalizations.of(context)?.disabled_user ?? "Disabled user",
|
||||
410: AppLocalizations.of(context)?.invalid_token ?? "Invalid token",
|
||||
500: AppLocalizations.of(context)?.internal_error_server ??
|
||||
"Internal error server"
|
||||
};
|
||||
|
||||
final errorMessage = messages[responseGet.statusCode] ??
|
||||
AppLocalizations.of(context)?.unknown_error_auth ??
|
||||
"Unknown error auth";
|
||||
showAlertDialog(context, AppLocalizations.of(context)?.error ?? "Error",
|
||||
errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// This method is rerun every time setState is called, for instance as done
|
||||
// by the _incrementCounter method above.
|
||||
//
|
||||
// The Flutter framework has been optimized to make rerunning build methods
|
||||
// fast, so that you can just rebuild anything that needs updating rather
|
||||
// than having to individually change instances of widgets.
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
// Here we take the value from the MyHomePage object that was created by
|
||||
// the App.build method, and use it to set our appbar title.
|
||||
title: Text("${eventName}", overflow: TextOverflow.ellipsis),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
leading: IconButton(
|
||||
icon: Icon(Icons.arrow_back),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context, MaterialPageRoute(builder: (_) => ListItemMenu()));
|
||||
},
|
||||
)),
|
||||
drawer: MyDrawer(),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!)),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 60.0),
|
||||
child: Image.network(
|
||||
imgUrl,
|
||||
width: MediaQuery.of(context).size.width *
|
||||
0.5, // 50% of screen width
|
||||
height: MediaQuery.of(context).size.height * 0.5,
|
||||
loadingBuilder: (BuildContext context, Widget child,
|
||||
ImageChunkEvent? loadingProgress) {
|
||||
if (loadingProgress == null) {
|
||||
return child; // The image has finished loading
|
||||
}
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
},
|
||||
errorBuilder: (BuildContext context, Object error,
|
||||
StackTrace? stackTrace) {
|
||||
return Center(
|
||||
child: Icon(Icons.error,
|
||||
size: MediaQuery.of(context).size.width * 0.1),
|
||||
);
|
||||
},
|
||||
)),
|
||||
Row(children: [
|
||||
Icon(Icons.event),
|
||||
Text(
|
||||
AppLocalizations.of(context)?.item_date ?? "Date : ",
|
||||
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
|
||||
)
|
||||
]),
|
||||
Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text("${eventStartDate}",
|
||||
style: TextStyle(fontSize: 15.0)))
|
||||
],
|
||||
),
|
||||
Row(children: [
|
||||
Icon(Icons.explore),
|
||||
Text(
|
||||
AppLocalizations.of(context)?.item_maps ?? "Maps : ",
|
||||
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
|
||||
)
|
||||
]),
|
||||
Row(children: [
|
||||
Flexible(
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => MapboxPages(
|
||||
title: '${widget.title}',
|
||||
place: '${place}')));
|
||||
},
|
||||
child: Text("${place}",
|
||||
style: TextStyle(fontSize: 15.0),
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis)))
|
||||
]),
|
||||
Row(children: [
|
||||
Icon(Icons.link),
|
||||
Text(
|
||||
AppLocalizations.of(context)?.item_link ?? "Link : ",
|
||||
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
|
||||
)
|
||||
]),
|
||||
Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child:
|
||||
Text("${eventLink}", style: TextStyle(fontSize: 15.0)))
|
||||
],
|
||||
),
|
||||
Row(children: [
|
||||
Icon(Icons.add_shopping_cart),
|
||||
Text(
|
||||
AppLocalizations.of(context)?.item_ticket ?? "Ticket : ",
|
||||
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
|
||||
)
|
||||
]),
|
||||
Row(
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text("${eventTicket}",
|
||||
style: TextStyle(fontSize: 15.0)))
|
||||
],
|
||||
),
|
||||
Row(children: [
|
||||
Icon(Icons.group),
|
||||
Text(
|
||||
AppLocalizations.of(context)?.item_organizer ?? "Organizers : ",
|
||||
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
|
||||
)
|
||||
]),
|
||||
Row(children: [
|
||||
Flexible(
|
||||
flex: 3,
|
||||
fit: FlexFit.tight,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 8,
|
||||
bottom: 8,
|
||||
left: 8,
|
||||
),
|
||||
child: Wrap(
|
||||
runSpacing: 2.0,
|
||||
spacing: 2.0,
|
||||
children: organizers.map((String tag) {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
borderRadius: BorderRadius.all(
|
||||
Radius.circular(20.0),
|
||||
),
|
||||
color: Colors.blue,
|
||||
),
|
||||
margin: const EdgeInsets.symmetric(horizontal: 5.0),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 10.0, vertical: 5.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
InkWell(
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ListItemOrganizers(
|
||||
organizer: '$tag')));
|
||||
},
|
||||
child: Text(
|
||||
'$tag',
|
||||
style: const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList()),
|
||||
)),
|
||||
]),
|
||||
Row(children: [
|
||||
Icon(Icons.description),
|
||||
Text(
|
||||
AppLocalizations.of(context)?.item_description ??
|
||||
"Description : ",
|
||||
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold))
|
||||
]),
|
||||
Row(children: [
|
||||
Flexible(
|
||||
child: Text("${eventDescription}",
|
||||
style: TextStyle(fontSize: 15.0),
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis))
|
||||
]),
|
||||
Row(children: [
|
||||
Icon(Icons.category),
|
||||
Text(AppLocalizations.of(context)?.item_tags ?? "Tags : ",
|
||||
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold))
|
||||
]),
|
||||
Row(
|
||||
children: [
|
||||
Flexible(
|
||||
flex: 3,
|
||||
fit: FlexFit.tight,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 8,
|
||||
bottom: 8,
|
||||
left: 8,
|
||||
),
|
||||
child: Wrap(
|
||||
runSpacing: 2.0,
|
||||
spacing: 2.0,
|
||||
children: tags.map((String tag) {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
borderRadius: BorderRadius.all(
|
||||
Radius.circular(20.0),
|
||||
),
|
||||
color: Colors.blue,
|
||||
),
|
||||
margin:
|
||||
const EdgeInsets.symmetric(horizontal: 5.0),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 10.0, vertical: 5.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
InkWell(
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) =>
|
||||
ListItemTags(tags: '$tag')));
|
||||
},
|
||||
child: Text(
|
||||
'$tag',
|
||||
style:
|
||||
const TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList()),
|
||||
)),
|
||||
],
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => EditEvent(
|
||||
events: events,
|
||||
imgPath: "",
|
||||
)),
|
||||
);
|
||||
},
|
||||
backgroundColor: Colors.blue,
|
||||
tooltip: AppLocalizations.of(context)?.search ?? 'Search',
|
||||
child: const Icon(Icons.edit, color: Colors.white),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
275
covas_mobile_old/lib/pages/ListItemByOrganizers.dart
Normal file
275
covas_mobile_old/lib/pages/ListItemByOrganizers.dart
Normal file
@@ -0,0 +1,275 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import "ItemMenu.dart";
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:flutter/material.dart';
|
||||
import '../classes/events.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:intl/date_symbol_data_local.dart';
|
||||
|
||||
import '../variable/globals.dart' as globals;
|
||||
import '../classes/MyDrawer.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; //
|
||||
import '../classes/notification_service.dart';
|
||||
|
||||
// app starting point
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
await initializeDateFormatting("fr_FR", null);
|
||||
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
localizationsDelegates: [
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: [
|
||||
const Locale('fr', 'FR'),
|
||||
],
|
||||
home: const ListItemOrganizers(organizer: "default"),
|
||||
debugShowCheckedModeBanner: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// homepage class
|
||||
class ListItemOrganizers extends StatefulWidget {
|
||||
const ListItemOrganizers({Key? key, required this.organizer})
|
||||
: super(key: key);
|
||||
|
||||
final String organizer;
|
||||
@override
|
||||
State<ListItemOrganizers> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
// homepage state
|
||||
class _MyHomePageState extends State<ListItemOrganizers> {
|
||||
int _fetchCount = 0;
|
||||
bool _isLoading = false;
|
||||
late ScrollController _scrollController;
|
||||
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
void _incrementFetchCount() {
|
||||
setState(() {
|
||||
_fetchCount++;
|
||||
});
|
||||
}
|
||||
|
||||
void _scrollListener() {
|
||||
if (_scrollController.position.pixels ==
|
||||
_scrollController.position.maxScrollExtent) {
|
||||
_incrementFetchCount();
|
||||
_fetchData();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _fetchData() async {
|
||||
if (_isLoading) return;
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
await Future.delayed(Duration(seconds: 2));
|
||||
getPosts(widget.organizer, count: _fetchCount);
|
||||
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> toggleInterested(String eventId) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
final url = Uri.parse("${globals.api}/events/${eventId}/interest");
|
||||
if (accessToken.isNotEmpty) {
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw (AppLocalizations.of(context)?.toogle_interest ??
|
||||
"Error toogle interest: ${response.statusCode}");
|
||||
}
|
||||
|
||||
var event = json.decode(response.body);
|
||||
return event;
|
||||
}
|
||||
return {"interested": false, "interested_count": 0};
|
||||
}
|
||||
|
||||
// variable to call and store future list of posts
|
||||
|
||||
// function to fetch data from api and return future list of posts
|
||||
static Future<List<Events>> getPosts(organizer, {count = 0}) async {
|
||||
await initializeDateFormatting("fr_FR", null);
|
||||
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
final List<Events> body = [];
|
||||
if (accessToken.isNotEmpty) {
|
||||
DateTime currentDatetime = DateTime.now();
|
||||
num limit = 20 * (count + 1);
|
||||
var url = Uri.parse(
|
||||
"${globals.api}/events?organizers=${organizer}&limit=${limit}¤t_datetime=${currentDatetime.toString()}");
|
||||
final response = await http.get(url, headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=${accessToken}"
|
||||
});
|
||||
final List body = json.decode(utf8.decode(response.bodyBytes));
|
||||
return body.map((e) => Events.fromJson(e)).toList();
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
_scrollController = ScrollController();
|
||||
_scrollController.addListener(_scrollListener);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// build function
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
drawer: MyDrawer(),
|
||||
body: Center(
|
||||
// FutureBuilder
|
||||
child: FutureBuilder<List<Events>>(
|
||||
future: getPosts(widget.organizer),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
// until data is fetched, show loader
|
||||
return const CircularProgressIndicator();
|
||||
} else if (snapshot.hasData) {
|
||||
// once data is fetched, display it on screen (call buildPosts())
|
||||
final posts = snapshot.data!;
|
||||
return buildPosts(posts);
|
||||
} else {
|
||||
// if no data, show simple Text
|
||||
return Text(
|
||||
AppLocalizations.of(context)?.no_data ?? "No data available");
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// function to display fetched data on screen
|
||||
Widget buildPosts(List<Events> posts) {
|
||||
String organizer =
|
||||
AppLocalizations.of(context)?.item_organizer ?? "Organizer : ";
|
||||
// ListView Builder to show data in a list
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
// Here we take the value from the MyHomePage object that was created by
|
||||
// the App.build method, and use it to set our appbar title.
|
||||
title: Text("${organizer}${widget.organizer}",
|
||||
overflow: TextOverflow.ellipsis),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: ListView.separated(
|
||||
controller: _scrollController,
|
||||
itemCount: posts.isNotEmpty
|
||||
? posts.length + (_isLoading ? 1 : 0) // Add 1 only if loading
|
||||
: 0,
|
||||
itemBuilder: (context, index) {
|
||||
final post = posts[index];
|
||||
final startDate = DateTime.parse(post.startDate!);
|
||||
|
||||
final locale = Provider.of<LocaleProvider>(context, listen: false)
|
||||
.locale
|
||||
?.toString() ??
|
||||
'en_US';
|
||||
|
||||
final dateLongue =
|
||||
DateFormat('EEEE d MMMM y', locale).format(startDate);
|
||||
final countInterestedString =
|
||||
AppLocalizations.of(context)?.count_interested ??
|
||||
"Interested people number";
|
||||
final countInterested =
|
||||
"${countInterestedString} : ${post.interestedCount}";
|
||||
|
||||
return ListTile(
|
||||
title: Text('${post.name!}'),
|
||||
subtitle:
|
||||
Text('${post.place!}\n${dateLongue}\n${countInterested}'),
|
||||
trailing: IconButton(
|
||||
onPressed: () async {
|
||||
try {
|
||||
final result = await toggleInterested(post.id!);
|
||||
setState(() {
|
||||
post.interested = result["interested"];
|
||||
post.interestedCount = result["interested_count"];
|
||||
});
|
||||
if (result["interested"] == true) {
|
||||
NotificationService.scheduleEventNotification(
|
||||
eventId: post.id!,
|
||||
title: "Rappel évènement",
|
||||
body:
|
||||
"Ton évènement '${post.name}' commence dans 1 heure !",
|
||||
eventDate: DateTime.parse(post.startDate!),
|
||||
);
|
||||
} else {
|
||||
NotificationService.cancel(post.id!);
|
||||
}
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
AppLocalizations.of(context)?.error_update ??
|
||||
"Error when updating")),
|
||||
);
|
||||
}
|
||||
},
|
||||
icon: Icon(
|
||||
post.interested ?? false
|
||||
? Icons.favorite
|
||||
: Icons.favorite_border,
|
||||
color:
|
||||
post.interested ?? false ? Colors.red : Colors.grey)),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ItemMenu(title: post.id!)));
|
||||
});
|
||||
},
|
||||
separatorBuilder: (context, index) {
|
||||
return Divider();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
277
covas_mobile_old/lib/pages/ListItemByTags.dart
Normal file
277
covas_mobile_old/lib/pages/ListItemByTags.dart
Normal file
@@ -0,0 +1,277 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import "ItemMenu.dart";
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:flutter/material.dart';
|
||||
import '../classes/events.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:intl/date_symbol_data_local.dart';
|
||||
|
||||
import '../variable/globals.dart' as globals;
|
||||
|
||||
import '../classes/MyDrawer.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; //
|
||||
import '../classes/notification_service.dart';
|
||||
|
||||
// app starting point
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
await initializeDateFormatting("fr_FR", null);
|
||||
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
localizationsDelegates: [
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: [
|
||||
const Locale('fr', 'FR'),
|
||||
],
|
||||
home: const ListItemTags(tags: "default"),
|
||||
debugShowCheckedModeBanner: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// homepage class
|
||||
class ListItemTags extends StatefulWidget {
|
||||
const ListItemTags({Key? key, required this.tags}) : super(key: key);
|
||||
|
||||
final String tags;
|
||||
@override
|
||||
State<ListItemTags> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
// homepage state
|
||||
class _MyHomePageState extends State<ListItemTags> {
|
||||
// variable to call and store future list of posts
|
||||
|
||||
int _fetchCount = 0;
|
||||
bool _isLoading = false;
|
||||
late ScrollController _scrollController;
|
||||
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
void _incrementFetchCount() {
|
||||
setState(() {
|
||||
_fetchCount++;
|
||||
});
|
||||
}
|
||||
|
||||
void _scrollListener() {
|
||||
if (_scrollController.position.pixels ==
|
||||
_scrollController.position.maxScrollExtent) {
|
||||
_incrementFetchCount();
|
||||
|
||||
_fetchData();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _fetchData() async {
|
||||
print("Counter : ${_fetchCount}");
|
||||
if (_isLoading) return;
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
await Future.delayed(Duration(seconds: 2));
|
||||
getPosts(widget.tags, count: _fetchCount);
|
||||
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
|
||||
// function to fetch data from api and return future list of posts
|
||||
static Future<List<Events>> getPosts(tags, {count = 0}) async {
|
||||
await initializeDateFormatting("fr_FR", null);
|
||||
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
final List<Events> body = [];
|
||||
if (accessToken.isNotEmpty) {
|
||||
DateTime currentDatetime = DateTime.now();
|
||||
num limit = 20 * (count + 1);
|
||||
|
||||
var url = Uri.parse(
|
||||
"${globals.api}/events?tags=${tags}&limit=${limit}¤t_datetime=${currentDatetime.toString()}");
|
||||
final response = await http.get(url, headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=${accessToken}"
|
||||
});
|
||||
final List body = json.decode(utf8.decode(response.bodyBytes));
|
||||
return body.map((e) => Events.fromJson(e)).toList();
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> toggleInterested(String eventId) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
final url = Uri.parse("${globals.api}/events/${eventId}/interest");
|
||||
if (accessToken.isNotEmpty) {
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw (AppLocalizations.of(context)?.toogle_interest ??
|
||||
"Error toogle interest: ${response.statusCode}");
|
||||
}
|
||||
|
||||
var event = json.decode(response.body);
|
||||
return event;
|
||||
}
|
||||
return {"interested": false, "interested_count": 0};
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
_authService.checkTokenStatus(context);
|
||||
_scrollController = ScrollController();
|
||||
_scrollController.addListener(_scrollListener);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// build function
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
drawer: MyDrawer(),
|
||||
body: Center(
|
||||
// FutureBuilder
|
||||
child: FutureBuilder<List<Events>>(
|
||||
future: getPosts(widget.tags),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
// until data is fetched, show loader
|
||||
return const CircularProgressIndicator();
|
||||
} else if (snapshot.hasData) {
|
||||
// once data is fetched, display it on screen (call buildPosts())
|
||||
final posts = snapshot.data!;
|
||||
return buildPosts(posts);
|
||||
} else {
|
||||
// if no data, show simple Text
|
||||
return Text(
|
||||
AppLocalizations.of(context)?.no_data ?? "No data available");
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// function to display fetched data on screen
|
||||
Widget buildPosts(List<Events> posts) {
|
||||
// ListView Builder to show data in a list
|
||||
String tag = AppLocalizations.of(context)?.item_tags ?? "Tags : ";
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
// Here we take the value from the MyHomePage object that was created by
|
||||
// the App.build method, and use it to set our appbar title.
|
||||
title: Text("${tag}${widget.tags}", overflow: TextOverflow.ellipsis),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: ListView.separated(
|
||||
controller: _scrollController,
|
||||
itemCount: posts.isNotEmpty
|
||||
? posts.length + (_isLoading ? 1 : 0) // Add 1 only if loading
|
||||
: 0,
|
||||
itemBuilder: (context, index) {
|
||||
final post = posts[index];
|
||||
final startDate = DateTime.parse(post.startDate!);
|
||||
final locale = Provider.of<LocaleProvider>(context, listen: false)
|
||||
.locale
|
||||
?.toString() ??
|
||||
'en_US';
|
||||
final dateLongue =
|
||||
DateFormat('EEEE d MMMM y', locale).format(startDate);
|
||||
|
||||
final countInterestedString =
|
||||
AppLocalizations.of(context)?.count_interested ??
|
||||
"Interested people number";
|
||||
final countInterested =
|
||||
"${countInterestedString} : ${post.interestedCount}";
|
||||
|
||||
return ListTile(
|
||||
title: Text('${post.name!}'),
|
||||
subtitle:
|
||||
Text('${post.place!}\n${dateLongue}\n${countInterested}'),
|
||||
trailing: IconButton(
|
||||
onPressed: () async {
|
||||
try {
|
||||
final result = await toggleInterested(post.id!);
|
||||
setState(() {
|
||||
post.interested = result["interested"];
|
||||
post.interestedCount = result["interested_count"];
|
||||
});
|
||||
if (result["interested"] == true) {
|
||||
NotificationService.scheduleEventNotification(
|
||||
eventId: post.id!,
|
||||
title: "Rappel évènement",
|
||||
body:
|
||||
"Ton évènement '${post.name}' commence dans 1 heure !",
|
||||
eventDate: DateTime.parse(post.startDate!),
|
||||
);
|
||||
} else {
|
||||
NotificationService.cancel(post.id!);
|
||||
}
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
AppLocalizations.of(context)?.error_update ??
|
||||
"Error when updating")),
|
||||
);
|
||||
}
|
||||
},
|
||||
icon: Icon(
|
||||
post.interested ?? false
|
||||
? Icons.favorite
|
||||
: Icons.favorite_border,
|
||||
color:
|
||||
post.interested ?? false ? Colors.red : Colors.grey)),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ItemMenu(title: post.id!)));
|
||||
});
|
||||
},
|
||||
separatorBuilder: (context, index) {
|
||||
return Divider();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
954
covas_mobile_old/lib/pages/ListItemMenu.dart
Normal file
954
covas_mobile_old/lib/pages/ListItemMenu.dart
Normal file
@@ -0,0 +1,954 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'ItemMenu.dart';
|
||||
import '../classes/events.dart';
|
||||
import '../classes/MyDrawer.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:intl/date_symbol_data_local.dart';
|
||||
import 'dart:math';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import '../variable/globals.dart' as globals;
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import "Camera.dart";
|
||||
import 'package:camera/camera.dart';
|
||||
|
||||
import '../classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; // Créé plus loin
|
||||
import '../classes/notification_service.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
await MobileAds.instance.initialize();
|
||||
await initializeDateFormatting("fr_FR", null);
|
||||
|
||||
runApp(ChangeNotifierProvider(
|
||||
create: (_) => LocaleProvider(),
|
||||
child: const MyApp(),
|
||||
));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final localeProvider = Provider.of<LocaleProvider>(context);
|
||||
return MaterialApp(
|
||||
localizationsDelegates: [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: [const Locale('fr', 'FR'), const Locale('en')],
|
||||
locale: localeProvider.locale,
|
||||
home: Builder(builder: (context) => ListItemMenu()),
|
||||
debugShowCheckedModeBanner: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ListItemMenu extends StatefulWidget {
|
||||
const ListItemMenu({super.key});
|
||||
|
||||
@override
|
||||
State<ListItemMenu> createState() => _MyHomePageState();
|
||||
}
|
||||
|
||||
class _MyHomePageState extends State<ListItemMenu> {
|
||||
BannerAd? _bannerAd;
|
||||
final AuthService _authService = AuthService();
|
||||
late ScrollController _scrollController;
|
||||
int _fetchCount = 0;
|
||||
bool _isLoading = false;
|
||||
|
||||
Future<List<Events>> postsFuture = getPosts();
|
||||
List<Events> filteredPosts = [];
|
||||
String geographicalZone = '';
|
||||
String itemName = '';
|
||||
String itemTags = '';
|
||||
String query = '';
|
||||
List<Map<String, dynamic>> suggestionsGeo = [];
|
||||
List<Map<String, dynamic>> suggestionsItem = [];
|
||||
List<Map<String, dynamic>> suggestionsTags = [];
|
||||
TextEditingController inputGeo = TextEditingController();
|
||||
TextEditingController startDatepicker = TextEditingController();
|
||||
TextEditingController endDatepicker = TextEditingController();
|
||||
TextEditingController inputItem = TextEditingController();
|
||||
TextEditingController inputTags = TextEditingController();
|
||||
|
||||
bool showDateFields = false; // State to toggle date fields
|
||||
bool showArrow = true;
|
||||
bool showInputSearch = true;
|
||||
bool showInputGeo = true;
|
||||
bool showInputTag = true;
|
||||
// Fetching events from API
|
||||
static Future<List<Events>> getPosts() async {
|
||||
await initializeDateFormatting("fr_FR");
|
||||
PermissionStatus status = await Permission.location.status;
|
||||
final List<Events> body = [];
|
||||
var url = Uri.parse("${globals.api}/events");
|
||||
if (status.isGranted) {
|
||||
print("Location permission granted");
|
||||
|
||||
// Get the current position with high accuracy
|
||||
|
||||
const LocationSettings locationSettings = LocationSettings(
|
||||
accuracy: LocationAccuracy.medium,
|
||||
timeLimit: Duration(seconds: 5),
|
||||
);
|
||||
Position? position;
|
||||
try {
|
||||
position = await Geolocator.getCurrentPosition(
|
||||
locationSettings: locationSettings);
|
||||
} on LocationServiceDisabledException {
|
||||
// Handle location services disabled
|
||||
print('Location services are disabled.');
|
||||
position = await Geolocator.getLastKnownPosition();
|
||||
if (position == null) {
|
||||
print('No last known position available.');
|
||||
}
|
||||
} catch (e) {
|
||||
// Handle other errors
|
||||
print('Failed to get location: $e');
|
||||
position = await Geolocator.getLastKnownPosition();
|
||||
if (position == null) {
|
||||
print('No last known position available.');
|
||||
}
|
||||
}
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
|
||||
if (position != null) {
|
||||
// Calculate the boundaries
|
||||
double radiusInKm = prefs.getDouble("kilometer") ?? 50.0;
|
||||
double latDistance = radiusInKm / 111.0;
|
||||
double lonDistance =
|
||||
radiusInKm / (111.0 * cos(position.latitude * pi / 180));
|
||||
|
||||
double minLat = position.latitude - latDistance;
|
||||
double maxLat = position.latitude + latDistance;
|
||||
double minLon = position.longitude - lonDistance;
|
||||
double maxLon = position.longitude + lonDistance;
|
||||
DateTime currentDatetime = DateTime.now();
|
||||
url = Uri.parse("${globals.api}/events/search"
|
||||
"?min_lat=$minLat&max_lat=$maxLat"
|
||||
"&min_lon=$minLon&max_lon=$maxLon¤t_datetime=${currentDatetime.toString()}");
|
||||
}
|
||||
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
|
||||
if (accessToken.isNotEmpty) {
|
||||
final response = await http.get(url, headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=${accessToken}"
|
||||
});
|
||||
final List body = json.decode(utf8.decode(response.bodyBytes));
|
||||
return body.map((e) => Events.fromJson(e)).toList();
|
||||
}
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
String formatDate(String date) {
|
||||
var splitedDate = date.split("-");
|
||||
var day = splitedDate[0];
|
||||
var month = splitedDate[1];
|
||||
var year = splitedDate[2];
|
||||
|
||||
return "${year}-${month}-${day}";
|
||||
}
|
||||
|
||||
void _incrementFetchCount() {
|
||||
setState(() {
|
||||
_fetchCount++;
|
||||
});
|
||||
_fetchData();
|
||||
}
|
||||
|
||||
void _scrollListener() {
|
||||
if (_scrollController.position.pixels ==
|
||||
_scrollController.position.maxScrollExtent) {
|
||||
_incrementFetchCount();
|
||||
}
|
||||
|
||||
// Scroll to top
|
||||
}
|
||||
|
||||
Future<void> _fetchData() async {
|
||||
print("Counter : ${_fetchCount}");
|
||||
if (_isLoading) return;
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
await Future.delayed(Duration(seconds: 2));
|
||||
fetchPostsByLocation();
|
||||
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
_scrollController = ScrollController();
|
||||
_scrollController.addListener(_scrollListener);
|
||||
|
||||
// Initialize data fetch when the page loads
|
||||
_getCurrentLocation();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// Get the device's current location
|
||||
Future<void> _getCurrentLocation() async {
|
||||
PermissionStatus status = await Permission.location.status;
|
||||
|
||||
if (status.isGranted) {
|
||||
print("Location permission granted");
|
||||
|
||||
// Get the current position with high accuracy
|
||||
const LocationSettings locationSettings = LocationSettings(
|
||||
accuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5));
|
||||
Position? position;
|
||||
try {
|
||||
position = await Geolocator.getCurrentPosition(
|
||||
locationSettings: locationSettings);
|
||||
} on LocationServiceDisabledException {
|
||||
// Handle location services disabled
|
||||
print('Location services are disabled.');
|
||||
position = await Geolocator.getLastKnownPosition();
|
||||
if (position == null) {
|
||||
print('No last known position available.');
|
||||
}
|
||||
} catch (e) {
|
||||
// Handle other errors
|
||||
print('Failed to get location: $e');
|
||||
position = await Geolocator.getLastKnownPosition();
|
||||
if (position == null) {
|
||||
print('No last known position available.');
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse geocode: Get city and country from latitude and longitude using Mapbox Search API
|
||||
if (position != null) {
|
||||
_getCityAndCountry(position!.latitude, position!.longitude);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Method to get city and country from latitude and longitude using Mapbox API
|
||||
Future<void> _getCityAndCountry(double latitude, double longitude) async {
|
||||
await dotenv.load(fileName: ".env"); // Load .env file
|
||||
|
||||
final mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? '';
|
||||
final url = Uri.parse(
|
||||
'https://api.mapbox.com/geocoding/v5/mapbox.places/$longitude,$latitude.json?access_token=$mapboxAccessToken',
|
||||
);
|
||||
|
||||
try {
|
||||
// Send GET request to Mapbox API
|
||||
final response = await http.get(url);
|
||||
|
||||
// If the request is successful (HTTP status 200)
|
||||
print("status mapbox : ${response.statusCode}");
|
||||
if (response.statusCode == 200) {
|
||||
// Parse the response body
|
||||
final data = json.decode(response.body);
|
||||
|
||||
// Extract the city and country from the response
|
||||
final features = data['features'];
|
||||
|
||||
if (features.isNotEmpty) {
|
||||
String city = _getCityFromFeatures(features);
|
||||
String country = _getCountryFromFeatures(features);
|
||||
print("city : ${city} ${country}");
|
||||
if (city.isNotEmpty && country.isNotEmpty) {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
prefs.setDouble("city_lat", latitude);
|
||||
prefs.setDouble("city_long", longitude);
|
||||
fetchPostsByLocation();
|
||||
setState(() {
|
||||
inputGeo.text = "${city}, ${country}";
|
||||
});
|
||||
} else {
|
||||
fetchPostsByLocation();
|
||||
}
|
||||
} else {
|
||||
fetchPostsByLocation();
|
||||
}
|
||||
} else {
|
||||
fetchPostsByLocation();
|
||||
throw Exception('Failed to load location data');
|
||||
}
|
||||
} catch (e) {
|
||||
fetchPostsByLocation();
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to extract the city from the Mapbox features array
|
||||
String _getCityFromFeatures(List<dynamic> features) {
|
||||
for (var feature in features) {
|
||||
if (feature['place_type'] != null &&
|
||||
feature['place_type'].contains('place')) {
|
||||
return feature['text'] ?? '';
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
// Helper function to extract the country from the Mapbox features array
|
||||
String _getCountryFromFeatures(List<dynamic> features) {
|
||||
for (var feature in features) {
|
||||
if (feature['place_type'] != null &&
|
||||
feature['place_type'].contains('country')) {
|
||||
return feature['text'] ?? '';
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
Future<void> searchSuggestionsGeo(String input) async {
|
||||
await dotenv.load(fileName: ".env"); // Load .env file
|
||||
|
||||
final mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? '';
|
||||
final url =
|
||||
'https://api.mapbox.com/geocoding/v5/mapbox.places/${input}.json?access_token=${mapboxAccessToken}&proximity=ip';
|
||||
final response = await http.get(Uri.parse(url));
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
setState(() {
|
||||
suggestionsGeo = (data['features'] as List)
|
||||
.map((feature) => {
|
||||
'place_name': feature['place_name'],
|
||||
'geometry': feature[
|
||||
'geometry'], // Include geometry for latitude/longitude
|
||||
})
|
||||
.toList();
|
||||
if (suggestionsGeo.isNotEmpty) {
|
||||
showArrow = false;
|
||||
showInputSearch = false;
|
||||
showInputTag = false;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw Exception(AppLocalizations.of(context)?.failed_suggestions ??
|
||||
'Failed to load suggestions');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Uri> getUrlForEvents() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final latitude = prefs.getDouble("city_lat") ?? 0.0;
|
||||
final longitude = prefs.getDouble("city_long") ?? 0.0;
|
||||
|
||||
final radiusInKm = prefs.getDouble("kilometer") ?? 50.0;
|
||||
String endpoint = "events";
|
||||
String queryParameters = "";
|
||||
|
||||
if (latitude != 0.0 && longitude != 0.0) {
|
||||
final latDistance = radiusInKm / 111.0;
|
||||
final lonDistance = radiusInKm / (111.0 * cos(latitude * pi / 180));
|
||||
|
||||
final minLat = latitude - latDistance;
|
||||
final maxLat = latitude + latDistance;
|
||||
final minLon = longitude - lonDistance;
|
||||
final maxLon = longitude + lonDistance;
|
||||
|
||||
endpoint = "events/search";
|
||||
queryParameters =
|
||||
"min_lat=$minLat&max_lat=$maxLat&min_lon=$minLon&max_lon=$maxLon";
|
||||
}
|
||||
|
||||
final currentDate = DateTime.now();
|
||||
String dateParameter = "current_datetime=${currentDate.toIso8601String()}";
|
||||
|
||||
if (startDatepicker.text.isNotEmpty || endDatepicker.text.isNotEmpty) {
|
||||
endpoint = "events/search";
|
||||
if (startDatepicker.text.isNotEmpty) {
|
||||
final startDate = DateTime.parse(formatDate(startDatepicker.text));
|
||||
dateParameter = "start_date=${startDate.toIso8601String()}";
|
||||
}
|
||||
if (endDatepicker.text.isNotEmpty) {
|
||||
final endDate = DateTime.parse(formatDate(endDatepicker.text));
|
||||
dateParameter += "&end_date=${endDate.toIso8601String()}";
|
||||
}
|
||||
}
|
||||
|
||||
if (inputItem.text.isNotEmpty) {
|
||||
queryParameters += "&item=${inputItem.text}";
|
||||
}
|
||||
|
||||
if (inputTags.text.isNotEmpty) {
|
||||
queryParameters += "&tags=${inputTags.text}";
|
||||
}
|
||||
|
||||
if (queryParameters.isNotEmpty) {
|
||||
queryParameters = "$queryParameters&$dateParameter";
|
||||
} else {
|
||||
queryParameters = dateParameter;
|
||||
}
|
||||
int limit = 20 * (_fetchCount + 1);
|
||||
|
||||
return Uri.parse(
|
||||
"${globals.api}/$endpoint?$queryParameters&limit=${limit}");
|
||||
}
|
||||
|
||||
Future<void> searchSuggestionsByItem(String input) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
|
||||
if (accessToken.isNotEmpty) {
|
||||
var url = await getUrlForEvents();
|
||||
final response = await http.get(url, headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
||||
});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(utf8.decode(response.bodyBytes));
|
||||
setState(() {
|
||||
suggestionsItem = (data as List)
|
||||
.map((feature) => {'name': feature['name']})
|
||||
.toList();
|
||||
if (suggestionsItem.isNotEmpty) {
|
||||
showDateFields = false;
|
||||
showArrow = false;
|
||||
}
|
||||
});
|
||||
print("status code : ${response.statusCode}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> searchSuggestionsByTag(String input) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
|
||||
if (accessToken.isNotEmpty) {
|
||||
var url = Uri.parse("${globals.api}/tags?name=${input}");
|
||||
final response = await http.get(url, headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
||||
});
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(utf8.decode(response.bodyBytes));
|
||||
setState(() {
|
||||
suggestionsTags = (data as List)
|
||||
.map((feature) => {'name': feature['name']})
|
||||
.toList();
|
||||
if (suggestionsTags.isNotEmpty) {
|
||||
showInputGeo = false;
|
||||
showInputSearch = false;
|
||||
showArrow = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> toggleInterested(String eventId) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
final url = Uri.parse("${globals.api}/events/${eventId}/interest");
|
||||
if (accessToken.isNotEmpty) {
|
||||
final response = await http.post(
|
||||
url,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw (AppLocalizations.of(context)?.toogle_interest ??
|
||||
"Error toogle interest: ${response.statusCode}");
|
||||
}
|
||||
|
||||
var event = json.decode(response.body);
|
||||
return event;
|
||||
}
|
||||
return {"interested": false, "interested_count": 0};
|
||||
}
|
||||
|
||||
Future<void> fetchPostsByLocation() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
|
||||
if (accessToken.isNotEmpty) {
|
||||
var url = await getUrlForEvents();
|
||||
final response = await http.get(url, headers: {
|
||||
"Content-Type": "application/json",
|
||||
HttpHeaders.cookieHeader: "access_token=$accessToken"
|
||||
});
|
||||
|
||||
print("status code : ${response.statusCode}");
|
||||
if (response.statusCode == 200) {
|
||||
final List<dynamic> body = json.decode(utf8.decode(response.bodyBytes));
|
||||
print("results fetch : ${body}");
|
||||
print("fetch count : ${_fetchCount}");
|
||||
// Update state after getting the response
|
||||
|
||||
setState(() {
|
||||
int counter = filteredPosts.length;
|
||||
// If we have results, map them to Events
|
||||
filteredPosts = body
|
||||
.map((e) => Events.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
if (counter == filteredPosts.length) {
|
||||
_fetchCount--;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw Exception('Failed to load posts');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onTapFunctionDatePicker(
|
||||
{required BuildContext context, String position = ""}) async {
|
||||
DateTime dateEvent = DateTime.now();
|
||||
if (startDatepicker.text.isNotEmpty) {
|
||||
dateEvent = DateTime.parse(formatDate(startDatepicker.text));
|
||||
}
|
||||
|
||||
DateTime? pickedDate = await showDatePicker(
|
||||
context: context, firstDate: dateEvent, lastDate: DateTime(2104));
|
||||
if (pickedDate == null) return;
|
||||
if (position == "start") {
|
||||
startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
|
||||
} else if (position == "end") {
|
||||
endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
|
||||
}
|
||||
|
||||
fetchPostsByLocation();
|
||||
}
|
||||
|
||||
Padding _buildDateField(String position) {
|
||||
TextEditingController datePicker = startDatepicker;
|
||||
String hintText = AppLocalizations.of(context)?.start_date ?? "Start date";
|
||||
if (position == "end") {
|
||||
datePicker = endDatepicker;
|
||||
hintText = AppLocalizations.of(context)?.end_date ?? "End date";
|
||||
}
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: datePicker,
|
||||
readOnly: true,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
suffixIcon: datePicker.text.isEmpty
|
||||
? null
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: () async {
|
||||
setState(() {
|
||||
datePicker.text = '';
|
||||
});
|
||||
fetchPostsByLocation();
|
||||
},
|
||||
),
|
||||
hintText: hintText),
|
||||
onTap: () =>
|
||||
onTapFunctionDatePicker(context: context, position: position)),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSearchField({
|
||||
required TextEditingController controller,
|
||||
required String labelText,
|
||||
required Function(String) onChanged,
|
||||
required Function() onClear,
|
||||
required List<Map<String, dynamic>> suggestions,
|
||||
required Function(Map<String, dynamic>) onSuggestionTap,
|
||||
}) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: controller,
|
||||
decoration: InputDecoration(
|
||||
labelText: labelText,
|
||||
border: OutlineInputBorder(),
|
||||
suffixIcon: controller.text.isEmpty
|
||||
? null
|
||||
: IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: () => onClear(),
|
||||
),
|
||||
),
|
||||
onChanged: onChanged,
|
||||
),
|
||||
if (suggestions.isNotEmpty)
|
||||
Container(
|
||||
height: 200,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.blue),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: suggestions.length,
|
||||
itemBuilder: (context, index) {
|
||||
final suggestion = suggestions[index];
|
||||
return ListTile(
|
||||
title: Text(
|
||||
suggestion['name'] ?? suggestion['place_name'] ?? ''),
|
||||
onTap: () => onSuggestionTap(suggestion),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> popCamera() async {
|
||||
await availableCameras().then((value) => Navigator.push(context,
|
||||
MaterialPageRoute(builder: (_) => Camera(camera: value.first))));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final loc = AppLocalizations.of(context);
|
||||
final localeProvider = Provider.of<LocaleProvider>(context);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(loc?.menu_list ?? "Item list menu"),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
drawer: MyDrawer(),
|
||||
body: Column(
|
||||
children: [
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!),
|
||||
),
|
||||
if (showInputSearch)
|
||||
_buildSearchField(
|
||||
controller: inputItem,
|
||||
labelText: loc?.search_item ?? "Search by item",
|
||||
onChanged: (value) {
|
||||
_fetchCount = 0;
|
||||
if (value.isNotEmpty) {
|
||||
setState(() {
|
||||
itemName = value;
|
||||
searchSuggestionsByItem(value);
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
inputItem.clear();
|
||||
itemName = '';
|
||||
suggestionsItem.clear();
|
||||
showDateFields = true;
|
||||
showArrow = true;
|
||||
});
|
||||
fetchPostsByLocation();
|
||||
}
|
||||
},
|
||||
onClear: () {
|
||||
_fetchCount = 0;
|
||||
setState(() {
|
||||
inputItem.clear();
|
||||
itemName = '';
|
||||
suggestionsItem.clear();
|
||||
showDateFields = true;
|
||||
showArrow = true;
|
||||
});
|
||||
fetchPostsByLocation();
|
||||
},
|
||||
suggestions: suggestionsItem,
|
||||
onSuggestionTap: (suggestion) async {
|
||||
_fetchCount = 0;
|
||||
setState(() {
|
||||
itemName = suggestion['name'];
|
||||
inputItem.text = itemName;
|
||||
suggestionsItem.clear();
|
||||
showDateFields = true;
|
||||
showArrow = true;
|
||||
});
|
||||
await fetchPostsByLocation();
|
||||
},
|
||||
),
|
||||
if ((showDateFields) && (showInputTag))
|
||||
_buildSearchField(
|
||||
controller: inputTags,
|
||||
labelText: loc?.search_tag ?? "Search by tags",
|
||||
onChanged: (value) {
|
||||
_fetchCount = 0;
|
||||
if (value.isNotEmpty) {
|
||||
setState(() {
|
||||
itemTags = value;
|
||||
searchSuggestionsByTag(value);
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
inputTags.clear();
|
||||
showArrow = true;
|
||||
showInputSearch = true;
|
||||
showInputGeo = true;
|
||||
itemTags = '';
|
||||
});
|
||||
fetchPostsByLocation();
|
||||
}
|
||||
},
|
||||
onClear: () {
|
||||
_fetchCount = 0;
|
||||
setState(() {
|
||||
inputTags.clear();
|
||||
});
|
||||
fetchPostsByLocation();
|
||||
},
|
||||
suggestions: suggestionsTags,
|
||||
onSuggestionTap: (suggestion) async {
|
||||
_fetchCount = 0;
|
||||
|
||||
setState(() {
|
||||
itemTags = suggestion['name'];
|
||||
inputTags.text = itemTags;
|
||||
suggestionsTags.clear();
|
||||
showArrow = true;
|
||||
showInputSearch = true;
|
||||
showInputGeo = true;
|
||||
});
|
||||
await fetchPostsByLocation();
|
||||
},
|
||||
),
|
||||
if ((showDateFields) && (showArrow))
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: <Widget>[
|
||||
Flexible(child: _buildDateField("start")),
|
||||
Flexible(child: _buildDateField("end"))
|
||||
]),
|
||||
if ((showDateFields) && (showInputGeo))
|
||||
_buildSearchField(
|
||||
controller: inputGeo,
|
||||
labelText:
|
||||
loc?.search_geographical ?? 'Search by geographical zone',
|
||||
onChanged: (value) async {
|
||||
_fetchCount = 0;
|
||||
|
||||
if (value.isNotEmpty) {
|
||||
setState(() {
|
||||
geographicalZone = value;
|
||||
searchSuggestionsGeo(value);
|
||||
});
|
||||
} else {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.remove("city_lat");
|
||||
prefs.remove("city_long");
|
||||
setState(() {
|
||||
inputGeo.clear();
|
||||
geographicalZone = '';
|
||||
suggestionsGeo.clear();
|
||||
showArrow = true;
|
||||
showInputSearch = true;
|
||||
showInputTag = true;
|
||||
});
|
||||
fetchPostsByLocation();
|
||||
}
|
||||
},
|
||||
onClear: () async {
|
||||
_fetchCount = 0;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.remove("city_lat");
|
||||
prefs.remove("city_long");
|
||||
setState(() {
|
||||
inputGeo.clear();
|
||||
geographicalZone = '';
|
||||
suggestionsGeo.clear();
|
||||
showArrow = true;
|
||||
showInputSearch = true;
|
||||
showInputTag = true;
|
||||
});
|
||||
fetchPostsByLocation();
|
||||
},
|
||||
suggestions: suggestionsGeo,
|
||||
onSuggestionTap: (suggestion) async {
|
||||
_fetchCount = 0;
|
||||
final latitude = suggestion['geometry']['coordinates'][1];
|
||||
final longitude = suggestion['geometry']['coordinates'][0];
|
||||
setState(() {
|
||||
geographicalZone = suggestion['place_name'];
|
||||
inputGeo.text = geographicalZone;
|
||||
suggestionsGeo.clear();
|
||||
showArrow = true;
|
||||
showInputSearch = true;
|
||||
showInputTag = true;
|
||||
});
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.setDouble("city_lat", latitude);
|
||||
prefs.setDouble("city_long", longitude);
|
||||
await fetchPostsByLocation();
|
||||
},
|
||||
),
|
||||
if (showArrow)
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
showDateFields = !showDateFields; // Toggle visibility
|
||||
});
|
||||
},
|
||||
icon: Icon(
|
||||
showDateFields
|
||||
? Icons.keyboard_arrow_up
|
||||
: Icons.keyboard_arrow_down,
|
||||
color: Colors.blue,
|
||||
),
|
||||
tooltip: showDateFields
|
||||
? loc?.show_date_field ?? 'Show Date Fields'
|
||||
: loc?.hide_date_field ?? 'Hide Date Fields',
|
||||
),
|
||||
Expanded(
|
||||
child: FutureBuilder<List<Events>>(
|
||||
future: postsFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return Center(child: CircularProgressIndicator());
|
||||
} else if (snapshot.hasData) {
|
||||
final posts = snapshot.data!;
|
||||
final displayedPosts =
|
||||
filteredPosts.isEmpty ? posts : filteredPosts;
|
||||
return buildPosts(displayedPosts);
|
||||
} else {
|
||||
return Center(
|
||||
child: Text(AppLocalizations.of(context)?.no_data ??
|
||||
"No data available"),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: popCamera,
|
||||
backgroundColor: Colors.blue,
|
||||
tooltip: loc?.search ?? 'Recherche',
|
||||
child: const Icon(Icons.photo_camera, color: Colors.white),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Function to display fetched data on screen
|
||||
Widget buildPosts(List<Events> posts) {
|
||||
final displayedPosts = filteredPosts;
|
||||
// If filteredPosts is empty, show a message saying no data is available
|
||||
if (displayedPosts.isEmpty) {
|
||||
return Center(
|
||||
child: Text(
|
||||
AppLocalizations.of(context)?.no_events ??
|
||||
'No events available for this location.',
|
||||
style: TextStyle(fontSize: 18, color: Colors.grey)),
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
controller: _scrollController,
|
||||
itemCount: displayedPosts.isNotEmpty
|
||||
? displayedPosts.length +
|
||||
(_isLoading ? 1 : 0) // Add 1 only if loading
|
||||
: 0,
|
||||
itemBuilder: (context, index) {
|
||||
if (index >= displayedPosts.length) {
|
||||
return _isLoading
|
||||
? Center(child: CircularProgressIndicator())
|
||||
: SizedBox.shrink();
|
||||
}
|
||||
final post = displayedPosts[index];
|
||||
final startDate = DateTime.parse(post.startDate!);
|
||||
//final date = DateFormat.yMd().format(startDate);
|
||||
//final time = DateFormat.Hm().format(startDate);
|
||||
final locale =
|
||||
Provider.of<LocaleProvider>(context).locale?.toString() ??
|
||||
'en_US';
|
||||
final dateLongue =
|
||||
DateFormat('EEEE d MMMM y', locale).format(startDate);
|
||||
final countInterestedString =
|
||||
AppLocalizations.of(context)?.count_interested ??
|
||||
"Interested people number";
|
||||
final countInterested =
|
||||
"${countInterestedString} : ${post.interestedCount}";
|
||||
return ListTile(
|
||||
title: Text('${post.name!}'),
|
||||
subtitle: Text('${post.place!}\n${dateLongue}\n${countInterested}'),
|
||||
trailing: IconButton(
|
||||
onPressed: () async {
|
||||
try {
|
||||
final result = await toggleInterested(post.id!);
|
||||
setState(() {
|
||||
post.interested = result["interested"];
|
||||
post.interestedCount = result["interested_count"];
|
||||
});
|
||||
|
||||
if (result["interested"] == true) {
|
||||
NotificationService.scheduleEventNotification(
|
||||
eventId: post.id!,
|
||||
title: "Rappel évènement",
|
||||
body:
|
||||
"Ton évènement '${post.name}' commence dans 1 heure !",
|
||||
eventDate: DateTime.parse(post.startDate!),
|
||||
);
|
||||
} else {
|
||||
NotificationService.cancel(post.id!);
|
||||
}
|
||||
} catch (e) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
AppLocalizations.of(context)?.error_update ??
|
||||
"Error when updating")),
|
||||
);
|
||||
}
|
||||
},
|
||||
icon: Icon(
|
||||
post.interested ?? false
|
||||
? Icons.favorite
|
||||
: Icons.favorite_border,
|
||||
color:
|
||||
post.interested ?? false ? Colors.red : Colors.grey)),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => ItemMenu(title: post.id!)),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
separatorBuilder: (context, index) {
|
||||
return Divider();
|
||||
});
|
||||
}
|
||||
}
|
169
covas_mobile_old/lib/pages/LoginDemo.dart
Normal file
169
covas_mobile_old/lib/pages/LoginDemo.dart
Normal file
@@ -0,0 +1,169 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
import '../pages/ListItemMenu.dart';
|
||||
import '../pages/AddProfile.dart';
|
||||
import '../pages/ForgotPassword.dart';
|
||||
import '../classes/alert.dart';
|
||||
import '../classes/ad_helper.dart';
|
||||
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; //
|
||||
|
||||
class LoginDemo extends StatefulWidget {
|
||||
@override
|
||||
_LoginDemoState createState() => _LoginDemoState();
|
||||
}
|
||||
|
||||
class _LoginDemoState extends State<LoginDemo> with ShowAlertDialog {
|
||||
BannerAd? _bannerAd;
|
||||
TextEditingController inputPseudo = TextEditingController();
|
||||
TextEditingController inputPassword = TextEditingController();
|
||||
final AuthService _authService = AuthService();
|
||||
bool _rememberMe = false;
|
||||
|
||||
Future<void> _login(BuildContext context) async {
|
||||
final pseudo = inputPseudo.text;
|
||||
final password = inputPassword.text;
|
||||
|
||||
if (pseudo.isEmpty || password.isEmpty) {
|
||||
showAlertDialog(context, AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.empty_input ?? "Empty input");
|
||||
return;
|
||||
}
|
||||
|
||||
bool success =
|
||||
await _authService.login(pseudo, password, rememberMe: _rememberMe);
|
||||
|
||||
if (success) {
|
||||
Navigator.push(
|
||||
context, MaterialPageRoute(builder: (_) => ListItemMenu()));
|
||||
} else {
|
||||
showAlertDialog(context, AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.failed_auth ?? "Authentication failed");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
|
||||
_checkLocationPermission();
|
||||
_checkLoginStatus();
|
||||
}
|
||||
|
||||
Future<void> _checkLoginStatus() async {
|
||||
bool loggedIn = await _authService.isLoggedIn();
|
||||
if (loggedIn) {
|
||||
Navigator.push(
|
||||
context, MaterialPageRoute(builder: (_) => ListItemMenu()));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _checkLocationPermission() async {
|
||||
PermissionStatus status = await Permission.location.status;
|
||||
if (!status.isGranted) {
|
||||
await Permission.location.request();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)?.login_page ?? "Login Page"),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextField(
|
||||
controller: inputPseudo,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.pseudo ?? 'Pseudo',
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.enter_existing_pseudo ??
|
||||
'Enter a existing pseudo',
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
|
||||
child: TextField(
|
||||
controller: inputPassword,
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.password ?? "Password",
|
||||
hintText: AppLocalizations.of(context)?.enter_password ??
|
||||
"Enter the password",
|
||||
),
|
||||
),
|
||||
),
|
||||
CheckboxListTile(
|
||||
title: Text(
|
||||
AppLocalizations.of(context)?.remembr_me ?? "Remember me"),
|
||||
value: _rememberMe,
|
||||
onChanged: (newValue) {
|
||||
setState(() {
|
||||
_rememberMe = newValue ?? false;
|
||||
});
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.push(context,
|
||||
MaterialPageRoute(builder: (_) => PasswordForgot()));
|
||||
},
|
||||
child: Text(
|
||||
AppLocalizations.of(context)?.forgot_password ??
|
||||
'Forgot Password',
|
||||
style: TextStyle(color: Colors.blue, fontSize: 15)),
|
||||
),
|
||||
Container(
|
||||
height: 50,
|
||||
width: 250,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue, borderRadius: BorderRadius.circular(20)),
|
||||
child: TextButton(
|
||||
onPressed: () => _login(context),
|
||||
child: Text(AppLocalizations.of(context)?.sign_in ?? 'Sign in',
|
||||
style: TextStyle(color: Colors.white, fontSize: 25)),
|
||||
),
|
||||
),
|
||||
SizedBox(height: 130),
|
||||
InkWell(
|
||||
child: Text(AppLocalizations.of(context)?.new_user ??
|
||||
'New User? Create Account'),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context, MaterialPageRoute(builder: (_) => AddProfile()));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
452
covas_mobile_old/lib/pages/MapboxPages.dart
Normal file
452
covas_mobile_old/lib/pages/MapboxPages.dart
Normal file
@@ -0,0 +1,452 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart'; // For environment variables
|
||||
import 'package:flutter/services.dart'; // For loading assets
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:mapbox_gl/mapbox_gl.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:geolocator/geolocator.dart'; // For getting the user's location
|
||||
|
||||
import '../classes/alert.dart'; // Assuming this contains your error dialog code.
|
||||
import '../variable/globals.dart' as globals;
|
||||
import '../classes/MyDrawer.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; //
|
||||
|
||||
void main() async {
|
||||
await dotenv.load(fileName: ".env"); // Load .env file
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Directions Example',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: const MapboxPages(title: 'Event Location', place: "Flutter"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MapboxPages extends StatefulWidget {
|
||||
const MapboxPages({Key? key, required this.title, required this.place})
|
||||
: super(key: key);
|
||||
|
||||
final String title;
|
||||
final String place;
|
||||
|
||||
@override
|
||||
State<MapboxPages> createState() => _MapboxPagesState();
|
||||
}
|
||||
|
||||
class _MapboxPagesState extends State<MapboxPages> with ShowAlertDialog {
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
late MapboxMapController mapController;
|
||||
late String mapboxAccessToken;
|
||||
List<LatLng> routeCoordinates = [];
|
||||
String selectedMode = 'driving';
|
||||
double longitude = 0.0;
|
||||
double latitude = 0.0;
|
||||
bool isLoading = true;
|
||||
late LatLng userPosition;
|
||||
bool isUserPositionInitialized = false;
|
||||
Line? currentRouteLine;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
|
||||
_getUserLocation();
|
||||
}
|
||||
|
||||
void _initToken() {
|
||||
mapboxAccessToken = dotenv.env['MAPBOX_ACCESS_TOKEN'] ?? '';
|
||||
if (mapboxAccessToken.isEmpty) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.map_token ??
|
||||
"Map Access Token is not available.");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _getEventInfo() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
|
||||
if (accessToken.isNotEmpty) {
|
||||
var urlGet = Uri.parse("${globals.api}/events/${widget.title}");
|
||||
|
||||
var responseGet = await http.get(urlGet,
|
||||
headers: {HttpHeaders.cookieHeader: 'access_token=${accessToken}'});
|
||||
if (responseGet.statusCode == 200) {
|
||||
var events = jsonDecode(utf8.decode(responseGet.bodyBytes));
|
||||
latitude = events["latitude"];
|
||||
longitude = events["longitude"];
|
||||
|
||||
setState(() {
|
||||
isLoading = false;
|
||||
});
|
||||
} else {
|
||||
_handleErrorResponse(responseGet.statusCode);
|
||||
}
|
||||
} else {
|
||||
showAlertDialog(context, AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.invalid_cache ?? "Invalid cache.");
|
||||
}
|
||||
}
|
||||
|
||||
void _handleErrorResponse(int statusCode) {
|
||||
final messages = {
|
||||
400: AppLocalizations.of(context)?.request_error ??
|
||||
"Poorly constructed query",
|
||||
406: AppLocalizations.of(context)?.incorrect_password ??
|
||||
"Incorrect password",
|
||||
404: AppLocalizations.of(context)?.unknown_user ?? "Unknown user",
|
||||
403: AppLocalizations.of(context)?.disabled_user ?? "Disabled user",
|
||||
410: AppLocalizations.of(context)?.invalid_token ?? "Invalid token",
|
||||
500: AppLocalizations.of(context)?.internal_error_server ??
|
||||
"Internal error server"
|
||||
};
|
||||
|
||||
final errorMessage = messages[statusCode] ??
|
||||
AppLocalizations.of(context)?.unknown_error_auth ??
|
||||
"Unknown error auth";
|
||||
showAlertDialog(
|
||||
context, AppLocalizations.of(context)?.error ?? "Error", errorMessage);
|
||||
}
|
||||
|
||||
Future<void> _getUserLocation() async {
|
||||
try {
|
||||
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.geo_disabled ??
|
||||
"Location services are disabled.");
|
||||
return;
|
||||
}
|
||||
|
||||
LocationPermission permission = await Geolocator.checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await Geolocator.requestPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.permission_denied ??
|
||||
"Location permissions are denied.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (permission == LocationPermission.deniedForever) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.enable_permission ??
|
||||
"Location permissions are permanently denied. Enable them in settings.");
|
||||
return;
|
||||
}
|
||||
const LocationSettings locationSettings = LocationSettings(
|
||||
accuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5));
|
||||
Position? position;
|
||||
try {
|
||||
position = await Geolocator.getCurrentPosition(
|
||||
locationSettings: locationSettings);
|
||||
} on LocationServiceDisabledException {
|
||||
// Handle location services disabled
|
||||
position = await Geolocator.getLastKnownPosition();
|
||||
if (position == null) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.no_last_position ??
|
||||
"No last known position available..");
|
||||
}
|
||||
} catch (e) {
|
||||
// Handle other errors
|
||||
position = await Geolocator.getLastKnownPosition();
|
||||
if (position == null) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.no_last_position ??
|
||||
"No last known position available");
|
||||
}
|
||||
}
|
||||
if (position != null) {
|
||||
setState(() {
|
||||
userPosition = LatLng(position!.latitude, position!.longitude);
|
||||
isUserPositionInitialized = true;
|
||||
});
|
||||
}
|
||||
_initToken();
|
||||
_getEventInfo();
|
||||
} catch (e) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.failed_location ??
|
||||
"Failed to get user location");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _fetchRoute(
|
||||
LatLng origin, LatLng destination, String mode) async {
|
||||
final url = Uri.parse(
|
||||
'https://api.mapbox.com/directions/v5/mapbox/$mode/${origin.longitude},${origin.latitude};${destination.longitude},${destination.latitude}?geometries=geojson&access_token=$mapboxAccessToken',
|
||||
);
|
||||
|
||||
final response = await http.get(url);
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
final geometry = data['routes'][0]['geometry']['coordinates'];
|
||||
setState(() {
|
||||
routeCoordinates = geometry.map<LatLng>((coord) {
|
||||
return LatLng(coord[1], coord[0]);
|
||||
}).toList();
|
||||
});
|
||||
} else {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.failed_fetch ??
|
||||
"Failed to fetch the route");
|
||||
}
|
||||
}
|
||||
|
||||
// Called when the map is created
|
||||
void _onStyleLoaded() async {
|
||||
// Log the map controller and coordinates
|
||||
|
||||
// Check if the mapController is really initialized
|
||||
if (mapController != null) {
|
||||
try {
|
||||
// Ensure the coordinates are valid
|
||||
if (latitude != 0.0 && longitude != 0.0) {
|
||||
// Load marker image as Uint8List
|
||||
final userMarkerImage = await _loadMarkerImage('images/marker.png');
|
||||
|
||||
// Register the image with Mapbox
|
||||
await mapController.addImage('event-marker', userMarkerImage);
|
||||
|
||||
final symbolOptions = SymbolOptions(
|
||||
geometry: LatLng(latitude, longitude),
|
||||
iconImage: "event-marker", // Use the registered custom marker
|
||||
iconSize: 0.4, // Optional: Adjust size
|
||||
);
|
||||
|
||||
// Debugging symbol options
|
||||
|
||||
// Add symbol to map
|
||||
mapController!.addSymbol(symbolOptions);
|
||||
} else {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.invalid_coordinates_symbol ??
|
||||
"Error: Invalid coordinates, cannot add symbol.");
|
||||
}
|
||||
} catch (e) {
|
||||
// Handle any exception that occurs when adding the symbol
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.error_symbol ??
|
||||
"Error when adding symbol.");
|
||||
}
|
||||
} else {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.error_symbol ??
|
||||
"Error when adding symbol.");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _drawRouteAndMarkers() async {
|
||||
// Remove previous route line if it exists
|
||||
if (currentRouteLine != null) {
|
||||
await mapController.removeLine(currentRouteLine!);
|
||||
currentRouteLine = null;
|
||||
}
|
||||
if (!isUserPositionInitialized) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.position_not_init ??
|
||||
"User position is not yet initialized. Try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mapController != null &&
|
||||
userPosition != null &&
|
||||
latitude != 0.0 &&
|
||||
longitude != 0.0) {
|
||||
final destination = LatLng(latitude, longitude);
|
||||
|
||||
// Register the custom images
|
||||
// Add event marker
|
||||
|
||||
final eventMarkerImage = await _loadMarkerImage('images/marker-red.png');
|
||||
|
||||
// Register the image with Mapbox
|
||||
await mapController.addImage('user-marker', eventMarkerImage);
|
||||
await mapController.addSymbol(SymbolOptions(
|
||||
geometry: userPosition,
|
||||
iconImage: 'user-marker', // Custom icon for event
|
||||
iconSize: 0.2,
|
||||
));
|
||||
|
||||
// Fetch and draw route
|
||||
await _fetchRoute(userPosition, destination, selectedMode);
|
||||
|
||||
if (routeCoordinates.isNotEmpty) {
|
||||
currentRouteLine = await mapController.addLine(
|
||||
LineOptions(
|
||||
geometry: routeCoordinates,
|
||||
lineColor: '#3b9ddd',
|
||||
lineWidth: 5.0,
|
||||
lineOpacity: 0.8,
|
||||
),
|
||||
);
|
||||
|
||||
_zoomToFitRoute(routeCoordinates);
|
||||
}
|
||||
} else {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.invalid_coordinates ??
|
||||
"Invalid coordinates or user position.");
|
||||
}
|
||||
}
|
||||
|
||||
void _zoomToFitRoute(List<LatLng> coordinates) {
|
||||
// Calculate the bounding box
|
||||
double minLat = coordinates.first.latitude;
|
||||
double maxLat = coordinates.first.latitude;
|
||||
double minLng = coordinates.first.longitude;
|
||||
double maxLng = coordinates.first.longitude;
|
||||
|
||||
for (LatLng coord in coordinates) {
|
||||
if (coord.latitude < minLat) minLat = coord.latitude;
|
||||
if (coord.latitude > maxLat) maxLat = coord.latitude;
|
||||
if (coord.longitude < minLng) minLng = coord.longitude;
|
||||
if (coord.longitude > maxLng) maxLng = coord.longitude;
|
||||
}
|
||||
|
||||
// Define the bounds
|
||||
LatLng southwest = LatLng(minLat, minLng);
|
||||
LatLng northeast = LatLng(maxLat, maxLng);
|
||||
|
||||
mapController.moveCamera(
|
||||
CameraUpdate.newLatLngBounds(
|
||||
LatLngBounds(southwest: southwest, northeast: northeast),
|
||||
left: 50, // Padding on the left
|
||||
top: 50, // Padding on the top
|
||||
right: 50, // Padding on the right
|
||||
bottom: 50, // Padding on the bottom
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Load image from assets
|
||||
Future<Uint8List> _loadMarkerImage(String assetPath) async {
|
||||
final ByteData data = await rootBundle.load(assetPath);
|
||||
return data.buffer.asUint8List();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.place),
|
||||
actions: [
|
||||
DropdownButton<String>(
|
||||
value: selectedMode,
|
||||
items: [
|
||||
DropdownMenuItem(
|
||||
value: 'walking',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.directions_walk, color: Colors.blue),
|
||||
SizedBox(width: 8),
|
||||
Text(AppLocalizations.of(context)?.walking ?? 'Walking'),
|
||||
],
|
||||
),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
value: 'cycling',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.directions_bike, color: Colors.green),
|
||||
SizedBox(width: 8),
|
||||
Text(AppLocalizations.of(context)?.cycling ?? 'Cycling'),
|
||||
],
|
||||
),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
value: 'driving',
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(Icons.directions_car, color: Colors.red),
|
||||
SizedBox(width: 8),
|
||||
Text(AppLocalizations.of(context)?.driving ?? 'Driving'),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
onChanged: (mode) {
|
||||
setState(() {
|
||||
selectedMode = mode!;
|
||||
});
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
drawer: MyDrawer(),
|
||||
body: Stack(
|
||||
children: [
|
||||
isLoading
|
||||
? Center(child: CircularProgressIndicator())
|
||||
: MapboxMap(
|
||||
accessToken: mapboxAccessToken,
|
||||
onMapCreated: (controller) {
|
||||
mapController = controller;
|
||||
},
|
||||
onStyleLoadedCallback: _onStyleLoaded,
|
||||
initialCameraPosition: CameraPosition(
|
||||
target: LatLng(latitude, longitude),
|
||||
zoom: 14.0,
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
bottom: 20,
|
||||
right: 20,
|
||||
child: FloatingActionButton(
|
||||
onPressed: _drawRouteAndMarkers,
|
||||
child: Icon(Icons.directions),
|
||||
tooltip: AppLocalizations.of(context)?.get_direction ??
|
||||
'Get Directions and Markers',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
815
covas_mobile_old/lib/pages/UpdateEventImage.dart
Normal file
815
covas_mobile_old/lib/pages/UpdateEventImage.dart
Normal file
@@ -0,0 +1,815 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:textfield_tags/textfield_tags.dart';
|
||||
import '../classes/MyDrawer.dart';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'ItemMenu.dart';
|
||||
import '../classes/alert.dart';
|
||||
import '../classes/eventAdded.dart';
|
||||
|
||||
import '../variable/globals.dart' as globals;
|
||||
|
||||
import '../classes/ad_helper.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
import '../classes/auth_service.dart';
|
||||
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../locale_provider.dart'; //
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await MobileAds.instance.initialize();
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
Map<String, dynamic> events = {};
|
||||
String imagePath = "";
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
home: UpdateeventImage(events: events, imagePath: imagePath),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UpdateeventImage extends StatefulWidget {
|
||||
const UpdateeventImage(
|
||||
{Key? key, required this.events, required this.imagePath})
|
||||
: super(key: key);
|
||||
final Map<String, dynamic> events;
|
||||
final String imagePath;
|
||||
|
||||
@override
|
||||
_UpdateeventImageState createState() => _UpdateeventImageState();
|
||||
}
|
||||
|
||||
class _UpdateeventImageState extends State<UpdateeventImage>
|
||||
with ShowAlertDialog, ShowEventDialog {
|
||||
BannerAd? _bannerAd;
|
||||
final AuthService _authService = AuthService();
|
||||
|
||||
TextEditingController inputName = TextEditingController();
|
||||
|
||||
TextEditingController inputDate = TextEditingController();
|
||||
TextEditingController inputDesc = TextEditingController();
|
||||
|
||||
TextEditingController inputGeo = TextEditingController();
|
||||
|
||||
TextEditingController startDatepicker = TextEditingController();
|
||||
TextEditingController startTimepicker = TextEditingController();
|
||||
TextEditingController endDatepicker = TextEditingController();
|
||||
TextEditingController endTimepicker = TextEditingController();
|
||||
final _stringTagController = StringTagController();
|
||||
|
||||
List<Map<String, dynamic>> suggestions = [];
|
||||
String geographicalZone = "";
|
||||
|
||||
List<String> initialTags = [];
|
||||
|
||||
final _stringOrgaController = StringTagController();
|
||||
List<String> initialOrga = [];
|
||||
|
||||
onTapFunctionDatePicker(
|
||||
{required BuildContext context, required String position}) async {
|
||||
String date = "start_date";
|
||||
if (position == "end") {
|
||||
date = "end_date";
|
||||
}
|
||||
DateTime dateEvent;
|
||||
if (widget.events[date].toString().isEmpty) {
|
||||
dateEvent = DateTime.now();
|
||||
} else {
|
||||
dateEvent = DateTime.parse(widget.events[date]);
|
||||
}
|
||||
DateTime? pickedDate = await showDatePicker(
|
||||
context: context,
|
||||
firstDate: dateEvent,
|
||||
initialDate: dateEvent,
|
||||
lastDate: DateTime(2104));
|
||||
if (pickedDate == null) return;
|
||||
if (position == "start") {
|
||||
startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
|
||||
}
|
||||
if (position == "end") {
|
||||
endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
|
||||
}
|
||||
}
|
||||
|
||||
onTapFunctionTimePicker(
|
||||
{required BuildContext context, required String position}) async {
|
||||
String date = "start_date";
|
||||
if (position == "end") {
|
||||
date = "end_date";
|
||||
}
|
||||
TimeOfDay timeEvent;
|
||||
if (widget.events[date].toString().isEmpty) {
|
||||
timeEvent = TimeOfDay.now();
|
||||
} else {
|
||||
timeEvent = TimeOfDay.fromDateTime(DateTime.parse(widget.events[date]));
|
||||
}
|
||||
TimeOfDay? pickedDate =
|
||||
await showTimePicker(context: context, initialTime: timeEvent);
|
||||
if (pickedDate == null) return;
|
||||
if (position == "start") {
|
||||
startTimepicker.text = pickedDate.format(context);
|
||||
}
|
||||
if (position == "end") {
|
||||
endTimepicker.text = pickedDate.format(context);
|
||||
}
|
||||
}
|
||||
|
||||
convertNulltoEmptyString(var check) {
|
||||
if (check == null) {
|
||||
return "";
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
convertNulltoArray(List<String> check) {
|
||||
if (check == null) {
|
||||
return [];
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
String formatDate(String date) {
|
||||
var splitedDate = date.split("-");
|
||||
var day = splitedDate[0];
|
||||
var month = splitedDate[1];
|
||||
var year = splitedDate[2];
|
||||
|
||||
return "${year}-${month}-${day}";
|
||||
}
|
||||
|
||||
Future<void> _updateEvent(BuildContext context) async {
|
||||
// Gather inputs
|
||||
var name = inputName.text;
|
||||
var place = inputGeo.text;
|
||||
var description = inputDesc.text;
|
||||
List<String> tags = List<String>.from(_stringTagController.getTags as List);
|
||||
List<String> organizers =
|
||||
List<String>.from(_stringOrgaController.getTags as List);
|
||||
|
||||
var startDateFormat = formatDate(startDatepicker.text);
|
||||
DateTime startDateCompare = DateTime.parse(startDateFormat);
|
||||
DateTime dateNow = DateTime.now();
|
||||
var endDateFormat = formatDate(endDatepicker.text);
|
||||
var startDate =
|
||||
"${startDateFormat}T${startTimepicker.text.replaceAll('-', ':')}";
|
||||
var endDate = "${endDateFormat}T${endTimepicker.text.replaceAll('-', ':')}";
|
||||
|
||||
if (!startDateCompare.isAfter(dateNow)) {
|
||||
showAlertDialog(context, AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.no_future_event ?? "No future event");
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var accessToken = prefs.getString("access_token") ?? "";
|
||||
|
||||
if (accessToken.isEmpty) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.missing_token ??
|
||||
"Missing access token");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await dotenv.load();
|
||||
final ApiTokenGoogle = dotenv.env['PLACE_API_KEY'] ?? '';
|
||||
// Searchbox API for geocoding the place (No session token)
|
||||
final searchboxUrl = Uri.parse(
|
||||
'https://maps.googleapis.com/maps/api/place/textsearch/json?query=${place}&key=${ApiTokenGoogle}');
|
||||
|
||||
// Perform the request
|
||||
final searchboxResponse = await http.get(searchboxUrl);
|
||||
|
||||
if (searchboxResponse.statusCode != 200) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.geocoding_error ??
|
||||
"Error when geocoding");
|
||||
return;
|
||||
}
|
||||
|
||||
final searchboxData = json.decode(searchboxResponse.body);
|
||||
if (searchboxData['results'].isEmpty) {
|
||||
showAlertDialog(context, AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.no_found_place ?? "No found place");
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract place details from the searchbox response
|
||||
final firstFeature = searchboxData['results'][0];
|
||||
place = firstFeature["formatted_address"];
|
||||
final coordinates = firstFeature['geometry']['location'];
|
||||
final longitude = coordinates["lng"];
|
||||
final latitude = coordinates["lat"];
|
||||
|
||||
// Check if a similar event exists
|
||||
final eventsUrl = Uri.parse(
|
||||
"${globals.api}/events/search?item=$name&date_event=$startDate");
|
||||
final eventsResponse = await http.get(eventsUrl, headers: {
|
||||
HttpHeaders.cookieHeader: 'access_token=$accessToken',
|
||||
});
|
||||
|
||||
if (eventsResponse.statusCode == 200) {
|
||||
final events = json.decode(utf8.decode(eventsResponse.bodyBytes));
|
||||
if (events.isNotEmpty) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ItemMenu(title: events[0]["id"]),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Upload image to imgbb
|
||||
final imgbbUrl = Uri.parse(
|
||||
'https://api.imgbb.com/1/upload?expiration=15552000&key=${dotenv.env["IMGBB_API_KEY"]}');
|
||||
File image = File(widget.imagePath);
|
||||
Uint8List imageBytes = await image.readAsBytes();
|
||||
String base64Image = base64.encode(imageBytes);
|
||||
|
||||
final imgbbRequest = http.MultipartRequest('POST', imgbbUrl)
|
||||
..fields['image'] = base64Image;
|
||||
final imgbbResponse =
|
||||
await http.Response.fromStream(await imgbbRequest.send());
|
||||
|
||||
if (imgbbResponse.statusCode != 200) {
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.upload_error ??
|
||||
"Error when image uploading");
|
||||
return;
|
||||
}
|
||||
|
||||
final imgbbData = json.decode(imgbbResponse.body);
|
||||
final imgUrl = imgbbData['data']['url'];
|
||||
|
||||
// Create or update the event
|
||||
final eventUrl = Uri.parse("${globals.api}/events");
|
||||
final eventResponse = await http.put(
|
||||
eventUrl,
|
||||
headers: {
|
||||
HttpHeaders.cookieHeader: 'access_token=$accessToken',
|
||||
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
|
||||
HttpHeaders.contentTypeHeader: 'application/json',
|
||||
},
|
||||
body: jsonEncode({
|
||||
'name': name,
|
||||
'place': place,
|
||||
'start_date': startDate,
|
||||
'end_date': endDate,
|
||||
'organizers': organizers,
|
||||
'latitude': latitude,
|
||||
'longitude': longitude,
|
||||
'description': description,
|
||||
'imgUrl': imgUrl,
|
||||
'tags': tags,
|
||||
}),
|
||||
);
|
||||
|
||||
if (eventResponse.statusCode == 200 || eventResponse.statusCode == 201) {
|
||||
String event_message =
|
||||
AppLocalizations.of(context)?.event_added ?? "Event added";
|
||||
showEventDialog(context, "$event_message : $name");
|
||||
} else {
|
||||
handleHttpError(eventResponse.statusCode, context);
|
||||
}
|
||||
} catch (e) {
|
||||
showAlertDialog(context, AppLocalizations.of(context)?.error ?? "Error",
|
||||
AppLocalizations.of(context)?.app_error ?? "Error application");
|
||||
}
|
||||
}
|
||||
|
||||
// Utility function to handle HTTP errors
|
||||
void handleHttpError(int statusCode, BuildContext context) {
|
||||
final messages = {
|
||||
400: AppLocalizations.of(context)?.request_error ??
|
||||
"Poorly constructed query",
|
||||
406: AppLocalizations.of(context)?.incorrect_password ??
|
||||
"Incorrect password",
|
||||
404: AppLocalizations.of(context)?.unknown_user ?? "Unknown user",
|
||||
403: AppLocalizations.of(context)?.disabled_user ?? "Disabled user",
|
||||
410: AppLocalizations.of(context)?.invalid_token ?? "Invalid token",
|
||||
500: AppLocalizations.of(context)?.internal_error_server ??
|
||||
"Internal error server"
|
||||
};
|
||||
showAlertDialog(
|
||||
context,
|
||||
AppLocalizations.of(context)?.error ?? "Error",
|
||||
messages[statusCode] ??
|
||||
AppLocalizations.of(context)?.unknown_error ??
|
||||
"Unknown error");
|
||||
}
|
||||
|
||||
void start() async {
|
||||
print("events : ${widget.events}");
|
||||
inputName.text = convertNulltoEmptyString(widget.events["name"]);
|
||||
inputGeo.text = convertNulltoEmptyString(widget.events["place"]);
|
||||
inputDesc.text = convertNulltoEmptyString(widget.events["description"]);
|
||||
if (widget.events["start_date"].toString().isNotEmpty) {
|
||||
DateTime pickedStartDate =
|
||||
DateTime.parse(convertNulltoEmptyString(widget.events["start_date"]));
|
||||
startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedStartDate);
|
||||
startTimepicker.text = DateFormat("HH-mm").format(pickedStartDate);
|
||||
}
|
||||
if (widget.events["end_date"].toString().isNotEmpty) {
|
||||
DateTime pickedEndDate =
|
||||
DateTime.parse(convertNulltoEmptyString(widget.events["end_date"]));
|
||||
endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedEndDate);
|
||||
endTimepicker.text = DateFormat("HH-mm").format(pickedEndDate);
|
||||
}
|
||||
initialTags = List<String>.from(widget.events['tags'] as List);
|
||||
initialOrga = List<String>.from(widget.events['organizers'] as List);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authService.checkTokenStatus(context);
|
||||
|
||||
AdHelper.createBannerAd(() => setState(() {})).then((ad) {
|
||||
setState(() {
|
||||
_bannerAd = ad;
|
||||
});
|
||||
});
|
||||
start();
|
||||
}
|
||||
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String? _validateField(String? value) {
|
||||
return value!.isEmpty
|
||||
? AppLocalizations.of(context)?.required_input ?? 'Required input'
|
||||
: null;
|
||||
}
|
||||
|
||||
Future<void> searchSuggestions(String input) async {
|
||||
await dotenv.load(fileName: ".env"); // Load .env file
|
||||
|
||||
final ApiTokenGoogle = dotenv.env['PLACE_API_KEY'] ?? '';
|
||||
|
||||
// Define the Searchbox API URL
|
||||
final searchboxUrl = Uri.parse(
|
||||
'https://maps.googleapis.com/maps/api/place/textsearch/json?query=${input}&key=${ApiTokenGoogle}');
|
||||
|
||||
// Perform the request
|
||||
final response = await http.get(searchboxUrl);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = json.decode(response.body);
|
||||
|
||||
setState(() {
|
||||
// Map the results to extract name and full_address
|
||||
suggestions = (data['results'] as List)
|
||||
.map((feature) => {
|
||||
'name': feature['name'],
|
||||
'formatted_address': feature[
|
||||
'formatted_address'] // Adjusted to match the data structure
|
||||
})
|
||||
.toList();
|
||||
});
|
||||
} else {
|
||||
throw Exception('Failed to load suggestions');
|
||||
}
|
||||
}
|
||||
|
||||
Padding _buildGeographicalZoneSearchField() {
|
||||
return Padding(
|
||||
padding:
|
||||
const EdgeInsets.only(left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
child: Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: inputGeo,
|
||||
decoration: InputDecoration(
|
||||
labelText: AppLocalizations.of(context)?.location ?? 'Location',
|
||||
border: OutlineInputBorder(),
|
||||
suffixIcon: IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
inputGeo.clear(); // Clear the text field
|
||||
geographicalZone = ''; // Reset the geographical zone state
|
||||
suggestions.clear(); // Optionally clear suggestions
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
geographicalZone = value;
|
||||
searchSuggestions(value);
|
||||
});
|
||||
},
|
||||
),
|
||||
if (suggestions.isNotEmpty)
|
||||
Container(
|
||||
height: 200,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: Colors.blue),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: suggestions.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Text(suggestions[index]['name']),
|
||||
subtitle: Text(suggestions[index]['formatted_address']),
|
||||
onTap: () async {
|
||||
print("suggestion tapped : ${suggestions[index]}");
|
||||
|
||||
setState(() {
|
||||
geographicalZone =
|
||||
suggestions[index]['formatted_address'];
|
||||
inputGeo.text = geographicalZone;
|
||||
suggestions.clear();
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Text(AppLocalizations.of(context)?.add_event ??
|
||||
"Add or Update a event"),
|
||||
backgroundColor: Colors.blue,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
drawer: MyDrawer(),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_bannerAd == null
|
||||
? SizedBox.shrink()
|
||||
: SizedBox(
|
||||
height: _bannerAd!.size.height.toDouble(),
|
||||
width: _bannerAd!.size.width.toDouble(),
|
||||
child: AdWidget(ad: _bannerAd!)),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 60.0),
|
||||
child: Center(
|
||||
child: Container(
|
||||
width: 200,
|
||||
height: 150,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(100.0)),
|
||||
child: Image.file(File(widget.imagePath))),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
//padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
|
||||
padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: inputName,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.name ?? "Name",
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.edit_event_name ??
|
||||
"Edit event name"),
|
||||
),
|
||||
),
|
||||
_buildGeographicalZoneSearchField(),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: startDatepicker,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.start_date ??
|
||||
"Start date",
|
||||
hintText: AppLocalizations.of(context)?.select_date ??
|
||||
"Click to select a date"),
|
||||
onTap: () => onTapFunctionDatePicker(
|
||||
context: context, position: "start")),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: startTimepicker,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.start_time ??
|
||||
"Start time",
|
||||
hintText: AppLocalizations.of(context)?.select_time ??
|
||||
"Click to select a time"),
|
||||
onTap: () => onTapFunctionTimePicker(
|
||||
context: context, position: "start")),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: endDatepicker,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.end_date ??
|
||||
"End date",
|
||||
hintText: AppLocalizations.of(context)?.select_date ??
|
||||
"Click to select a date"),
|
||||
onTap: () => onTapFunctionDatePicker(
|
||||
context: context, position: "end")),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextFormField(
|
||||
controller: endTimepicker,
|
||||
readOnly: true,
|
||||
validator: (value) => _validateField(value),
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.end_time ??
|
||||
"End time",
|
||||
hintText: AppLocalizations.of(context)?.select_time ??
|
||||
"Click to select a time"),
|
||||
onTap: () => onTapFunctionTimePicker(
|
||||
context: context, position: "end")),
|
||||
),
|
||||
TextFieldTags<String>(
|
||||
textfieldTagsController: _stringTagController,
|
||||
initialTags: initialTags,
|
||||
textSeparators: const [' ', ','],
|
||||
validator: (String tag) {
|
||||
if (_stringTagController.getTags!.contains(tag)) {
|
||||
return AppLocalizations.of(context)?.already_tag ??
|
||||
'You have already entered this tag';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
inputFieldBuilder: (context, inputFieldValues) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
child: TextField(
|
||||
controller: inputFieldValues.textEditingController,
|
||||
focusNode: inputFieldValues.focusNode,
|
||||
onChanged: inputFieldValues.onTagChanged,
|
||||
onSubmitted: inputFieldValues.onTagSubmitted,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.tag ?? 'Tags',
|
||||
hintText: inputFieldValues.tags.isNotEmpty
|
||||
? ''
|
||||
: AppLocalizations.of(context)?.enter_tag ??
|
||||
"Enter tag...",
|
||||
errorText: inputFieldValues.error,
|
||||
prefixIcon: inputFieldValues.tags.isNotEmpty
|
||||
? SingleChildScrollView(
|
||||
controller:
|
||||
inputFieldValues.tagScrollController,
|
||||
scrollDirection: Axis.vertical,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 8,
|
||||
bottom: 8,
|
||||
left: 8,
|
||||
),
|
||||
child: Wrap(
|
||||
runSpacing: 4.0,
|
||||
spacing: 4.0,
|
||||
children: inputFieldValues.tags
|
||||
.map((String tag) {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
borderRadius: BorderRadius.all(
|
||||
Radius.circular(20.0),
|
||||
),
|
||||
color: Colors.blue,
|
||||
),
|
||||
margin:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 5.0),
|
||||
padding:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 10.0,
|
||||
vertical: 5.0),
|
||||
child: Row(
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
InkWell(
|
||||
child: Text(
|
||||
'$tag',
|
||||
style: const TextStyle(
|
||||
color: Colors.white),
|
||||
),
|
||||
onTap: () {
|
||||
//print("$tag selected");
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 4.0),
|
||||
InkWell(
|
||||
child: const Icon(
|
||||
Icons.cancel,
|
||||
size: 14.0,
|
||||
color: Color.fromARGB(
|
||||
255, 233, 233, 233),
|
||||
),
|
||||
onTap: () {
|
||||
inputFieldValues
|
||||
.onTagRemoved(tag);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList()),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
TextFieldTags<String>(
|
||||
textfieldTagsController: _stringOrgaController,
|
||||
initialTags: initialOrga,
|
||||
textSeparators: const [','],
|
||||
validator: (String tag) {
|
||||
if (_stringOrgaController.getTags!.contains(tag)) {
|
||||
return AppLocalizations.of(context)
|
||||
?.already_organiser ??
|
||||
'You have already entered this organizer';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
inputFieldBuilder: (context, inputFieldValues) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
child: TextField(
|
||||
controller: inputFieldValues.textEditingController,
|
||||
focusNode: inputFieldValues.focusNode,
|
||||
onChanged: inputFieldValues.onTagChanged,
|
||||
onSubmitted: inputFieldValues.onTagSubmitted,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText:
|
||||
AppLocalizations.of(context)?.organizer ??
|
||||
'Organizers',
|
||||
hintText: inputFieldValues.tags.isNotEmpty
|
||||
? ''
|
||||
: AppLocalizations.of(context)
|
||||
?.enter_organizer ??
|
||||
"Enter un organisateur...",
|
||||
errorText: inputFieldValues.error,
|
||||
prefixIcon: inputFieldValues.tags.isNotEmpty
|
||||
? SingleChildScrollView(
|
||||
controller:
|
||||
inputFieldValues.tagScrollController,
|
||||
scrollDirection: Axis.vertical,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
top: 8,
|
||||
bottom: 8,
|
||||
left: 8,
|
||||
),
|
||||
child: Wrap(
|
||||
runSpacing: 4.0,
|
||||
spacing: 4.0,
|
||||
children: inputFieldValues.tags
|
||||
.map((String tag) {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
borderRadius: BorderRadius.all(
|
||||
Radius.circular(20.0),
|
||||
),
|
||||
color: Colors.blue,
|
||||
),
|
||||
margin:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 5.0),
|
||||
padding:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 10.0,
|
||||
vertical: 5.0),
|
||||
child: Row(
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
InkWell(
|
||||
child: Text(
|
||||
'$tag',
|
||||
style: const TextStyle(
|
||||
color: Colors.white),
|
||||
),
|
||||
onTap: () {
|
||||
//print("$tag selected");
|
||||
},
|
||||
),
|
||||
const SizedBox(width: 4.0),
|
||||
InkWell(
|
||||
child: const Icon(
|
||||
Icons.cancel,
|
||||
size: 14.0,
|
||||
color: Color.fromARGB(
|
||||
255, 233, 233, 233),
|
||||
),
|
||||
onTap: () {
|
||||
inputFieldValues
|
||||
.onTagRemoved(tag);
|
||||
},
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList()),
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 15.0, right: 15.0, top: 15, bottom: 0),
|
||||
//padding: EdgeInsets.symmetric(horizontal: 15),
|
||||
child: TextField(
|
||||
controller: inputDesc,
|
||||
keyboardType: TextInputType.multiline,
|
||||
maxLines: 10,
|
||||
decoration: InputDecoration(
|
||||
border: OutlineInputBorder(),
|
||||
labelText: AppLocalizations.of(context)?.description ??
|
||||
'Description',
|
||||
hintText:
|
||||
AppLocalizations.of(context)?.describe_event ??
|
||||
'Describe the event'),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 30,
|
||||
),
|
||||
Container(
|
||||
height: 50,
|
||||
width: 250,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue,
|
||||
borderRadius: BorderRadius.circular(20)),
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
_updateEvent(context);
|
||||
}
|
||||
},
|
||||
child: Text(
|
||||
AppLocalizations.of(context)?.add_event ?? 'Add',
|
||||
style: TextStyle(color: Colors.white, fontSize: 25),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user