I have a strange bug. When i display the form i get a boolean value for fill a checkbox. If value is 0 from the database i can check and uncheck it and it works well but if the value is 1 so the checkbox appears checked (normal result) but i can't uncheck it. I use the newsletter value in my database for check or not check the checkbox at the beginning. I think probably the problem is there, i use setstate for refresh widget with the new value of newsletter variable but it uses the database value which is "checked" and delete the value of newsletter changed by the checkbox check event. So the problem seems to be that when i display the form i set the value of variable called newsletter with the value of database but normally i need to do that only at the initialisation of the page, how can i do that ?
if (values.newsletter=="1") {
newsletter=true;
}
else {
newsletter=false;
}
Here is my code :
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);
}