Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aelshamy/3ae03d8937a4b5e76a9580e23899e4dd to your computer and use it in GitHub Desktop.
Save aelshamy/3ae03d8937a4b5e76a9580e23899e4dd to your computer and use it in GitHub Desktop.
ValueListenerProvider example
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