Files
fforte/lib/screens/sharedWidgets/var_text_field.dart

160 lines
4.8 KiB
Dart

import 'package:fforte/enums/databases.dart';
import 'package:fforte/methods/excursion_db_helper.dart';
import 'package:fforte/methods/place_db_helper.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class VarTextField extends StatefulWidget {
final TextEditingController textController;
final String localization;
final DatabasesEnum dbDesignation;
final String dbName;
final String? defaultValue;
final String? otherDefault;
final bool required;
const VarTextField({
super.key,
required this.textController,
required this.localization,
required this.dbName,
required this.required,
required this.dbDesignation,
this.defaultValue,
this.otherDefault,
});
@override
State<VarTextField> createState() => _VarTextFieldState();
}
class _VarTextFieldState extends State<VarTextField> {
List<String> dbVar = [];
@override
void initState() {
super.initState();
if (widget.textController.text == "" && widget.defaultValue != null) {
_loadPref();
}
if (widget.otherDefault != null) {
widget.textController.text = widget.otherDefault!;
}
_loadData().then((e) => dbVar = e);
}
Future<List<String>> _loadData() async {
List<Map<String, dynamic>> entries = [];
List<Map<String, dynamic>> templatesEntries = [];
if (widget.dbDesignation == DatabasesEnum.place) {
entries = await PlaceDBHelper().getAllMainEntries();
templatesEntries = await PlaceDBHelper().getAllTemplates();
} else if (widget.dbDesignation == DatabasesEnum.excursion) {
entries = await ExcursionDBHelper().getAllMainEntries();
templatesEntries = await ExcursionDBHelper().getAllTemplates();
}
List<String> erg = [];
for (var element in entries) {
for (var key in element.keys) {
if (key == widget.dbName && element[key].toString() != "") {
erg.add(element[key].toString());
}
}
}
for (var element in templatesEntries) {
for (var key in element.keys) {
if (key == widget.dbName && element[key].toString() != "") {
erg.add(element[key].toString());
}
}
}
return erg;
}
void _loadPref() {
Future.delayed(Duration.zero, () async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String bLand = prefs.getString(widget.defaultValue!) ?? "";
setState(() {
widget.textController.text = bLand;
});
});
}
@override
Widget build(BuildContext context) {
return Row(
children: [
Expanded(
flex: 5,
child: TextField(
controller: widget.textController,
keyboardType: TextInputType.multiline,
maxLines: null,
onChanged: (value) {
setState(() {
widget.textController.text = value;
});
},
decoration: InputDecoration(
hintText: widget.localization,
enabledBorder:
widget.required
? (widget.textController.text.isEmpty
? const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red),
)
: const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green),
))
: const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
focusedBorder:
widget.required
? (widget.textController.text.isEmpty
? const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red),
)
: const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.green),
))
: const UnderlineInputBorder(
borderSide: BorderSide(color: Colors.grey),
),
),
),
),
const Expanded(child: SizedBox(width: 15)),
Expanded(
flex: 1,
child: Align(
alignment: Alignment.bottomLeft,
child: PopupMenuButton<String>(
onSelected: (String value) {
setState(() {
widget.textController.text = value;
});
},
itemBuilder: (BuildContext context) {
return dbVar.map((String item) {
return PopupMenuItem<String>(value: item, child: Text(item));
}).toList();
},
child: const Icon(Icons.arrow_drop_down),
),
),
),
],
);
}
}