简体   繁体   English

当我按下按钮颤动时,有什么办法可以使用补间改变背景颜色

[英]is there any way i could change background color using tween when i press a button flutter

So I am trying to find a way where I can press a button, change the background color then get back to the original color using tween.所以我试图找到一种方法,我可以按下按钮,更改背景颜色,然后使用补间恢复到原始颜色。
Is there anyway I could possibly achieve this?反正我有可能做到这一点吗?

Here is an example of How you can change background with a tween.这是如何使用补间更改背景的示例。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // Remove the debug banner
      debugShowCheckedModeBanner: false,
      title: 'Tween',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<Color?> _color;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: Duration(seconds: 5),
      vsync: this,
    );

    _color =
        ColorTween(begin: Colors.blue, end: Colors.amber).animate(_controller);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          const SizedBox(height: 20),
          AnimatedBuilder(
            animation: _color,
            builder: (BuildContext _, Widget? __) {
              return Container(
                width: 500,
                height: 300,
                decoration: BoxDecoration(
                    color: _color.value, shape: BoxShape.rectangle),
              );
            },
          ),
           SizedBox(height: 10),
          ElevatedButton(
            onPressed: () {
              _controller.forward();
            },
            child: Text('Change background'),
          ),
          SizedBox(height: 10),
          ElevatedButton(
            onPressed: () {
              _controller.reverse();
            },
            child: Text('back to Orignal'),
          ),
        ],
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM