简体   繁体   English

如何在 flutter 中链接线性进度指示器和 slider?

[英]How to link linear progress indicator and slider in flutter?

I am making a music player where the progress indicator will act as a slider to change the music position and also as a progress bar.我正在制作一个音乐播放器,其中进度指示器将充当 slider 以更改音乐 position 以及进度条。

As far as I understood the slider can only be set manually whereas the linearprogess indicator has no such slider.据我了解,slider 只能手动设置,而线性进程指示器没有这样的 slider。
I want a way to integrate both such that the slider shows the current progress of the music track as well as the user can drag it to affect the value.我想要一种集成两者的方法,以便 slider 显示音乐曲目的当前进度,并且用户可以拖动它来影响值。

Is there any way to do so?有什么办法吗? Or if I have any misconception please correct me.或者如果我有任何误解,请纠正我。

THANK YOU谢谢你

You can create a variable that holds the value for the slider and as you slide, you update this variable and also set the variable to the progress indicator.您可以创建一个变量来保存slider的值,并在滑动时更新此变量并将变量设置为进度指示器。

Like this:像这样:

class LinearSlider extends StatefulWidget {
  @override
  _LinearSliderState createState() => _LinearSliderState();
}

class _LinearSliderState extends State<LinearSlider> {
  double sliderValue = 0.3;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          Slider(
            value: sliderValue,
            onChanged: (v){
              setState(() {
                sliderValue = v;
              });
            },
          ),

          LinearProgressIndicator(
            value: sliderValue,
          ),
        ],
      ),
    );
  }
}

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

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