Skip to content

Instantly share code, notes, and snippets.

@eduardoflorence
Created November 29, 2022 14:13
Show Gist options
  • Save eduardoflorence/24e4a14f446eb32cb2f2d5b26596a05c to your computer and use it in GitHub Desktop.
Save eduardoflorence/24e4a14f446eb32cb2f2d5b26596a05c to your computer and use it in GitHub Desktop.
Flutter StreamBuilder Shared
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: const Text('Page B')),
//backgroundColor: Colors.transparent,
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: StreamBuilder<List<SeqDepEntity>>(
stream: seqDepService.activeSeqDepListStream,
initialData: seqDepService.activeSeqDepList,
builder: ((context, snapshot) {
if (snapshot.hasData) {
return (Text(
'The lenght of last \nSnapshot data is: ${snapshot.data!.length}',
style: const TextStyle(
color: Colors.black, fontSize: 20)));
} else {
return Container();
}
}),
),
),
const SizedBox(height: 50),
const Center(
child: Text(
'Enter items on Stream in page C',
style: TextStyle(color: Colors.black, fontSize: 15),
),
),
IconButton(
icon: const 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: const Text('Page C')),
//backgroundColor: Colors.transparent,
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
StreamBuilder<List<SeqDepEntity>>(
stream: seqDepService.activeSeqDepListStream,
initialData: seqDepService.activeSeqDepList,
builder: ((context, snapshot) {
if (snapshot.hasData) {
return (Text(
'The lenght of last \nSnapshot data is: ${snapshot.data!.length}',
style: const TextStyle(
color: Colors.black, fontSize: 20)));
} else {
return Container();
}
})),
Center(
child: IconButton(
icon: const 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!;
}
final List<SeqDepEntity> _activeListSeqDep = [];
final StreamController<List<SeqDepEntity>> _controller =
StreamController<List<SeqDepEntity>>.broadcast();
@override
List<SeqDepEntity> get activeSeqDepList => _activeListSeqDep;
@override
Stream<List<SeqDepEntity>> get activeSeqDepListStream => _controller.stream;
@override
void addActiveSeqDep(SeqDepEntity seqDep) {
_activeListSeqDep.add(seqDep);
_addInStream(_activeListSeqDep);
}
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