Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class NewStopWatch extends StatefulWidget {
- @override
- _NewStopWatchState createState() => new _NewStopWatchState();
- }
- class _NewStopWatchState extends State<NewStopWatch> {
- static _NewStopWatchState stopwatch;
- Stopwatch watch = new Stopwatch();
- Timer timer;
- bool startStop = true;
- static String elapsedTime = '';
- String duration;
- updateTime(Timer timer) {
- if (watch.isRunning) {
- setState(() {
- elapsedTime = transformMilliSeconds(watch.elapsedMilliseconds);
- User.getCurrentUser().getCurrentActivity().setElapsedTime(elapsedTime);
- });
- }
- }
- @override
- Widget build(BuildContext context) {
- return new Container(
- padding: EdgeInsets.all(20.0),
- child: new Column(
- children: <Widget>[
- new Text(elapsedTime, style: new TextStyle(fontSize: 25.0)),
- SizedBox(height: 20.0),
- new Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: <Widget>[
- FloatingActionButton(
- heroTag: "btn1",
- backgroundColor: Colors.red,
- onPressed: () => startOrStop(),
- child: Icon(Icons.pause)),
- SizedBox(width: 20.0),
- FloatingActionButton(
- heroTag: "btn2",
- backgroundColor: Colors.green,
- onPressed: () => completeActivity(),
- child: Icon(Icons.check)),
- ],
- )
- ],
- ));
- }
- void initState() {
- super.initState();
- WidgetsBinding.instance
- .addPostFrameCallback((_) => startWatch());
- }
- startOrStop() {
- if(startStop) {
- startWatch();
- } else {
- stopWatch();
- }
- }
- startWatch() {
- setState(() {
- startStop = false;
- watch.start();
- timer = Timer.periodic(Duration(milliseconds: 100), updateTime);
- });
- }
- stopWatch() {
- setState(() {
- startStop = true;
- watch.stop();
- setTime();
- });
- }
- resetWatch() {
- watch.reset();
- setTime();
- }
- setTime() {
- var timeSoFar = watch.elapsedMilliseconds;
- setState(() {
- elapsedTime = transformMilliSeconds(timeSoFar);
- });
- }
- completeActivity() { //do I call activity.stopActivity()?
- return showDialog(
- context: context,
- builder: (context) => new AlertDialog(
- title: new Text('Complete Activity?',
- style: new TextStyle(color: Colors.black, fontSize: 20.0)),
- actions: <Widget>[
- new FlatButton(
- onPressed: () {
- duration = elapsedTime;
- print("Current activitiy: ${User.getCurrentUser().getCurrentActivity()}");
- // User.getCurrentUser().completeActivity();
- User.getCurrentUser().addPastActivity(User.getCurrentUser().getCurrentActivity());
- User.getCurrentUser().getCurrentActivity().setStatus(ActivityStatus.completed);
- User.getCurrentUser().setCurrentActivity(null);
- Navigator.push(context, MaterialPageRoute(builder: (context) => FrontPage()));
- // Navigator.popUntil(context, ModalRoute.withName("/"),);
- },
- child:
- new Text('Yes', style: new TextStyle(fontSize: 18.0)),
- ),
- new FlatButton(
- onPressed: () => Navigator.pop(context), // this line dismisses the dialog
- child: new Text('No', style: new TextStyle(fontSize: 18.0)),
- )
- ],
- ),
- ) ??
- false;
- }
- transformMilliSeconds(int milliseconds) {
- int hundreds = (milliseconds / 10).truncate();
- int seconds = (hundreds / 100).truncate();
- int minutes = (seconds / 60).truncate();
- int hours = (minutes / 60).truncate();
- String hoursStr = (hours % 60).toString().padLeft(2, '0');
- String minutesStr = (minutes % 60).toString().padLeft(2, '0');
- String secondsStr = (seconds % 60).toString().padLeft(2, '0');
- return "$hoursStr:$minutesStr:$secondsStr";
- }
- static String getElapsedTime() {
- return elapsedTime;
- }
- }
- class FrontPage extends StatelessWidget {
- Activity currentActivity;
- var recentActivities = <Widget>[];//<Widget>[Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary(),Activity("Skiing", "assets/activity_logos/Squat.png").getSummary()];
- var upcomingActivities = <Widget>[];//<Widget>[Activity("Jog", "assets/activity_logos/Squat.png", status: ActivityStatus.completed,).getSummary()];
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- centerTitle: true,
- title: Text('Home'),
- actions: <Widget>[
- GestureDetector(
- child: Align(child:Text("+", style: TextStyle(fontSize: 40)), alignment: Alignment.centerLeft, widthFactor: 2,),
- onTap: () => Navigator.pushNamed(context, '/activity/new'),
- )
- ],
- ),
- drawer: NavigationDrawer(),
- body: Center(
- child: Padding(
- padding: const EdgeInsets.only(left: 30.0, right: 30.0, top: 30),
- child:ListView(
- children: <Widget>[
- Text('Current Activity:', style: TextStyle(fontSize: 15 ,height: 2, decoration: TextDecoration.underline)),
- _returnCurrentActivity(context) ,
- Text('Upcoming Activities:', style: TextStyle(fontSize: 15 ,height: 2, decoration: TextDecoration.underline)),
- _returnUpcomingActivities(),
- Text('Recent Activities:', style: TextStyle(fontSize: 15 ,height: 2, decoration: TextDecoration.underline)),
- _returnRecentActivities()
- ],
- ),
- ),
- ),
- );}
- class FrontPage extends StatelessWidget {
- Stopwatch watch = Stopwatch();
- Timer timer = Timer.periodic(Duration(milliseconds: 100), updateTime);
- //... code
- //.. somewhere a NewStopWatch is created
- return NewStopWatch(watch, timer);
- }
- //... code
- class NewStopWatch extends StatefulWidget {
- Stopwatch watch;
- Timer timer;
- NewStopWatch(this.watch, this.timer);
- @override
- _NewStopWatchState createState() => new _NewStopWatchState();
- }
- class _NewStopWatchState extends State<NewStopWatch> {
- static _NewStopWatchState stopwatch;
- bool startStop = true;
- static String elapsedTime = '';
- String duration;
- Stopwatch get watch => widget.watch;
- Timer get timer => widget.timer;
- void initState() {
- super.initState();
- WidgetsBinding.instance
- .addPostFrameCallback((_) => startWatch());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement