Vip*_*bey 3 dart flutter flutter-layout
我是 flutter 应用程序开发的新手,遇到了一个问题。我的应用程序包含大约 5-6 个屏幕,所有屏幕都包含像这样的脚手架小部件。
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFF332F43)
);
}
Run Code Online (Sandbox Code Playgroud)
现在在所有屏幕上我都有相同的概念和设计,并且所有屏幕将共享相同的背景颜色。现在我在所有屏幕上都有一个按钮,即“更改主题”按钮,然后单击我想要更改的“更改主题”按钮所有屏幕支架小部件都要更改。现在我怎样才能实现这一点?请帮助我解决我的问题。
Color color = Colors.blue; // make it at root level
void main() {
runApp(MaterialApp(home: Page1()));
}
Run Code Online (Sandbox Code Playgroud)
在您的 page1 类中,导入上述文件。
class Page1 extends StatefulWidget {
@override
_Page1State createState() => _Page1State();
}
class _Page1State extends State<Page1> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: color,
appBar: AppBar(title: Text("Page 1")),
body: Center(
child: Column(
children: <Widget>[
RaisedButton(
onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (c) => Page2())),
child: Text("Go to Page 2"),
),
RaisedButton(
child: Text("Change color"),
onPressed: () => setState(() => color = Colors.red),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
在 page2 类中,导入第一个文件。
class Page2 extends StatefulWidget {
@override
_Page2State createState() => _Page2State();
}
class _Page2State extends State<Page2> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: color,
appBar: AppBar(title: Text("Page 2")),
body: Center(
child: Column(
children: <Widget>[
RaisedButton(
onPressed: () => Navigator.pop(context),
child: Text("Back"),
),
RaisedButton(
child: Text("Change color"),
onPressed: () => setState(() => color = Colors.green),
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
Flutter 已经预定义了在应用程序中更改脚手架背景颜色的方法。
只需在 main.dart(主文件)内的MaterialApp Widget中更改它即可。
MaterialApp(
title: 'Flutter',
theme: ThemeData(
scaffoldBackgroundColor: const Color(0xFF332F43),
),
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13652 次 |
| 最近记录: |