Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何根据百分比指示器更改文本_Flutter_Dart - Fatal编程技术网

Flutter 如何根据百分比指示器更改文本

Flutter 如何根据百分比指示器更改文本,flutter,dart,Flutter,Dart,我有一个百分比指示器,我想在一个文本小部件中显示它的百分比,当它的属性为9.0时,它的百分比类型为(比如90%) 像这样的。正如你所看到的,它的百分比超出了它的区域,它显示了当前的百分比 这是我的指标: child: LinearPercentIndicator( width: MediaQuery.of(context).size.width - 50, animation: true, lineHeig

我有一个百分比指示器,我想在一个文本小部件中显示它的百分比,当它的属性为9.0时,它的百分比类型为(比如90%) 像这样的。正如你所看到的,它的百分比超出了它的区域,它显示了当前的百分比

这是我的指标:

  child: LinearPercentIndicator(
              width: MediaQuery.of(context).size.width - 50,
              animation: true,
              lineHeight: 6.0,
              animationDuration: 2000,
              percent: 0.6,
              linearStrokeCap: LinearStrokeCap.roundAll,
              backgroundColor: Color(0xffD2D9EC),
            ),

我想将百分比:
0.6,
转换为整数,然后在文本小部件中显示它,我想做与此相反的事情。这意味着我要将int转换为double,并用指示符百分比显示它。我想更改一次,比如当用户来到页面时。从API中获取并显示它。

在这里,您使用SeekBar,并在Listner中使用SeekBar Listner,您可以在Textview中更改文本

//SBVolume是seekbar的id

this.sbVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            public void onStartTrackingTouch(SeekBar see`enter code here`kBar) {
            }

            public void onStopTracki`enter code here`ngTouch(SeekBar seekBar) {
            }

            public void onProgressChanged(SeekBar seekBar, int i, boolean z) {
              // in this you can change text
            }
        });
```

下面是一个示例代码,用于将十进制转换为百分比并显示在
Text
小部件上。其思想是将百分比绑定到一个公共值,并将其转换为文本显示。我正在使用一个
有状态小部件
和一个
滑块
来更改百分比值,但您可以始终绑定到任何源,如AOI响应时间或类似的内容

下面是示例代码-

import 'package:flutter/material.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';

void main() {
  //Run the App Widget
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Demo App",
      home: Scaffold(
        appBar: AppBar(
          title: Text("Demo App"),
        ),
        body: Demo(),
      ),
    );
  }
}

class Demo extends StatefulWidget {
  @override
  _DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {
  double _value = 0.0;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '${(_value * 100.0).toInt()}%',
              style: TextStyle(fontSize: 20.0),
            ),
            SizedBox(
              height: 10.0,
            ),
            LinearPercentIndicator(
              alignment: MainAxisAlignment.center,
              width: MediaQuery.of(context).size.width - 50,
              lineHeight: 50.0,
              animationDuration: 2000,
              percent: _value,
              linearStrokeCap: LinearStrokeCap.butt,
              backgroundColor: Color(0xffD2D9EC),
            ),
            SizedBox(
              height: 10.0,
            ),
            Slider(
              min: 0.0,
              max: 1.0,
              value: _value,
              onChanged: (value) => {
                setState(() {
                  _value = value;
                })
              },
            )
          ],
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“包:百分比指示器/线性百分比指示器.dart”;
void main(){
//运行应用程序小部件
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“演示应用程序”,
家:脚手架(
appBar:appBar(
标题:文本(“演示应用程序”),
),
正文:Demo(),
),
);
}
}
类Demo扩展StatefulWidget{
@凌驾
_DemoState createState();
}
类_DemoState扩展了状态{
双_值=0.0;
@凌驾
小部件构建(构建上下文){
返回安全区(
儿童:中心(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“${(_value*100.0).toInt()}%”,
样式:TextStyle(fontSize:20.0),
),
大小盒子(
身高:10.0,
),
线性指示器(
对齐:MainAxisAlignment.center,
宽度:MediaQuery.of(context).size.width-50,
线宽:50.0,
动画持续时间:2000年,
百分比:_值,
linearStrokeCap:linearStrokeCap.butt,
背景颜色:颜色(0xffD2D9EC),
),
大小盒子(
身高:10.0,
),
滑块(
最小值:0.0,
最高:1.0,
值:_值,
一旦更改:(值)=>{
设置状态(){
_价值=价值;
})
},
)
],
),
),
);
}
}
以及输出-


这将为您提供一个如何实现所需解决方案的想法。

您可以发布您迄今为止尝试过的代码吗?,,,,,,,,,,,现在我确实需要再次阅读代码。如果您阅读了标签,它会显示“颤振”和“飞镖”。不感谢你的努力,但奥古斯特·基莫的答案正是我想要的,我同意你的答案
import 'package:flutter/material.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';

void main() {
  //Run the App Widget
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Demo App",
      home: Scaffold(
        appBar: AppBar(
          title: Text("Demo App"),
        ),
        body: Demo(),
      ),
    );
  }
}

class Demo extends StatefulWidget {
  @override
  _DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {
  double _value = 0.0;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '${(_value * 100.0).toInt()}%',
              style: TextStyle(fontSize: 20.0),
            ),
            SizedBox(
              height: 10.0,
            ),
            LinearPercentIndicator(
              alignment: MainAxisAlignment.center,
              width: MediaQuery.of(context).size.width - 50,
              lineHeight: 50.0,
              animationDuration: 2000,
              percent: _value,
              linearStrokeCap: LinearStrokeCap.butt,
              backgroundColor: Color(0xffD2D9EC),
            ),
            SizedBox(
              height: 10.0,
            ),
            Slider(
              min: 0.0,
              max: 1.0,
              value: _value,
              onChanged: (value) => {
                setState(() {
                  _value = value;
                })
              },
            )
          ],
        ),
      ),
    );
  }
}