1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| class Boutons {
String textOFF;
String textON;
bool isPressed;
Boutons(this.textOFF, this.textON, this.isPressed);
}
class _MyHomePageState extends State<MyHomePage> {
// définition couleurs
var pressedBackColor = Colors.red;
var defaultBackColor = Colors.lightGreen;
List<Boutons> boutons = [
Boutons('Bouton #1', 'Clicked #1', false),
Boutons('Bouton #2', 'Clicked #2', false),
Boutons('Bouton #3', 'Clicked #3', false),
Boutons('Bouton #4', 'Clicked #4', false),
];
void onPressed(Boutons obj) {
// change couleur de fond par défaut
defaultBackColor = Colors.yellow;
setState(() {
for (Boutons el in boutons) {
el.isPressed = (el == obj);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: _createButton(context, boutons),
)),
);
}
List<Widget> _createButton(BuildContext context, List<Boutons> boutons) {
return boutons.map((Boutons btn) {
var tColor = btn.isPressed ? Colors.white : Colors.black;
var bColor = btn.isPressed ? pressedBackColor : defaultBackColor;
var text = btn.isPressed ? btn.textON : btn.textOFF;
return RaisedButton(
textColor: tColor,
color: bColor,
child: Text(text, style: TextStyle(fontSize: 21)),
onPressed: () => onPressed(btn),
);
}).toList();
}
} |
Partager