Всем привет!
Молю о помощи.
Имеется StatefulWidget в котором вызываю AlertDialog, создал initState и setState, но цвет кнопки меняется только если закрыть и ещё раз открыть AlertDialog, а не сразу при нажатии.
import ...
class HomeScreen extends StatefulWidget {
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
late String _addField;
late IconData _selectedValue;
late bool _active;
@override
void initState() {
_addField = 'Pusto';
_active = false;
_selectedValue = Icons.delete_forever_outlined;
super.initState();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Padding(
padding: const EdgeInsets.only(left: 8, right: 8),
child: Column(children: [
ActiveTaskInfo(
task: tasks.first,
),
const TextWidget(),
Expanded(child: TasksList()),
]),
),
//bottomNavigationBar: const BottomBar(),
//floatingActionButton: FloatingButton(),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Add Task'),
content: TextField(onChanged: (String value) {
_addField = value;
}),
actions: [
DropdownButton<IconData>(
value: _selectedValue,
onChanged: (IconData? newValue) {
setState(() {
_selectedValue = newValue!;
});
},
items: dropdownItems,
),
ElevatedButton(
onPressed: () {
setState(() {
tasks.addAll({
TaskData(
taskName: _addField,
tagNameOne: 'Work',
tagNameTwo: 'Rasion Project',
icon: _selectedValue,
taskTime: '00:32:10')
});
decorations.addAll({
TaskTagsDecorations(
firstTagTextColor: const Color(0xffFD5B71),
secondTagTextColor: const Color(0xff9B51E0),
firstTagColor: const Color(0xff1F0D20),
secondTagColor: const Color(0xff150C2B),
iconColor: const Color(0xff7012CF))
});
});
Navigator.of(context).pop();
},
child: const Text('Add')),
ElevatedButton(
onPressed: () {
setState(() {
_active = !_active;
});
},
child: Text('Work'),
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(_active ? Colors.blue : Colors.red)
),
),
],
);
});
},
child: const Text('Add'),
),
),
);
}
List<DropdownMenuItem<IconData>> get dropdownItems {
List<DropdownMenuItem<IconData>> menuItems = [
const DropdownMenuItem(
child: Icon(Icons.delete_forever_outlined),
value: Icons.delete_forever_outlined),
const DropdownMenuItem(
child: Icon(Icons.category_rounded), value: Icons.category_rounded),
const DropdownMenuItem(
child: Icon(Icons.format_quote_rounded),
value: Icons.format_quote_rounded),
const DropdownMenuItem(
child: Icon(Icons.menu_book_rounded), value: Icons.menu_book_rounded),
];
return menuItems;
}
}