Last active
October 23, 2019 13:23
-
-
Save maheshj01/ce50ed70d230de52b844d16697dba8af to your computer and use it in GitHub Desktop.
trying to change theme of app from light<->dark using bloc
This file contains hidden or 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
| nightBloc.dart | |
| import 'dart:async'; | |
| import 'package:rxdart/rxdart.dart'; | |
| class NightBloc { | |
| final nightController = BehaviorSubject<bool>(); | |
| StreamSink<bool> get toggleSink => nightController.sink; | |
| Stream<bool> get toggleStream => nightController.stream ?? false; | |
| NightBloc() { | |
| print("added to stream"); | |
| toggleSink.add(false); | |
| } | |
| void dispose() { | |
| nightController.close(); | |
| } | |
| } | |
| main.dart | |
| import 'package:flutter/material.dart'; | |
| import 'package:myapp/bloc/nIghtmodebloc.dart'; | |
| void main() => runApp(MyApp()); | |
| class MyApp extends StatefulWidget { | |
| @override | |
| _MyAppState createState() => _MyAppState(); | |
| } | |
| class _MyAppState extends State<MyApp> { | |
| final bloc = new NightBloc(); | |
| @override | |
| void initState() { | |
| // TODO: implement initState | |
| super.initState(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return StreamBuilder( | |
| stream: bloc.toggleStream, | |
| builder: (BuildContext context, AsyncSnapshot<bool> snapshot) { | |
| print("State changed ${snapshot.data}"); | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| debugShowCheckedModeBanner: false, | |
| theme: | |
| (snapshot.data ?? true) ? ThemeData.dark() : ThemeData.light(), | |
| home: MyHomePage(title: 'Flutter Demo ${snapshot.data}'), | |
| ); | |
| }); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| MyHomePage({Key key, this.title}) : super(key: key); | |
| final String title; | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| bool data = false; | |
| @override | |
| void initState() { | |
| // TODO: implement initState | |
| // configureSSH(); | |
| super.initState(); | |
| } | |
| final bloc = NightBloc(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| key: Key("ScaffoldKey"), | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| actions: <Widget>[ | |
| Container( | |
| padding: EdgeInsets.only(right: 20), | |
| child: Switch( | |
| value: data, | |
| onChanged: (data) { | |
| bloc.toggleSink.add(data); | |
| print(bloc.toggleStream); | |
| setState(() { | |
| data = data; | |
| }); | |
| }, | |
| )), | |
| IconButton( | |
| icon: Icon(Icons.settings), | |
| onPressed: () { | |
| Navigator.of(context).push( | |
| MaterialPageRoute(builder: (context) => Configuration())); | |
| }, | |
| ) | |
| ], | |
| ), | |
| body: Center( | |
| child:Text("HELLO WORLD"); | |
| )); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment