108 lines
3.5 KiB
Dart
108 lines
3.5 KiB
Dart
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<Settings> createState() => _SettingsState();
|
|
}
|
|
|
|
class _SettingsState extends State<Settings> {
|
|
late Future<String> initName;
|
|
late Future<String> initBLand;
|
|
TextEditingController nameVornameC = TextEditingController();
|
|
TextEditingController bLandC = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
initName = _loadName();
|
|
initBLand = _loadBLand();
|
|
}
|
|
|
|
Future<String> _loadName() {
|
|
Future<String> 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<String> _loadBLand() {
|
|
Future<String> 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);
|
|
}
|
|
|
|
void _saveBLand() async {
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
await prefs.setString('bLand', bLandC.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<String>(
|
|
future: initName, // your Future<String>
|
|
builder:
|
|
(BuildContext context, AsyncSnapshot<String> 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<String>(
|
|
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();
|
|
_saveBLand();
|
|
}),
|
|
);
|
|
}
|
|
}
|