poller 1.1.3 poller: ^1.1.3 copied to clipboard
A custom poller for Flutter apps to use to periodically poll for information.
import 'package:flutter/material.dart';
import 'package:poller/poller.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Poller Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Poller Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Poller _poller;
@override
void initState() {
super.initState();
_poller = new Poller(
seconds: 1,
callback: () {
setState(() {
_counter += 1;
});
},
logging: true,
);
}
bool _canStart() {
return _poller.isStopped;
}
bool _canStop() {
return !_poller.isStopped;
}
bool _canPause() {
return !_poller.isStopped && !_poller.isPaused;
}
bool _canResume() {
return !_poller.isStopped && _poller.isPaused;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Poller has executed this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
child: Text('Start'),
onPressed: _canStart()
? () {
setState(() {
_poller.start();
});
}
: null,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
child: Text('Stop'),
onPressed: _canStop()
? () {
setState(() {
_poller.stop();
});
}
: null,
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
child: Text('Pause'),
onPressed: _canPause()
? () {
setState(() {
_poller.pause();
});
}
: null,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
child: Text('Resume'),
onPressed: _canResume()
? () {
setState(() {
_poller.resume();
});
}
: null,
),
),
],
),
],
),
),
);
}
}