📜  如何在颤振中使用 wrap 小部件 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:24.357000             🧑  作者: Mango

代码示例1
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
  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: [
          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,
              ),
            ),
          ),
        ],
      ),
    );
  }
}