I have programmed a page in Flutter. You can use two arrows (elevated buttons) to control which list element should be displayed. If the first element is reached, the left arrow should be hidden. If the last element is reached, the right button should be hidden. This also works to some extent. You can no longer press the button directly, but the color change to gray is delayed, which is particularly annoying if you want to look through a picture gallery, for example. Unfortunately, I can’t figure out why.
import 'package:flutter/material.dart';
class ButtonProblem extends StatefulWidget {
const ButtonProblem({
super.key,
});
@override
State<ButtonProblem> createState() => _ButtonProblemState();
}
class _ButtonProblemState extends State<ButtonProblem> {
late int currentIndex;
late List<int> list;
@override
void initState() {
super.initState();
//Liste zum durchgehen
list = [0, 1, 2, 3, 4, 5, 6];
currentIndex = 0;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Button Problem'),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
body: Column(children: [
SizedBox(
height: 20,
),
Text("currentIndex: ${currentIndex.toString()}"),
Text("current list item: ${list[currentIndex].toString()}"),
SizedBox(
height: 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: currentIndex > 0 ? () => navigateToPrevious() : null,
style: ElevatedButton.styleFrom(
primary: Colors.blue,
minimumSize: const Size(150, 50),
),
child: const Icon(Icons.arrow_back, color: Colors.white),
),
ElevatedButton(
onPressed: (currentIndex < list.length - 1)
? () => navigateToNext()
: null,
style: ElevatedButton.styleFrom(
primary: Colors.blue,
minimumSize: const Size(150, 50),
),
child: Icon(Icons.arrow_forward, color: Colors.white),
),
],
),
]),
);
}
navigateToPrevious() {
setState(() {
currentIndex--;
});
}
navigateToNext() {
setState(() {
currentIndex++;
});
}
}
I have tried to use the IgnorePointer widget, but that also didn’t help.
Would be grateful for any help