Flutter_实现日间夜间主题切换

class _DynamicThemeStateState extends State<DynamicThemeState> {
  Brightness _brightness = Brightness.light;//默认主题为日间模式
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        brightness: _brightness,//实现主题的切换
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('如何创建和使用Flutter的路由与导航'),
        ),
        body: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: (){
                setState(() {
                  if(_brightness==Brightness.light){
                    _brightness = Brightness.dark;
                  }else{
                    _brightness = Brightness.light;
                  }
                });
              },
              child: Text('切换主题'),
            ),
            RouteNavigator()
          ],
        )
      ),

Flutter_实现日间夜间主题切换_第1张图片

你可能感兴趣的:(Flutter)