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

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter