#flutter #dart
Вопрос:
вот класс местоположения, определенный в разделе местоположение.файл для дротиков
class Location extends StatefulWidget {
@override
_LocationState createState() => _LocationState();
}
class _LocationState extends State<Location> {
@override
void initState(){
_getLocation();
super.initState();
}
String _value = "";
String _adresse = "";
String get adresse => _adresse;
String _coordonnees = "";
String get coordonnees => _coordonnees;
var _locationMessage;
late Position currentPosition;
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _getLocation(),
builder: (context, snapshot) {
if(snapshot.hasData){
return TextFormField(
initialValue : "${(_locationMessage as String)}",
decoration: InputDecoration(labelText: 'adresse', icon: Icon(Icons.location_on)),
enabled: true,
validator: (value) {
if (value == null || value.isEmpty) {
return "Veuillez entrez votre adresse";
}
return null;
},
//here i save the value of my textFormfield in my variable class
onSaved: (value) {
this._adresse = value.toString();
this._coordonnees = _locationMessage;
},
);
}else if(snapshot.hasError){
//error
return Text("Une erreur est survenue lors du chargement de l'adresse.");
}else{
return LinearProgressIndicator(value: null);
}
},
);
}
}
в файле form.dart я использую виджет местоположения.
как я могу использовать метод onsave для присвоения значения «адрес» и «координаты» моего объекта местоположения переменной «адрес» и «координаты» файла form.dart?
class Formulaire extends StatefulWidget {
@override
_FormulaireState createState() => _FormulaireState();
}
class _FormulaireState extends State<Formulaire> {
late SaveModel model;
String _nom = "";
String value = "";
String _date = "";
String _adresse = "";
String _coordonnees = "";
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
Widget _buildNom() {
return TextFormField(
decoration: InputDecoration(labelText: 'nom', icon: Icon(Icons.camera_alt)),
validator: (value) {
if (value == null || value.isEmpty) {
return "Veuillez entrez votre nom";
}
return null;
},
//here i can use the onsave method without any problem
onSaved: (value) {
this._nom = value.toString();
this.model.nom = this._nom;
},
);
}
Widget _buildDate() {
this._date = DateTime.now().toString();
return DateTimeFormField(
decoration: InputDecoration(labelText: 'nom', icon: Icon(Icons.access_time)),
initialValue: DateTime.now().toLocal(),
enabled: false,
//here again i can use the onsave method without any problem
onSaved: (value) {
this._date = value.toString();
this.model.date = this._date;
},
);
}
//the callback when i want to validate my form
_valid() async {
if (this._formkey.currentState!.validate()) {
return;
} else {
this._formkey.currentState?.save();
}
}
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(24),
child: Form(
key: _formkey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildNom(),
_buildDate(),
//here i create the Location widget
// my question : how can i use the onsave method to assign a the "address" and "coordonnee" value of my Location object to the variable "adresse" and "coordonnees" of form.dart file ?
Location(),
SizedBox(
height: 100,
),
ElevatedButton(
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<Color>(Colors.redAccent)),
onPressed: _valid,
child: Text(
'Valider',
style: TextStyle(color: Colors.white54, fontSize: 16),
),
),
],
),
),
);
}
i don’t have any idea of how to do that.