Created
September 19, 2019 04:23
-
-
Save jtlapp/3667b641e39e3c7de29b2e8cd562ac1c 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