如何更改CircularProgressIndicator的颜色?
颜色的值是Animation< color >的实例,但我希望有一种更简单的方法来改变颜色,没有动画的麻烦。
如何更改CircularProgressIndicator的颜色?
颜色的值是Animation< color >的实例,但我希望有一种更简单的方法来改变颜色,没有动画的麻烦。
主题是可以插入到小部件树中的任何位置的小部件。 它用自定义值覆盖当前主题 试试这个:
new Theme(
data: Theme.of(context).copyWith(accentColor: Colors.yellow),
child: new CircularProgressIndicator(),
);
参考:https://gitter.im/flutter/flutter?at=5a84cf9218f388e626a51c2d
这招对我很管用:
CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.white))
在主。dart设置主题为accentColor, CircularProgressIndicator将使用该颜色
void main() => runApp(new MaterialApp(
theme: ThemeData(primaryColor: Colors.red, **accentColor: Colors.yellowAccent**),
debugShowCheckedModeBanner: false,
home: SplashPage()
));
可以使用accentColor作为widget的前景色。它可以改变任何前景小部件的颜色,包括circularprogressbar。
void main() => runApp(
MaterialApp(
title: 'Demo App',
home: MainClass(),
theme: ThemeData(accentColor: Colors.black),
),
);
默认情况下,它从Themedata继承了accentColor
void main() => runApp(new MaterialApp(
theme: ThemeData(
primaryColor: Colors.blue,
accentColor: Colors.blueAccent,
//This will be the color for CircularProgressIndicator color
),
home: Homepage()
));
你可以用你的新颜色改变这个accentColor属性。 另一种方法是像这样使用预定义的主题数据
void main() => runApp(new MaterialApp(
theme: ThemeData.light().copyWith(
accentColor: Colors.blueAccent,
//change the color for CircularProgressIndicator color here
),
home: Homepage()
));
或者,您可以直接在CircularProgressIndicator中更改此颜色属性,如下所示
CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(Colors.red),
),
解决问题的三种方法
1)使用valueColor属性
CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(Colors.blue),
),
2)在MaterialApp主部件中设置accentColor。 这是最好的方法,因为当你使用CircularProgressIndicator小部件时,你不想一直设置颜色
MaterialApp(
title: 'My App',
home: MainPAge(),
theme: ThemeData(accentColor: Colors.blue),
),
3)使用主题小部件
Theme(
data: Theme.of(context).copyWith(colorScheme: ColorScheme(
primary: Colors.red,
// You should set other properties too
)),
child: new CircularProgressIndicator(),
)
对于单色集合,
CircularProgressIndicator(
valueColor:AlwaysStoppedAnimation<Color>(Colors.red),
);
适用于多种颜色变换/设置。
class MyApp extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyApp> with TickerProviderStateMixin {
AnimationController animationController;
@override
void dispose() {
// TODO: implement dispose
super.dispose();
animationController.dispose();
}
@override
void initState() {
super.initState();
animationController =
AnimationController(duration: new Duration(seconds: 2), vsync: this);
animationController.repeat();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Color Change CircularProgressIndicator"),
),
body: Center(
child: CircularProgressIndicator(
valueColor: animationController
.drive(ColorTween(begin: Colors.blueAccent, end: Colors.red)),
),
),
);
}
}
像这样使用——>
CircularProgressIndicator (valueColor: AlwaysStoppedAnimation (Colors.grey [500]),)),
backgroundColor设置浅色,它看到像浅色背景色的圆,valueColor它是加载颜色,它将显示编译加载圆超过灰色
CircularProgressIndicator(
backgroundColor: Colors.gray,
valueColor: AlwaysStoppedAnimation<Color>(Colors.black)
)
<com.google.android.material.progressindicator.CircularProgressIndicator app:indicatorColor="@color/primaryColor" />
如果你想全局更改,在最新版本的flutter中,你应该更改colorScheme:
void main() => runApp(
MaterialApp(
title: 'App',
home: Home(),
theme: ThemeData(
colorScheme: ColorScheme(
primary: Colors.red,
// You should set other properties too
)
),
),
);
CircularProgressIndicator(
backgroundColor: Colors.amberAccent,
semanticsLabel: 'Linear progress indicator',
),
accentColor已弃用,不再有效。
要在ThemeData中全局设置它,像这样设置:
光的主题:
theme: ThemeData(
colorScheme: ColorScheme.dark(
primary: Colors.pink,
),
),
黑暗的主题:
theme: ThemeData(
colorScheme: ColorScheme(
primary: Colors.pink,
),
),
本地:
或者如果你想要它只用于本地的一个小部件,只需要像这样设置CircularProgressIndicator的属性:
CircularProgressIndicator(
backgroundColor:Colors.white,
valueColor: AlwaysStoppedAnimation<Color>(Colors.pink),
),
只需将此代码写入应用程序的主题数据
ThemeData(
progressIndicatorTheme: ProgressIndicatorThemeData(
color: Colors.grey.shade700,),)
使用progressIndicatorTheme允许为进度指示器定义一个主题。
ThemeData(
progressIndicatorTheme: ProgressIndicatorThemeData(color: Colors.white),
)
试试这个:
CircularProgressIndicator(
color: Colors.yellow, // Change your color here
),