简体   繁体   English

如何为 Flutter 中的 LinearProgressIndicator 添加边框/角半径?

[英]How to add a border/corner radius to a LinearProgressIndicator in Flutter?

I am trying to add a border radius to a LinearProgressIndicator in Flutter.我正在尝试将边界半径添加到LinearProgressIndicator中的 LinearProgressIndicator。

When I replace the LinearProgressIndicator with another widget (eg Text ) in the code below, it works, as expected.当我在下面的代码中用另一个小部件(例如Text )替换LinearProgressIndicator时,它按预期工作。

Container(
  decoration: new BoxDecoration(
      borderRadius:
          new BorderRadius.all(const Radius.circular(20.0))),
  child: LinearProgressIndicator(
    value: _time,
  ),
) 

一只忙碌的猫

1) Using Widget 1) 使用小工具

 Container(
          margin: EdgeInsets.symmetric(vertical: 20),
          width: 300,
          height: 20,
          child: ClipRRect(
            borderRadius: BorderRadius.all(Radius.circular(10)),
            child: LinearProgressIndicator(
              value: 0.7,
              valueColor: AlwaysStoppedAnimation<Color>(Color(0xff00ff00)),
              backgroundColor: Color(0xffD6D6D6),
            ),
          ),
        )

在此处输入图片说明

2) Using dependency 2) 使用依赖

List of different types indicator https://pub.dev/packages/percent_indicator不同类型指标列表https://pub.dev/packages/percent_indicator

Try this template code试试这个模板代码

        child:  Padding(
          padding: EdgeInsets.all(15.0),
          child:  LinearPercentIndicator(
            width: MediaQuery.of(context).size.width - 50,
            animation: true,
            lineHeight: 20.0,
            animationDuration: 2000,
            percent: 0.9,
            linearStrokeCap: LinearStrokeCap.roundAll,
            progressColor: Colors.greenAccent,
          ),
        )

在此处输入图片说明

Edit: I just wanted to point out my answer doesn't account for rounding the end of the 'value' fill inside the bar, which the OP wanted.编辑:我只是想指出我的答案并没有考虑到 OP 想要的栏内“值”填充的结尾。 However it seems many people came to this question looking for the answer I provided, (as did I before I found the answer and came back to respond).然而,似乎很多人来到这个问题寻找我提供的答案,(就像我在找到答案并回来回应之前一样)。

So if you need the inside of this indicator also to have a rounded edge, as of now I think the accepted answer from Amit is the best route.所以如果你需要这个指标的内部也有一个圆边,到目前为止,我认为 Amit 接受的答案是最好的途径。 If the inside of the indicator can have a flat edge and you don't want to use a third party package, I think my answer below is still the best option.如果指标的内部可以有一个平坦的边缘并且您不想使用第三方包,我认为我下面的答案仍然是最好的选择。

Original:原来的:

You actually don't need to use a third party package, and can wrap your LinearProgressIndicator with the ClipRRect widget and give it a circular border radius.您实际上不需要使用第三方包,并且可以使用ClipRRect小部件包装您的LinearProgressIndicator并为其指定圆形边框半径。 If you want to give it a specific thickness/height then you could use a container as an intermediary:如果你想给它一个特定的厚度/高度,那么你可以使用容器作为中介:

ClipRRect(
  borderRadius: BorderRadius.circular(8),
  child: Container(
    height: 10,
      child: LinearProgressIndicator(
        value: 0.35, // percent filled
        valueColor: AlwaysStoppedAnimation<Color>(Colors.orange),
        backgroundColor: Color(0xFFFFDAB8),
      ),
    ),
  )

would produce this, if placed in another widget with a defined width:如果将其放置在具有定义宽度的另一个小部件中,则会产生此效果:

在此处输入图片说明

Let's try my custom progress bar, simple and don't use any library :)让我们试试我的自定义进度条,简单且不使用任何库:)

在此处输入图片说明

class ProgressBar extends StatelessWidget {
 final double max;
 final double current;
 final Color color;

 const ProgressBar(
  {Key? key,
  required this.max,
  required this.current,
  this.color = AppColors.secondaryColor})
  : super(key: key);
  @override
  Widget build(BuildContext context) {
  return LayoutBuilder(
  builder: (_, boxConstraints) {
    var x = boxConstraints.maxWidth;
    var percent = (current / max) * x;
    return Stack(
      children: [
        Container(
          width: x,
          height: 7,
          decoration: BoxDecoration(
            color: Color(0xffd3d3d3),
            borderRadius: BorderRadius.circular(35),
          ),
        ),
        AnimatedContainer(
          duration: Duration(milliseconds: 500),
          width: percent,
          height: 7,
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.circular(35),
          ),
        ),
      ],
    );
  },
);
}
}

I am going to leave my answer in case someone is looking for something custom, basic with custom BorderRadius如果有人正在寻找自定义的东西,我将留下我的答案,基本的自定义 BorderRadius

      Container(
        height: 24.0,
        margin: EdgeInsets.only(top: 12.0, bottom: 2.0),
        decoration: BoxDecoration(
          color: Colors.grey,
          borderRadius: BorderRadius.all(Radius.circular(4.0)),
        ),
        clipBehavior: Clip.antiAlias,
        child: FractionallySizedBox(
          alignment: Alignment.centerLeft,
          widthFactor: 0.57,
          heightFactor: 1.0,
          child: Container(
            decoration: BoxDecoration(
              color: Colors.blueAccent,
              borderRadius: BorderRadius.all(Radius.circular(4.0)),
            ),
            clipBehavior: Clip.antiAlias,
          ),
        ),
      ),

Feel free to try it out :)随意尝试一下:)

You can try this code:你可以试试这段代码:

Container(
   decoration: const BoxDecoration(
   borderRadius: BorderRadius.all(Radius.circular(40.0))),
   child: const ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(20)),
                  child: LinearProgressIndicator(
                    minHeight: 5.0,
                    value: 0.2,
                    color: Colors.white,
                    backgroundColor: Color(0xFF3B2D73),
                  ),
                ),

To make the inner bar rounded, we need to use the following logic :要使内栏变圆,我们需要使用以下逻辑:

  1. Make the Outer progress bar rounded, use ClipRRect and set borderRadius使外部进度条变圆,使用 ClipRRect 并设置borderRadius

    <\/li>

  2. To make the inner bar rounded, we can use Container, where we will make one side (right side) rounded using borderRadius.要使内栏变圆,我们可以使用 Container,我们将使用borderRadius 使一侧(右侧)变圆。 Then, we will place it on top of our progress bar, using Stack.然后,我们将使用 Stack 将它放在进度条的顶部。 The width of the container will be actually the progress shown in the progress bar.容器的宽度实际上将是进度条中显示的进度。

     Container( width: (MediaQuery.of(context).size.width - 30) * calculateProgressBarValue(Data1, Data2), height: 6, decoration: BoxDecoration( borderRadius: const BorderRadius.only( topLeft: Radius.circular(CommonDimens.MARGIN_10), bottomLeft: Radius.circular(CommonDimens.MARGIN_10), ), color: progressBarColor, ),<\/code><\/pre><\/li><\/ol>

    Full code :完整代码:

     ClipRRect( borderRadius: const BorderRadius.all(Radius.circular(10)), child: Stack( children: [ ClipRRect( borderRadius: const BorderRadius.all(Radius.circular(10)), child: LinearProgressIndicator( minHeight: 6.0, valueColor: AlwaysStoppedAnimation<Color>(Colors.grey), value: 1, ), ), Positioned( right: 0, child: Container( width: ((MediaQuery.of(context).size.width) - 30) * calculateProgressBarValue(Data1,Data2), height: 6, decoration: BoxDecoration( borderRadius: const BorderRadius.only( topLeft: Radius.circular(CommonDimens.MARGIN_10), bottomLeft: Radius.circular(CommonDimens.MARGIN_10), ), color: progressBarColor, ), ), ], ), );<\/code><\/pre>"

You can make use of curved_progress_bar , It has both CurvedCircularProgressIndicator as well as CurvedLinearProgressIndicator, and it works just like normal CircularProgressIndicator and LinearProgressIndicator.您可以使用curved_progress_bar ,它既有CurvedCircularProgressIndicator 也有CurvedLinearProgressIndicator,它的工作原理与普通的CircularProgressIndicator 和LinearProgressIndicator 一样。

Do not need to wrap LinearPercentIndicator inside a Container widget.不需要将 LinearPercentIndicator 包装在 Container 小部件中。 You could get the desired output only using LinearPercentIndicator.您可以仅使用 LinearPercentIndicator 获得所需的 output。

See the template code看模板代码

    LinearPercentIndicator(
        lineHeight: 40.0,
        barRadius: const Radius.circular(20.0),
        percent: 0.7,
        animation: true,
        animationDuration: 1000,
        backgroundColor: Color(0xFFD6D6D6),
        progressColor: Color(0xFF5BFB82),          
    ),

You could try my library ( capped_progress_indicator ) that does what you want and works exactly like Flutter's original LinearProgressIndicator and CircularProgressIndicator .您可以试试我的库 ( capped_progress_indicator ),它可以满足您的需求,并且与 Flutter 的原始LinearProgressIndicatorCircularProgressIndicator完全一样。 It not only rounds the ends of the track/background but also the ends of the progress/indicator.它不仅环绕轨道/背景的末端,还环绕进度/指示器的末端。

So its just a matter of installing the package and changing your code from因此,只需安装 package 并将您的代码从

LinearProgressIndicator(
  // ...
)

to

import 'package:capped_progress_indicator/capped_progress_indicator.dart';

LinearCappedProgressIndicator(
  // ...
)

You can also change the corner radius to your liking您还可以根据自己的喜好更改角半径

LinearCappedProgressIndicator(), // Circle end (default).
LinearCappedProgressIndicator(cornerRadius: 5), // Rounded end.
LinearCappedProgressIndicator(cornerRadius: 0), // Square end.

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

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