简体   繁体   English

如何在 Flutter 中创建自定义圆形进度条?

[英]How to create a custom circular progress bar in Flutter?

I want to make a custom circular progress bar.我想制作一个自定义的圆形进度条。 In which, I can set a maximum value like 1200. The progress of the circle changes according to my input value.其中,我可以设置一个最大值,如 1200。圆圈的进度根据我输入的值而变化。 If I input 100 as a value, it shows it as progress.如果我输入 100 作为值,它将显示为进度。 If I again input 200, it sums this value with the previous one such as: 200+100= 300 and shows the progress.如果我再次输入 200,它会将这个值与前一个值相加,例如:200+100=300 并显示进度。

It will do same until it reaches the maximum value.它会做同样的事情,直到达到最大值。 If it crosses the maximum value then it calculates the extra value I inputted.如果它超过最大值,那么它会计算我输入的额外值。

Like, if I input 1500, it shows extra value 300.比如,如果我输入 1500,它会显示额外的值 300。

I have checked a lot of articles about circular progress bar.我查了很多关于循环进度条的文章。

But I can't understand how to do this in Flutter.但我不明白如何在 Flutter 中做到这一点。

You can use CircularProgressIndicator.adaptive, it gives you the ability to give the progress "value".您可以使用 CircularProgressIndicator.adaptive,它使您能够赋予进度“价值”。

you can give any progress value from 0 to 1 (1 being the full circle), so can give your values as fraction (progressValue/totalValue) to the value parameter.您可以提供从 0 到 1 的任何进度值(1 是完整的圆圈),因此可以将您的值作为分数 (progressValue/totalValue) 提供给 value 参数。

For example in your case 100/1200例如在你的情况下 100/1200

and for the extra value, you can add a simple check that will determine if the progress value is greater than your totalValue then show their difference对于额外的价值,您可以添加一个简单的检查,以确定进度值是否大于您的 totalValue 然后显示它们的差异

Here is the code that increases the progress value by 100 by clicking on the button and also shows the extra value (if any).这是通过单击按钮将进度值增加 100 并显示额外值(如果有)的代码。

class ExampleScreen extends StatefulWidget {

 const ExampleScreen({Key? key}) : super(key: key);

  @override
  State<ExampleScreen> createState() => _ExampleScreenState();
}

class _ExampleScreenState extends State<ExampleScreen> {
  int totalValue = 1200;
  int loadingValue = 0;

  void onAddButtonPressed() {
    setState(() {
      loadingValue += 100;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: onAddButtonPressed,
        tooltip: 'Add 100',
        child: const Icon(Icons.add),
      ),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            CircularProgressIndicator.adaptive(
              value: loadingValue / totalValue,
            ),
            if (loadingValue > totalValue) ...[
              const SizedBox(
                height: 10,
              ),
              Text(
                'your extra values are ${loadingValue - totalValue}',
              ),
            ]
          ],
        ),
      ),
    );
  }
}

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

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