Last active
December 1, 2022 10:42
-
-
Save EuHigorBarbosa/1de15101b4161db670c98a5ac2439cbe to your computer and use it in GitHub Desktop.
Stream in several pages
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'; | |
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( | |
routes: { | |
'/pageA': (context) => const PageAA(), | |
'/pageB': (context) => const PageBB(), | |
'/pageC': (context) => const PageCC() | |
}, | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const PageAA(), | |
); | |
} | |
} | |
class PageAA extends StatefulWidget { | |
const PageAA({Key? key}) : super(key: key); | |
@override | |
State<PageAA> createState() => _PageAAState(); | |
} | |
class _PageAAState extends State<PageAA> { | |
@override | |
Widget build(BuildContext context) { | |
return LayoutBuilder(builder: (context, constraints) { | |
return Container( | |
//decoration: BackGroundImage.BackGroundImageBoxDecoration, | |
child: Scaffold( | |
appBar: AppBar(title: const Text('Page A')), | |
//backgroundColor: Colors.transparent, | |
body: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
const Center( | |
child: Text( | |
'Start the program in page C', | |
style: TextStyle( | |
color: Colors.black, | |
fontSize: 20, | |
), | |
), | |
), | |
IconButton( | |
icon: const Icon(Icons.arrow_forward_ios), | |
onPressed: () => Navigator.of(context).pushNamed('/pageB'), | |
) | |
], | |
), | |
), | |
), | |
); | |
}); | |
} | |
} | |
class PageBB extends StatefulWidget { | |
const PageBB({Key? key}) : super(key: key); | |
@override | |
State<PageBB> createState() => _PageBBState(); | |
} | |
class _PageBBState extends State<PageBB> { | |
SeqDepService seqDepService = SeqDepService(); | |
@override | |
Widget build(BuildContext context) { | |
return LayoutBuilder(builder: (context, constraints) { | |
return Container( | |
//decoration: BackGroundImage.BackGroundImageBoxDecoration, | |
child: Scaffold( | |
appBar: AppBar(title: Text('Page B')), | |
//backgroundColor: Colors.transparent, | |
body: Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 18.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Center(child: Text('StreamBuilder with Connection State\n\nWait 4 seconds',textAlign: TextAlign.center, style: TextStyle( color: Colors.red, fontSize: 20))), | |
Center(child: StreamBuilder<List<SeqDepEntity>>( | |
initialData: seqDepService.activeSeqDepList, | |
stream: seqDepService.activeSeqDepListStream, | |
builder: ((context, snapshot) { | |
if(snapshot.connectionState == ConnectionState.none){ | |
return Center(child: (Text(' ${snapshot.connectionState} \n last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15)))); | |
} | |
if(snapshot.connectionState == ConnectionState.waiting){ | |
return Column( | |
children: [ | |
Text(' ${snapshot.connectionState} \n last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15)), | |
CircularProgressIndicator(), | |
], | |
); | |
} | |
if(snapshot.connectionState == ConnectionState.done){ | |
return Center(child: (Text(' ${snapshot.connectionState} \n last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15)))); | |
} | |
if(snapshot.connectionState == ConnectionState.active){ | |
return Center(child: (Text(' ${snapshot.connectionState} \n last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15)))); | |
} | |
return Container(); | |
})),), | |
SizedBox(height: 40,), | |
Center(child: Text('StreamBuilder with hasData and hasError',textAlign: TextAlign.center, style: TextStyle(color: Colors.red, fontSize: 20))), | |
Center(child: StreamBuilder<List<SeqDepEntity>>( | |
initialData: seqDepService.activeSeqDepList, | |
stream: seqDepService.activeSeqDepListStream, | |
builder: ((context, snapshot) { | |
if(snapshot.hasData){ | |
return (Text('last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15))); | |
} if (snapshot.hasError) { | |
return Text('If has error the StreamBuilder show up error'); | |
} | |
else { | |
return Container(); | |
} | |
})),), | |
SizedBox(height: 50,), | |
Center(child: Text('Look the Stream in page C too', style: TextStyle(color: Colors.black, fontSize: 15))), | |
IconButton(icon: Icon(Icons.arrow_forward_ios), onPressed: ()=>Navigator.of(context).pushNamed('/pageC'),) | |
], | |
), | |
), | |
), | |
); | |
}); | |
} | |
} | |
class PageCC extends StatefulWidget { | |
const PageCC({Key? key}) : super(key: key); | |
@override | |
State<PageCC> createState() => _PageCCState(); | |
} | |
class _PageCCState extends State<PageCC> { | |
SeqDepService seqDepService = SeqDepService(); | |
@override | |
Widget build(BuildContext context) { | |
return LayoutBuilder(builder: (context, constraints) { | |
return Container( | |
//decoration: BackGroundImage.BackGroundImageBoxDecoration, | |
child: Scaffold( | |
appBar: AppBar(title: Text('Page C')), | |
//backgroundColor: Colors.transparent, | |
body: Padding( | |
padding: const EdgeInsets.symmetric(horizontal: 18.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
//crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Center(child: Text('StreamBuilder with Connection State\n\nWait 4 seconds',textAlign: TextAlign.center, style: TextStyle(color: Colors.red, fontSize: 20))), | |
Center( | |
child: StreamBuilder<List<SeqDepEntity>>( | |
initialData: (seqDepService.activeSeqDepList.length>0)?seqDepService.activeSeqDepList:[], | |
stream: seqDepService.activeSeqDepListStream, | |
builder: ((context, snapshot) { | |
if(snapshot.connectionState == ConnectionState.none){ | |
return (Text(' ${snapshot.connectionState} \n last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15))); | |
} | |
if(snapshot.connectionState == ConnectionState.waiting){ | |
return Column( | |
children: [ | |
Text(' ${snapshot.connectionState} \n last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15)), | |
CircularProgressIndicator(), | |
], | |
); | |
} | |
if(snapshot.connectionState == ConnectionState.done){ | |
return (Text(' ${snapshot.connectionState} \n last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15))); | |
} | |
if(snapshot.connectionState == ConnectionState.active){ | |
return (Text(' ${snapshot.connectionState} \n last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15))); | |
} | |
return Container(); | |
})), | |
), | |
SizedBox(height: 40,), | |
Center(child: Text('StreamBuilder with hasData and hasError',textAlign: TextAlign.center, style: TextStyle(color: Colors.red, fontSize: 20))), | |
Center(child: StreamBuilder<List<SeqDepEntity>>( | |
initialData: seqDepService.activeSeqDepList, | |
stream: seqDepService.activeSeqDepListStream, | |
builder: ((context, snapshot) { | |
if(snapshot.hasData){ | |
return (Text('last Snapshot data is: ${snapshot.data!.length}', style: TextStyle(color: Colors.black, fontSize: 15))); | |
} if (snapshot.hasError) { | |
return Text('If has error the StreamBuilder show up error'); | |
} | |
else { | |
return Container(); | |
} | |
})),), | |
//Center(child: IconButton(icon: Icon(Icons.add,size: 30, color: Colors.blue,), onPressed: ()=>seqDepService.addActiveSeqDep(SeqDepEntity(id:'unique', content:1)),)) | |
], | |
), | |
), | |
), | |
); | |
}); | |
} | |
} | |
class SeqDepEntity { | |
String id; | |
int content; | |
SeqDepEntity({ | |
required this.id, | |
required this.content, | |
}); | |
} | |
abstract class SeqDepService { | |
List<SeqDepEntity> get activeSeqDepList; | |
Stream<List<SeqDepEntity>> get activeSeqDepListStream; | |
/// Add a single item on stream data. The stream data is a List<SeqDepEntity> | |
//addActiveSeqDep( SeqDepEntity entity); | |
factory SeqDepService() { | |
return SeqDepServiceFFmpeg(); | |
} | |
} | |
class SeqDepServiceFFmpeg implements SeqDepService { | |
static SeqDepServiceFFmpeg? _instance; | |
SeqDepServiceFFmpeg._(); | |
factory SeqDepServiceFFmpeg(){ | |
_instance ??= SeqDepServiceFFmpeg._(); | |
return _instance!; | |
} | |
static List<SeqDepEntity> _activeListSeqDep = []; | |
static var timer = Timer(Duration(seconds: 0), (){}); | |
static MultiStreamController<List<SeqDepEntity>>? _multiController; | |
static final StreamController<List<SeqDepEntity>> _controller = StreamController<List<SeqDepEntity>>.broadcast( | |
onListen: () async{ | |
for(int i = 0; i<5; i++){ | |
await Future.delayed(Duration(seconds: 4)); | |
_activeListSeqDep.add(SeqDepEntity(content: i, id: 'id'+i.toString())); | |
_addInStream(_activeListSeqDep); | |
} | |
} | |
); | |
@override | |
List<SeqDepEntity> get activeSeqDepList => _activeListSeqDep; | |
@override | |
Stream<List<SeqDepEntity>> get activeSeqDepListStream => _controller.stream;//_activeMultiSeqDepStream;// | |
// @override | |
// void addActiveSeqDep( SeqDepEntity seqDep) { | |
// _activeListSeqDep.add(seqDep); | |
// _addInStream(_activeListSeqDep); | |
// } | |
static void _addInStream (List<SeqDepEntity> activeEntities){ | |
_controller.sink.add(activeEntities); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment