import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:shared_preferences/shared_preferences.dart'; class Settings extends StatefulWidget { const Settings({super.key}); @override State createState() => _SettingsState(); } class _SettingsState extends State { late Future initName; late Future initBLand; TextEditingController nameVornameC = TextEditingController(); TextEditingController bLandC = TextEditingController(); @override void initState() { super.initState(); initName = _loadName(); initBLand = _loadBLand(); } Future _loadName() { Future initName = Future.delayed(Duration.zero, () async { final SharedPreferences prefs = await SharedPreferences.getInstance(); final String initName = prefs.getString('nameVorname') ?? ''; nameVornameC.text = initName; return initName; }); return initName; } Future _loadBLand() { Future initBLand = Future.delayed(Duration.zero, () async { final SharedPreferences prefs = await SharedPreferences.getInstance(); final String initBLand = prefs.getString('bLand') ?? ''; bLandC.text = initBLand; return initBLand; }); return initBLand; } void _saveName() async { final SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString('nameVorname', nameVornameC.text); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text(AppLocalizations.of(context)!.settings)), body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ Text(AppLocalizations.of(context)!.changenamestate ,style: const TextStyle(fontSize: 20, ),), FutureBuilder( future: initName, // your Future builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { return TextField( controller: nameVornameC, decoration: InputDecoration( labelText: AppLocalizations.of(context)!.namevorname, ), ); } else { // Optionally, return a placeholder widget while waiting return const CircularProgressIndicator(); } }, ), FutureBuilder( future: initBLand, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return TextField( controller: bLandC, decoration: InputDecoration(labelText: AppLocalizations.of(context)!.bland), ); } else { return const CircularProgressIndicator(); } }, ) ], ), ), ), floatingActionButton: FloatingActionButton( child: const Icon(Icons.save), onPressed: () { _saveName(); }), ); } }