Flutter中如何制作一个不断增长和缩小的小部件?

kam*_*vyz 2 dart flutter

我如何使颤振中的小部件不断增大和缩小尺寸,以便用户可以识别出这是他或她可以按下的某种按钮?

Mar*_*lla 5

拥有StatefulWidget,这是一种简单的方法:

var height = 100.0;
var width = 100.0;

@override
void initState() {
  super.initState();
  _animation();
}

void _animation() {
  Timer.periodic(Duration(seconds: 1), (timer) {
    setState(() {
      height = height == 100.0 ? 50.0 : 100.0;
      width = width == 100.0 ? 50.0 : 100.0;
    });
  });
}

@override
Widget build(BuildContext context) {
  return Material(
    child: Center(
      child: AnimatedContainer(
        duration: Duration(seconds: 1),
        height: height,
        width: width,
        decoration: BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.circular(50)
        ),
      ),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)

此代码的结果如下:

在此处输入图片说明