[英]How do I temporarily change a text color (and back) in a Flutter widget?
I'm looking for a solution to temporarily change the text color in a Text widget to red for 3 seconds. 我正在寻找一种将Text小部件中的文本颜色临时更改为红色的解决方案,持续3秒钟。 I can currently animate it to red, but how do I change it to back to as it was after this duration? 我目前可以将其设置为红色,但是如何在此持续时间内将其恢复为原来的状态?
Like this... 像这样...
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SO(),
);
}
}
class SO extends StatefulWidget {
@override
_SOState createState() => _SOState();
}
class _SOState extends State<SO> {
static final orgColor = Colors.black;
var currentColor = orgColor;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(child: Text("this is colored",style: TextStyle(color: currentColor,fontSize: 40),)),
floatingActionButton: FloatingActionButton(onPressed: () {
setState(() {
currentColor = Colors.red;
});
Timer(Duration(seconds: 3),(){
setState(() {
currentColor = orgColor;
});
});
},),
);
}
}
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SO(),
);
}
}
class SO extends StatefulWidget {
@override
_SOState createState() => _SOState();
}
class _SOState extends State<SO> {
static final orgColor = Colors.red;
var currentColor = orgColor;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(child: Text("this is colored",style: TextStyle(color: currentColor,fontSize: 40),)),
floatingActionButton: FloatingActionButton(onPressed: () {
setState(() {
currentColor = Colors.blue;
});
Timer(Duration(seconds: 2),(){
setState(() {
currentColor = orgColor;
});
});
},),
);
}
}
Basically setting a timer to wait for some time(2 seconds here) and then reset the state. 基本上将计时器设置为等待一段时间(此处为2秒),然后重置状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.