Skip to content

Instantly share code, notes, and snippets.

@bubnenkoff
Created September 26, 2019 12:49
Show Gist options
  • Select an option

  • Save bubnenkoff/b00e79c295d1e81f1d06c601c7b79585 to your computer and use it in GitHub Desktop.

Select an option

Save bubnenkoff/b00e79c295d1e81f1d06c601c7b79585 to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'bloc.dart';
// final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
Stream<int> stream;
StreamController<int> myStreamController = StreamController.broadcast();
void main() {
MyClass myClass = MyClass();
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
.then((_) {
runApp(new MyApp());
});
}
class MyClass {
MyClass() {
stream = Stream<int>.periodic(Duration(seconds: 1), (t) => t + 1).take(1);
myStreamController.addStream(stream).then(
(done) {
Future.delayed(Duration(seconds: 1), () { // wait 2 seconds before Showing HomePage
myStreamController.addStream( Stream.value(42) );
// navigatorKey.currentState.pushNamed("/home");
// Navigator.push(context, route) // But we do not have context in this class!!
});
}
);
// stream.pipe(myStreamController.sink);
}
}
class ScaleRotateRoute extends PageRouteBuilder {
final Widget page;
ScaleRotateRoute({this.page})
: super(
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) =>
page,
transitionDuration: Duration(seconds: 1),
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) =>
ScaleTransition(
scale: Tween<double>(
begin: 0.0,
end: 1.0,
).animate(
CurvedAnimation(
parent: animation,
curve: Curves.fastOutSlowIn,
),
),
child: RotationTransition(
turns: Tween<double>(
begin: 0.0,
end: 1.0,
).animate(
CurvedAnimation(
parent: animation,
curve: Curves.linear,
),
),
child: child,
),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title: "Hello",
routes:
{
'/': (context) => SplashScreen(),
'/home': (context) => HomePage(),
},
// navigatorKey: navigatorKey,
);
}
}
class HomePage extends StatelessWidget {
Bloc _bloc = Bloc();
@override
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(),
body: Container(
child:
StreamBuilder(
stream: _bloc.counter,
initialData: 0,
builder: (BuildContext context, AsyncSnapshot<int> _snapshot)
{
return Column(
children: <Widget>[
Text("Pressed: ${_snapshot.data}"),
RaisedButton(child: Text("inc"), onPressed: () => { _bloc.eventSink.add(IncrementEvent()) },),
RaisedButton(child: Text("dec"), onPressed: () => { _bloc.eventSink.add(DecrementEvent()) },)
],
);
}
)
),
);
}
@override
dispose()
{
super.dispose();
_bloc.dispose();
}
}
class SplashScreen extends StatefulWidget {
@override
SplashScreenState createState() => SplashScreenState();
}
class SplashScreenState extends State<SplashScreen> {
@override
void initState() {
myStreamController.stream.where((d) => d == 42).listen((_) {
// Navigator.pushNamed(context, '/home');
Navigator.push(context, ScaleRotateRoute(page: HomePage()));
});
// listen((data) {
// if (data == 42)
// {
// Navigator.pushNamed(context, '/home');
// }
// });
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Container(
child: DefaultTextStyle(
style: TextStyle(
fontSize: 34,
color: Colors.black54
),
child: StreamBuilder(
stream: myStreamController.stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
switch (snapshot.data) {
case 1:
return Text("${snapshot.data}");
case 2:
return Text("${snapshot.data}");
case 3:
return Text("${snapshot.data}");
default:
return Text("${snapshot.data}");
}
} else
return CircularProgressIndicator();
})
)
),
));
// return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment