Last active
July 21, 2022 14:06
-
-
Save marcel-ploch/f6ac4df38b7f5a53bb213bd597b08dae to your computer and use it in GitHub Desktop.
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 'package:flutter/material.dart'; | |
import 'dart:async'; | |
import 'dart:math'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page with Streams'), | |
debugShowCheckedModeBanner: false, | |
); | |
} | |
} | |
//View One | |
class MyHomePage extends StatelessWidget { | |
MyHomePage({Key? key, required this.title}) : super(key: key); | |
String title; | |
void _incrementCounter() { | |
CounterService().incValue(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
actions: [ | |
IconButton( | |
icon: Icon(Icons.adaptive.arrow_forward), | |
onPressed: () { | |
Navigator.of(context).push( | |
MaterialPageRoute(builder: (_) { | |
return SecondPage(title: "SecondPage"); | |
}), | |
); | |
}, | |
), | |
], | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'You have pushed the button this many times:', | |
), | |
StreamBuilder<CounterModel>( | |
stream: CounterService().getStream(), | |
builder: (context, snapshot) { | |
if (!snapshot.hasData && !snapshot.hasError) { | |
return CircularProgressIndicator(); | |
} | |
if (snapshot.hasData) { | |
print("has data"); | |
print(snapshot.data!.counter); | |
var data = snapshot.data!.counter; | |
if (data == null) { | |
return CircularProgressIndicator(); | |
} | |
print(data); | |
return Text( | |
'Data in Stream is $data', | |
style: Theme.of(context).textTheme.headline4, | |
); | |
} else { | |
print("has error second screen"); | |
var error = snapshot.error; | |
return Text('$error'); | |
} | |
}) | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
} | |
//second view | |
class SecondPage extends StatelessWidget { | |
SecondPage({Key? key, required this.title}) : super(key: key); | |
String title; | |
void _incrementCounter() { | |
CounterService().incValue(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'You have pushed the button this many times:', | |
), | |
StreamBuilder<CounterModel>( | |
stream: CounterService().getStream(), | |
builder: (context, snapshot) { | |
if (!snapshot.hasData && !snapshot.hasError) { | |
return CircularProgressIndicator(); | |
} | |
if (snapshot.hasData) { | |
print("has data"); | |
print(snapshot.data!.counter); | |
var data = snapshot.data!.counter; | |
if (data == null) { | |
return CircularProgressIndicator(); | |
} | |
print(data); | |
return Text( | |
'Data in Stream is $data', | |
style: Theme.of(context).textTheme.headline4, | |
); | |
} else { | |
print("has error second screen"); | |
var error = snapshot.error; | |
return Text('$error'); | |
} | |
}) | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), // This trailing comma makes auto-formatting nicer for build methods. | |
); | |
} | |
} | |
//service | |
class CounterModel { | |
CounterModel({int? this.counter}) {} | |
int? counter; | |
} | |
class CounterService { | |
static final CounterService _singleton = CounterService._internal(); | |
static final StreamController<CounterModel> streamController = | |
StreamController<CounterModel>.broadcast(); | |
static final rng = Random(); | |
factory CounterService() { | |
return _singleton; | |
} | |
late CounterModel counter = CounterModel(counter: 0); | |
CounterService._internal() {} | |
Stream<CounterModel> getStream() { | |
Future.delayed(Duration(seconds: 0), () { | |
streamController.add(counter); | |
}); | |
return streamController.stream; | |
} | |
void incValue() async { | |
var randomd = rng.nextBool(); | |
streamController.add(CounterModel()); | |
await Future.delayed(Duration(seconds: 2)); | |
if (randomd) { | |
print("add data"); | |
counter.counter = counter.counter! + 1; | |
streamController.add(counter); | |
} else { | |
streamController.addError("Test Error"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment