Skip to content

Instantly share code, notes, and snippets.

@Alvarocda
Created December 15, 2021 14:02
Show Gist options
  • Save Alvarocda/140bbafb7eb24441ba9612a8ccf068ad to your computer and use it in GitHub Desktop.
Save Alvarocda/140bbafb7eb24441ba9612a8ccf068ad to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
///
///
///
class ExampleScreen extends StatefulWidget {
///
///
///
const ExampleScreen({Key? key}) : super(key: key);
///
///
///
@override
_ExampleScreenState createState() => _ExampleScreenState();
}
///
///
///
class _ExampleScreenState extends State<ExampleScreen> {
final StreamController<int?> _appBarCountStreamController =
StreamController<int?>();
final StreamController<List<String>> theStream =
StreamController<List<String>>();
@override
void initState() {
super.initState();
_simulatingLoading();
}
///
///
///
Future<void> _simulatingLoading() async {
await Future<void>.delayed(Duration(seconds: 5));
List<String> strings = <String>['A', 'B', 'C', 'D', 'E', 'F'];
theStream.add(strings);
}
///
///
///
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: StreamBuilder<int?>(
stream: _appBarCountStreamController.stream,
builder: (BuildContext context, AsyncSnapshot<int?> snapshot) {
if (snapshot.data == null) {
return Wrap(
children: <Widget>[
Text('Loading'),
CupertinoActivityIndicator()
],
);
} else {
return Text('Total :${snapshot.data}');
}
},
),
),
body: StreamBuilder<List<String>>(
stream: theStream.stream,
builder: (BuildContext context, AsyncSnapshot<List<String>> snapshot) {
if (snapshot.hasData) {
_appBarCountStreamController.add(snapshot.data!.length);
return ListView.builder(
itemCount: snapshot.requireData.length,
itemBuilder: (BuildContext context, int index) =>
Text(snapshot.data![index]),
);
}
return Wrap(
children: <Widget>[Text('Loading'), CupertinoActivityIndicator()],
);
},
),
);
}
///
///
///
@override
void dispose() {
_appBarCountStreamController.close();
theStream.close();
super.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment