StreamController StreamBuilder

 import "dart:async";

import "package:flutter/material.dart";

void main()
{
runApp(const MyApp());
}
class MyApp extends StatelessWidget
{
const MyApp({super.key});

@override
Widget build(BuildContext context)
{
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget
{
const HomePage({super.key});

@override
HomePageState createState()
{
return HomePageState();
}
}

class HomePageState extends State<HomePage>
{
int counter = 0;
StreamController<int> counterController = StreamController<int>();

@override
Widget build(BuildContext context)
{
return Scaffold(
body: SafeArea(
child: Center(
child: StreamBuilder(
stream: counterController.stream,
builder: (context, snapshot) {
if(snapshot.hasData) {
return Text(snapshot.data.toString(), style: const TextStyle(
fontSize: 70,
fontWeight: FontWeight.bold,
),);
}
else {
return const Text("0", style: TextStyle(
fontSize: 70,
fontWeight: FontWeight.bold,
),);
}
}
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: (){
counter++;
counterController.sink.add(counter);
},
child: const Icon(Icons.add),
),
);
}
}

Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

Pagination First Practical in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter