一旦构建了屏幕,然后在某种逻辑之后以某种方式我需要更改 AppBar 标题而不再次执行 build() 方法。这该怎么做?
您可以在 StatefulWidget 中调用 setState() 方法并更改 AppBar 的标题。你可以这样做:
class HomePage extends StatefulWidget {
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String appBarTitle = "Title1";
void changeTitle(){
setState(() {
appBarTitle = appBarTitle=="Title1"?"Title2":"Title1";
});
}
@override
Widget build(BuildContext context) {
return Container(
child: Scaffold(
appBar: AppBar(
title: Text(appBarTitle),
),
floatingActionButton: FloatingActionButton(
onPressed: changeTitle,
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2668 次 |
| 最近记录: |