Как использовать виджет упаковки в Flutter
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _selectedButton = null;
void setSelectedButton(int index) {
setState(() {
_selectedButton = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Wrap(children: [
for (int i = 0; i < 10; i++)
CustomElevatedButton(
i,
_selectedButton == null
? Colors.red
: _selectedButton == i
? Colors.red
: Colors.grey, () {
setSelectedButton(i);
}),
]),
),
);
}
}
class CustomElevatedButton extends StatelessWidget {
final int number;
final Color buttonColor;
final void Function() onPressed;
CustomElevatedButton(this.number, this.buttonColor, this.onPressed);
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(5.0),
child: Column(
children: <Widget>[
ElevatedButton(
onPressed: onPressed,
child: Text(
'${number + 1}',
textAlign: TextAlign.center,
),
style: ElevatedButton.styleFrom(
shadowColor: Colors.grey,
primary: buttonColor,
minimumSize: Size(70, 70),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
textStyle: TextStyle(
fontSize: 40,
fontWeight: FontWeight.bold,
),
),
),
],
),
);
}
}
mathiasgodwin