Last active
May 6, 2020 11:33
-
-
Save erluxman/96008818aaabd0ede2721d6151540f84 to your computer and use it in GitHub Desktop.
flutter dynamic theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
StreamController<bool> isLightTheme = StreamController.broadcast(); | |
main() { | |
runApp(MainApp()); | |
} | |
class MainApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return StreamBuilder<bool>( | |
initialData: true, | |
stream: isLightTheme.stream, | |
builder: (context, snapshot) { | |
return MaterialApp( | |
theme: snapshot.data ? ThemeData.light() : ThemeData.dark(), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
appBar: AppBar(title: Text("Dynamic Theme")), | |
body: SettingPage())); | |
}); | |
} | |
} | |
class SettingPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Center( | |
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: < | |
Widget>[ | |
RaisedButton( | |
color: Colors.blue, | |
child: Text("Light Theme", style: TextStyle(color: Colors.white)), | |
onPressed: () { | |
isLightTheme.add(true); | |
}), | |
RaisedButton( | |
color: Colors.black, | |
child: Text("Dark Theme", style: TextStyle(color: Colors.white)), | |
onPressed: () { | |
isLightTheme.add(false); | |
}), | |
]))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment