'setState' 如果使用自定义颜色,则无法在导航页面中工作
'setState' not working in navigated page if custom color is used
出于某种原因,每当我使用 flutter 文档中描述的方式导航到另一条路线时,即 https://flutter.dev/docs/cookbook/navigation/navigation-basics,并且如果我按以下方式使用自定义颜色:
color: Color(0xff0e0f26),
在那条路线中, setState
方法在其中不起作用。但是,如果我按以下方式使用颜色:
color: Colors.blue,
setState
方法有效。我不知道是什么原因造成的。我想使用 flutter 提供的颜色中不存在的颜色值。我该如何解决?完整的代码和解释(使用注释)在这里:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'test',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: homepage(),
);
}
}
class homepage extends StatefulWidget{
@override
_homepageState createState() => _homepageState();
}
class _homepageState extends State<homepage>{
@override
Widget build(BuildContext context){
return Scaffold(
body: Container(
alignment: Alignment.center,
color: Color(0xff0e0f26),
child: RaisedButton(
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
),
)
);
}
}
class SecondRoute extends StatefulWidget{
@override
_SecondRouteState createState() => _SecondRouteState();
}
class _SecondRouteState extends State<SecondRoute>{
bool test = false;
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text("Second"),
),
body: Container(
alignment: Alignment.center,
color: Color(0xff0e0f26), //Here, if I use 'color: Colors.blue', setState works.
child:Column(
children: [
RaisedButton(
onPressed: (){ //When the button is pressed, setState is triggered.
setState((){ //This should theoretically rebuild the widget with 'test' becoming true
//, thus showing the text widget below in the screen, but it doesn't.
test = true;
});
},
),
test ? Text("HELLO"):SizedBox(), //I want 'test' to become true, thus making the text
//widget come on screen.
],
),
),
);
}
}
谢谢。
你的文字太暗了。
如果你使用
test ? Text("HELLO", style: TextStyle(color: Colors.white30),):SizedBox(),
应该可以。这只会让你的文字更轻。您应该使用 ElevatedButton 而不是 RaisedButton,因为 RaisedButton 已被弃用并且也可能导致问题。
出于某种原因,每当我使用 flutter 文档中描述的方式导航到另一条路线时,即 https://flutter.dev/docs/cookbook/navigation/navigation-basics,并且如果我按以下方式使用自定义颜色:
color: Color(0xff0e0f26),
在那条路线中, setState
方法在其中不起作用。但是,如果我按以下方式使用颜色:color: Colors.blue,
setState
方法有效。我不知道是什么原因造成的。我想使用 flutter 提供的颜色中不存在的颜色值。我该如何解决?完整的代码和解释(使用注释)在这里:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'test',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: homepage(),
);
}
}
class homepage extends StatefulWidget{
@override
_homepageState createState() => _homepageState();
}
class _homepageState extends State<homepage>{
@override
Widget build(BuildContext context){
return Scaffold(
body: Container(
alignment: Alignment.center,
color: Color(0xff0e0f26),
child: RaisedButton(
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
},
),
)
);
}
}
class SecondRoute extends StatefulWidget{
@override
_SecondRouteState createState() => _SecondRouteState();
}
class _SecondRouteState extends State<SecondRoute>{
bool test = false;
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text("Second"),
),
body: Container(
alignment: Alignment.center,
color: Color(0xff0e0f26), //Here, if I use 'color: Colors.blue', setState works.
child:Column(
children: [
RaisedButton(
onPressed: (){ //When the button is pressed, setState is triggered.
setState((){ //This should theoretically rebuild the widget with 'test' becoming true
//, thus showing the text widget below in the screen, but it doesn't.
test = true;
});
},
),
test ? Text("HELLO"):SizedBox(), //I want 'test' to become true, thus making the text
//widget come on screen.
],
),
),
);
}
}
谢谢。
你的文字太暗了。 如果你使用
test ? Text("HELLO", style: TextStyle(color: Colors.white30),):SizedBox(),
应该可以。这只会让你的文字更轻。您应该使用 ElevatedButton 而不是 RaisedButton,因为 RaisedButton 已被弃用并且也可能导致问题。