I want to be able to update my forms in a futureBuilder. I need the futureBuilder to retrieve all the datas from the player in my DataBase. I am setting my initial values of my form right after I got the datas. When I'm trying to update de date using the Icon and DatePicker, the text doesn't change. The date is store in my "age" variable.
import 'dart:io';
import 'package:cleanmaybe/Controllers/players_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'login_form_view.dart';
class PlayerStatView extends StatefulWidget {
const PlayerStatView({Key? key}) : super(key: key);
@override
State<PlayerStatView> createState() => _PlayerStatViewState();
}
class _PlayerStatViewState extends State<PlayerStatView> {
late Future<dynamic> dataFuture;
ImagePicker picker = ImagePicker();
File? imageFile;
File? theNewPic;
String filepath = "";
File? image;
File? imageFile2;
late String pName;
late String lName;
late String phonePlayer;
late String age;
Future<void> _openImagePicker(ImageSource source) async {
final XFile? pickedImage = await picker.pickImage(source: source);
if (pickedImage != null) {
setState(() {
theNewPic = File(pickedImage.path);
filepath = theNewPic!.path;
});
print(pickedImage);
}
}
@override
void initState() {
super.initState();
dataFuture = getAllPlayerData();
}
@override
Widget build(BuildContext context) {
double imageSize = MediaQuery.of(context).size.width / 4;
return MaterialApp(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', ''),
Locale('fr', ''), // arabic, no country code
],
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("Profil du joueur"),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => Navigator.of(context).pop()),
actions: [
IconButton(
onPressed: () {
AlertDialog alert = AlertDialog(
title: const Text("Attention !"),
content: const Text(
"Voulez-vous vraiment supprimer ce profil?"),
actions: [
TextButton(
onPressed: () {
deletePlayer(context);
Navigator.of(context, rootNavigator: true).pop();
},
child: const Text("Oui")),
TextButton(
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
child: const Text("non"))
],
);
showDialog(
barrierDismissible: false,
context: context,
builder: (BuildContext ctx) {
return alert;
});
},
icon: const Icon(Icons.delete)),
IconButton(
onPressed: () {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => const LoginFormView()),
(Route<dynamic> route) => false);
},
icon: const Icon(Icons.exit_to_app))
]),
body: FutureBuilder<dynamic>(
future: dataFuture,
builder: (context, snapshot) {
var data = snapshot.data;
if (data == null) {
return const Center(child: CircularProgressIndicator());
} else {
pName = data[0]['firstName'];
lName = data[0]['lastName'];
imageFile = File(data[0]['photoName']);
phonePlayer = data[0]['phone'];
age = data[0]['age'];
// age = data[0]['age'];
return SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Container(
child: SizedBox(
child: Card(
color: Colors.white,
elevation: 10,
child: SizedBox(
child: Column(
children: [
SizedBox(
height: 200,
width: 200,
child: getImageChange(context))
],
),
),
),
),
),
Row(
children: <Widget>[
const SizedBox(
width: 128,
),
/* SizedBox(
width: 125,
),*/
IconButton(
onPressed: (() =>
_openImagePicker(ImageSource.camera)),
icon: const Icon(Icons.camera_alt_rounded),
),
const SizedBox(
width: 40,
),
IconButton(
onPressed: (() =>
_openImagePicker(ImageSource.gallery)),
icon: const Icon(Icons.photo_album_outlined),
),
],
),
const Divider(
color: Colors.blueAccent,
thickness: 0,
),
myTitle("Données du joueur"),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
Flexible(
child: Container(
padding: const EdgeInsets.all(10),
child: TextFormField(
initialValue: pName,
onChanged: (String value) {
setState(() {
pName = value;
});
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: "Prénom",
prefixIcon: Icon(Icons.person)),
),
)),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
Flexible(
child: Container(
padding: const EdgeInsets.all(10),
child: TextFormField(
initialValue: lName,
onChanged: (String value) {
setState(() {
lName = value;
});
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: "Nom de famille",
prefixIcon: Icon(Icons.person)),
),
)),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
Flexible(
child: Container(
padding: const EdgeInsets.all(10),
child: TextFormField(
initialValue: phonePlayer,
onChanged: (String value) {
setState(() {
phonePlayer = value;
});
},
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: "Numéro de téléphone",
prefixIcon: Icon(Icons.phone)),
),
)),
],
),
Container(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
mainAxisSize: MainAxisSize.max,
children: [
//Icon(Icons.cake),
Text("Date de naissance: "),
Builder(builder: (context) {
return IconButton(
icon: Icon(Icons.calendar_month),
onPressed: () {
showDatePicker(
locale: const Locale("fr", "FR"),
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1980),
lastDate: DateTime(2050),
).then((value) {
setState(() {
String formattedDate =
DateFormat('dd-MM-yyyy').format(value!);
age = formattedDate;
print(age);
});
});
},
);
}),
Flexible(
child: TextFormField(initialValue: age, onChanged: (String value){
setState(() {
age = value;
});
},),
),
],
),
),
],
));
}
}),
),
);
}
Future getImage(ImageSource source) async {
final chosenFile = await picker.pickImage(source: source);
if (chosenFile == null) {
print("j'ai rien");
} else {
var changing = File(chosenFile.path);
return changing;
}
}
Widget getImageChange(context) {
double imageSize = MediaQuery.of(context).size.width / 4;
if (theNewPic == null) {
if (imageFile?.path == '') {
return SizedBox(
width: 200,
child: Card(
color: Colors.white,
elevation: 10,
child: Container(
width: MediaQuery.of(context).size.width,
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(8),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(Icons.camera_alt_outlined),
Text(
"Ajoutez une photo",
textAlign: TextAlign.center,
)
],
),
),
),
);
} else {
return Image.file(
imageFile!,
height: imageSize,
width: imageSize,
fit: BoxFit.fill,
);
}
} else {
return Image.file(
theNewPic!,
height: imageSize,
width: imageSize,
fit: BoxFit.fill,
);
}
}
}
Text myTitle(String text) {
return Text(text,
style: const TextStyle(
color: Colors.black, fontWeight: FontWeight.bold, fontSize: 18));
}