I have tried looking at similar questions here without any luck. I wonder if my problem is due to my ListView being inside a FutureBuilder which again is inside its own widget, which again is inside an alertdialog with its own statefulbuilder.
I am trying to change the color of a button inside a listview after it is pressed.
The color is updated, but I have to rerender the alertdialog/listview to see the changes.
I removed a good portion of irrelevant code to make it readable and more easy to see the structure.
The structure of the code is as follows:
class _MainPageState extends State<MainPage> {
bool test = false; //Bool controlling the color of the button
_showDialog1(chosenTitle, choice) { //Function to trigger dialog
showDialog(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(builder: (context, setState) { //StatefulBuilder placed because thats what I thought was needed for my project to work.
return AlertDialog(
content: SingleChildScrollView(
child: choice,
),}})})
Widget hamburgerValue(value) { //THIS is the widget placed in the "choice" parameter above
var _messageController = TextEditingController();
List<dynamic> privSessions = [];
return FutureBuilder(
future: getData(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
return Wrap(
children: [
Column(
children: [
Container(
constraints: BoxConstraints(maxWidth: 300),
child: Text('Placeholder'),
),
Padding(
padding: const EdgeInsets.fromLTRB(0, 30, 0, 30.0),
child: Container(
color: Colors.white,
width: 300,
child: TextField(
controller: _messageController,
maxLines: 5,
decoration: InputDecoration(
//labelText: 'Din melding her',
border: OutlineInputBorder(),
hintText: 'Din melding her'),
),
),
),
],
),
SizedBox(
width: 150,
height: 300,
child: Container(
child: ListView.builder(
itemCount: privSessions.length,
itemBuilder: (context, i) {
bool test = false; //VARIABLE TO DETERMINE COLOR CHANGE
if (i == 0) {
return Column(
children: [
Text(privSessions[i]['day'],
style: TextStyle(fontSize: 20)),
Padding(
padding:
const EdgeInsets.only(bottom: 10.0),
child: Text(
datoFull,
style: TextStyle(fontSize: 15),
),
),
ElevatedButton(
onPressed: privSessions[i]
['available']
? () {
setState(() {
test = true; //WHERE THE SETSTATE HAPPENS
});
}
: null,
style: ElevatedButton.styleFrom(
primary: test
? Colors.red
: Colors.cyan),
child: Column(
children: [
Text(time,
style: TextStyle(
color: Colors.white)),
],
))
],
);
}