简体   繁体   English

如何在Flutter中为自定义画家的颜色设置动画?

[英]How to animate a color of a custom painter in Flutter?

I'm tring to animate a Progress-bar in Flutter. 我想为Flutter中的进度栏​​设置动画。 So far I'm able to paint the progressbar with a CustomPainter class :) 到目前为止,我已经可以使用CustomPainter类来绘制进度条了:)

My goal is to make the first bar (custompainter), acting like the secondbar: 我的目标是制作第一个栏(custompainter),就像第二个栏一样:

在此处输入图片说明

I found a lot of examples to make a redical progressbar, but that excists of one CustomPainter. 我发现有很多例子可以使进度条具有重复性,但是只使用了一个CustomPainter。 I (think) I need more seperate custompaintersto draw the hole line, like so: 我(认为)我需要更多单独的custompainters来绘制孔线,如下所示:

But now I want to start animatin the first dot I get stucked, what and how should I pass as values to animate the first circle? 但是现在我要开始对卡住的第一个点进行动画处理,我应该如何以及如何将其作为值传递给第一个圆以动画形式? Next I have to animate the lines. 接下来,我必须对线条进行动画处理。

Here is my code so far (a GitHub gist): https://gist.github.com/LiveLikeCounter/a1dffbe8953d39aa4af32c8f4dfc6553 到目前为止,这是我的代码(GitHub原理): https : //gist.github.com/LiveLikeCounter/a1dffbe8953d39aa4af32c8f4dfc6553

Thank you so much in advanced! 非常感谢您!

You can use an Row widget with a combination of Container and LinearProgressIndicator 您可以将Row小部件与ContainerLinearProgressIndicator结合使用

Since I am not aware of the rest of the app I'll be providing a sample widget tree for your reference. 由于我不知道该应用程序的其余部分,因此我将提供一个示例小部件树供您参考。

Widget Tree: 小部件树:

Row(
  children: <Widget>[
    Container([...]),
    LinearProgressIndicator([...]),
    Container([...]),
    LinearProgressIndicator([...]),
    Container([...]),
    ]
)

To make a circular Container along with the color transition, 要制作一个带有color过渡的圆形Container

AnimatedContainer(
duration: Duration(seconds:2),
width: x,
height: x,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(x/2),
// color: (progressvalue>200)? Colors.yellow : Colors.grey 
)
)

Sample Logic: 范例逻辑:

Container1 - progressValue > 0  
LinearProgressIndicator - (progressValue-10) to 110 
Container2 - progressValue > 110 
LinearProgressIndicator - (progressValue-120) to 220 
Container2 - progressValue > 220 

The above logic can be modified as per your convenience. 上述逻辑可以根据您的方便进行修改。

Working example for LinearProgressIndicator , LinearProgressIndicator工作示例,

import 'dart:async';
import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    debugShowCheckedModeBanner: false,
    home: new MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => new MyAppState();
}

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Slider Demo'),
      ),
      body: new Container(
        color: Colors.blueAccent,
        padding: new EdgeInsets.all(32.0),
        child: new ProgressIndicatorDemo(),
      ),
    );
  }
}

class ProgressIndicatorDemo extends StatefulWidget {

  @override
  _ProgressIndicatorDemoState createState() =>
      new _ProgressIndicatorDemoState();
}

class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo>
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  Animation<double> animation;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
        duration: const Duration(milliseconds: 2000), vsync: this);
    animation = Tween(begin: 0.0, end: 1.0).animate(controller)
      ..addListener(() {
        setState(() {
          // the state that has changed here is the animation object’s value
        });
      });
    controller.repeat();
  }


  @override
  void dispose() {
    controller.stop();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
        child: new Container(
          child:  LinearProgressIndicator( value:  animation.value,),

        )
    );
  }

}

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

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