-
-
Save aelshamy/4468395bce5dd1f0ca133acd35ef19d2 to your computer and use it in GitHub Desktop.
StreamProvider example
This file contains hidden or 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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
const appTitle = 'StreamProvider Demo'; | |
return MaterialApp( | |
title: appTitle, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: StreamProvider<int>( | |
initialData: 0, | |
builder: (context) { | |
// Pretend this is loading data and reporting the percent loaded. | |
return Stream<int> | |
.periodic(Duration(milliseconds: 100), (count) => count + 1) | |
.take(100); | |
}, | |
child: Scaffold( | |
appBar: AppBar( | |
title: Text(appTitle), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Consumer<int>( | |
builder: (context, percentDone, child) { | |
if (percentDone < 100) { | |
return Text("Loading... ($percentDone% done)"); | |
} | |
return Text("Done loading!"); | |
}, | |
), | |
], | |
), | |
), | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment