Answer the question
In order to leave comments, you need to log in
Flutter|How to change color of AlertDialog button?
Hello!
I pray for help.
There is a StatefulWidget in which I call AlertDialog, created initState and setState, but the color of the button changes only if you close and open AlertDialog again, and not immediately when you click it.
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;
}
}
Answer the question
In order to leave comments, you need to log in
The question is closed.
It was necessary to wrap in StatefulBuilder:
StatefulBuilder(builder: (context, myState) {
return ElevatedButton(
onPressed: () {
myState(() {
_active = !_active;
});
},
child: Text('Work'),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
_active ? Colors.blue : Colors.red)),
);
}),
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question