Bonjour,
Je rencontre un souci sur l’affichage d’un formulaire qui affiche l’email du membre ainsi qu’une case à cocher pour indiquer s’il souhaite recevoir la newsletter ou non. Donc j’appelle un script php/mysql qui me retourne l’email et la valeur de newsletter (soit 0 ou 1) et j’affiche dans un builder les valeurs retournées par le script. ça m’affiche donc son email et la valeur de newsletter en cochant ou non qui provient des valeurs de la base de données. Le code ci-dessous permet de cocher ou non à l’initialisation mais il me fout en même temps dans la merde car quand on coche ou décoche un setstate est réalisé pour changer la valeur de la variable « newsletter » mais le souci c’est que ça rappelle le code ci-dessous et écrase donc la valeur. Le souci est donc qu’il faudrait que le code ci-dessous qui est un code d’initialisation ne s’éxécute que à l’initialisation et pas lors du setstate. Quelqu’un aurait une idée ?
if (values.newsletter=="1") {
newsletter=true;
}
else {
newsletter=false;
}
Voici le code complet :
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'menu_member.dart';
import 'globals.dart' as globals;
import 'appbar_draw.dart';
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:email_validator/email_validator.dart';
import 'package:awesome_page_transitions/awesome_page_transitions.dart';
// Create a Form widget.
class Affiche_Profil extends StatefulWidget {
@override
_Affiche_Profil_State createState() {
return _Affiche_Profil_State();
}
}
// Create a corresponding State class.
// This class holds data related to the form.
class _Affiche_Profil_State extends State<Affiche_Profil> {
@override
final _formKey = GlobalKey<FormState>();
bool newsletter=false;
final _emailController=TextEditingController();
Future <user> profil;
Future <user> Display_Profil () async {
// SERVER LOGIN API URL
var url = 'https://www.fortune-island.com/app/info_profil.php';
var data = {
'id_membre': globals.id_membre,
};
var data_encode = jsonEncode(data);
// Starting Web API Call.
var response = await http.post(url,body: data_encode,headers: {'content-type': 'application/json','accept': 'application/json','authorization': globals.token});
// Getting Server response into variable.
var jsondata = json.decode(response.body);
user profile=user(jsondata["pseudo"],jsondata["email"],jsondata["newsletter"]);
return profile;
}
void initState() {
profil = Display_Profil();
super.initState();
}
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
Colors.blue[300],Colors.blue[400]
],
),
),
),
Scaffold(
appBar: drawappbar(true),
backgroundColor: Colors.transparent,
drawer: new DrawerOnly(className: Affiche_Profil()),
body:
Container(
height: MediaQuery
.of(context)
.size
.height,
width: MediaQuery
.of(context)
.size
.width,
child:
FutureBuilder(
future: profil,
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Center(
child: new CircularProgressIndicator(),);
default:
if (snapshot.hasError) {
return new Center(
child: new Text('Error: ${snapshot.error}'),);
}
else {
user values = snapshot.data;
if (snapshot.hasData==false) {
return Container(
child: Center(
child: Text("Données inaccessible !!!",style: TextStyle(color: Colors.white))
)
);
}
else {
_emailController.text=values.email;
if (values.newsletter=="1") {
newsletter=true;
}
else {
newsletter=false;
}
return Form(
key: _formKey,
autovalidate: true,
child:
ListView(
children: <Widget>[
Center(
child: Container(
margin: const EdgeInsets.only(top: 20.0),
child: Text("VOS INFOS",textAlign: TextAlign.center,style: TextStyle(fontSize: 30.0,color: Colors.white))
),
),
Padding(
padding: const EdgeInsets.only(top:8.0),
child:
TextFormField(
autofocus: true,
obscureText: false,
controller: _emailController,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0)
),
fillColor: Colors.white, filled: true,
icon: Icon(Icons.email,color: Colors.white),
hintText: "Entrez votre email",
contentPadding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
hintStyle: TextStyle(color: Colors.grey[500],fontSize: 10),
),
validator: (value) {
if (value.isEmpty) {
return 'Entrez votre email';
}
if (!EmailValidator.validate(value)) {
return 'Email non valide';
}
return null;
},
),
),
Padding(
padding: const EdgeInsets.only(top:8.0),
child:
CheckboxListTile(
title: Text("Newsletter :",style: TextStyle(color: Colors.white)),
value: newsletter,
onChanged: (bool newValue) {
setState(() {
newsletter = newValue;
});
}
),
),
Padding(
padding: const EdgeInsets.only(top:8.0),
child :
Center(
child: Container(
width:MediaQuery.of(context).size.width,
height:45,
child: RaisedButton(
color: Colors.green,
textColor: Colors.white,
padding: EdgeInsets.fromLTRB(9, 9, 9, 9),
child: Text('Modifier'),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25)
),
onPressed: () {
// Validate returns true if the form is valid, or false
// otherwise.
if (_formKey.currentState.validate()) {
ModifyInfos();
}
else {
return null;
}
},
),
),
),
),// <-- label
]
)
);
}
}
}
}
)
)
)
]
);
}
Future ModifyInfos() async{
// Getting value from Controller
String email = _emailController.text;
var url = 'https://www.fortune-island.com/app/modify_profil.php';
// Store all data with Param Name.
String etatnews="0";
if (newsletter==true) {
etatnews="1";
}
var data = {'id_membre':globals.id_membre,'email': email,'news': etatnews};
var data_encode=jsonEncode(data);
print(data_encode);
// Starting Web API Call.
var response = await http.post(url, body: data_encode,headers: {'content-type': 'application/json','accept': 'application/json','authorization': globals.token});
print(response.body);
Map <String,dynamic> map = json.decode(response.body);
if(map["status"] == 1)
{
AwesomeDialog(context: context,
useRootNavigator: true,
dialogType: DialogType.SUCCES,
animType: AnimType.BOTTOMSLIDE,
tittle: 'MODIFICATION DU PROFIL',
desc: map["libelle"],
btnOkOnPress: () {
setState(() {
profil = Display_Profil();
});
}).show();
}else{
// If Email or Password did not Matched.
// Showing Alert Dialog with Response JSON Message.
AwesomeDialog(context: context,
useRootNavigator: true,
dialogType: DialogType.INFO,
animType: AnimType.BOTTOMSLIDE,
tittle: 'INFORMATIONS INCHANGEES',
desc: map["libelle"],
btnOkOnPress: () {
setState(() {
profil = Display_Profil();
});
}).show();
}
}
}
class user {
final String pseudo;
final String email;
final String newsletter;
const user(this.pseudo,this.email,this.newsletter);
}