gpt4 book ai didi

flutter - 如何临时更改 Flutter 小部件中的文本颜色(和变回)?

转载 作者:IT王子 更新时间:2023-10-29 07:15:01 41 4
gpt4 key购买 nike

我正在寻找一种解决方案,将文本小部件中的文本颜色暂时更改为红色 3 秒。我目前可以将它设置为红色,但如何在这段时间后将其更改回原样?

最佳答案

像这样...

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;
});
});
},),
);
}
}

基本上设置一个定时器等待一段时间(这里是2秒)然后重置状态。

关于flutter - 如何临时更改 Flutter 小部件中的文本颜色(和变回)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57650778/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com