Forked from jtlapp/valuelistenerprovider_example.dart
Created
January 11, 2020 15:15
-
-
Save aelshamy/3ae03d8937a4b5e76a9580e23899e4dd to your computer and use it in GitHub Desktop.
ValueListenerProvider example
This file contains 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 CountDown extends ValueNotifier<int> { | |
CountDown(int downFrom) : super(downFrom) { | |
scheduleDecrement(); | |
} | |
void scheduleDecrement() { | |
Future.delayed(Duration(seconds: 1), () { | |
if (--value > 0) scheduleDecrement(); | |
}); | |
} | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
const appTitle = 'ValueListenerProvider Demo'; | |
return MaterialApp( | |
title: appTitle, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: ValueListenableProvider<int>( | |
builder: (context) => CountDown(10), | |
child: Scaffold( | |
appBar: AppBar( | |
title: Text(appTitle), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Consumer<int>( | |
builder: (context, count, child) { | |
if (count > 0) { | |
return Text("T minus $count seconds"); | |
} | |
return Text("Blast off!"); | |
}, | |
), | |
], | |
), | |
), | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment