[英]Why is my CircularProgressIndicator spinning randomly when the screen loads?
I have a CircularProgressIndicator that is meant to display progress when the user presses a button.我有一个 CircularProgressIndicator,用于在用户按下按钮时显示进度。 However, when I open the screen that uses this class it shows a loading animation and I'm not sure why.
但是,当我打开使用此类的屏幕时,它会显示加载动画,我不知道为什么。 When the screen opens, it shouldn't show the animation until the user presses the button.
当屏幕打开时,它不应该在用户按下按钮之前显示动画。
When the screen opens you see something similar to the animation on this page: https:\/\/www.c-sharpcorner.com\/article\/create-chrome-like-loading-animation\/<\/a>当屏幕打开时,您会在此页面上看到类似于动画的内容:
https<\/a> :\/\/www.c-sharpcorner.com\/article\/create-chrome-like-loading-animation\/
class _MsgInputState extends State<MsgInput> with TickerProviderStateMixin {
AnimationController? controller;
@override
void initState() {
super.initState();
initialize();
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
void initialize() async {
controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 3),
)..addListener(() {
setState(() {});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Align(
alignment: Alignment.bottomLeft,
child: Column(
children: [
Row(
children: [
GestureDetector(
onTap: () async {
controller?.forward();
},
onLongPressDown: (details) async {
controller?.forward();
},
onLongPressUp: () async {
controller?.stop();
controller?.reset();
},
child: Text('Record'),
),
],
),
Row(
children: <Widget>[
SizedBox(width: 100),
CircularProgressIndicator(
value: controller?.value,
semanticsLabel: 'Linear progress indicator',
),
],
),
],
),
)
);
}
If the value provided to a CircularProgressIndicator<\/code> is
null<\/code> it just defaults to a spinnging animation.
如果提供给
CircularProgressIndicator<\/code>的值为
null<\/code> ,则它默认为旋转动画。
In the beginnin
controller?.value<\/code> probably returns
null<\/code> .
在 beginnin
controller?.value<\/code>可能返回
null<\/code> 。
Change it to
value: controller?.value ?? 0<\/code>
将其更改为
value: controller?.value ?? 0<\/code>
value: controller?.value ?? 0<\/code> so that while it's
null<\/code> 0 is used as a value.
value: controller?.value ?? 0<\/code>以便当它为
null<\/code>时 0 用作值。
"
Here is a quick examples on how the provided example could be achieved:以下是有关如何实现所提供示例的快速示例:
class StateProgress extends StatelessWidget {
const StateProgress({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Center(
child: SizedBox(
height: 50,
width: 50,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor: AlwaysStoppedAnimation<Color>(Colors.blueAccent /*Should be the color of the indicator*/),
backgroundColor: Colors.white, // Could be made to match the color of the background the indicator is on
),
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.