From 254e59f84e4465c6cd1216b2a4f363fb380e3a5d Mon Sep 17 00:00:00 2001 From: Andreas Fahrecker Date: Thu, 8 Oct 2020 11:43:31 +0200 Subject: [PATCH] Added code in lib/main.dart from prototype Signed-off-by: Andreas Fahrecker --- lib/main.dart | 218 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 152 insertions(+), 66 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 11655b6..eace162 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:percent_indicator/circular_percent_indicator.dart'; +import 'package:percent_indicator/linear_percent_indicator.dart'; +import 'package:shared_preferences/shared_preferences.dart'; void main() { runApp(MyApp()); @@ -9,24 +12,12 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter Demo', + title: 'Time Progress Calculator', theme: ThemeData( - // This is the theme of your application. - // - // Try running your application with "flutter run". You'll see the - // application has a blue toolbar. Then, without quitting the app, try - // changing the primarySwatch below to Colors.green and then invoke - // "hot reload" (press "r" in the console where you ran "flutter run", - // or simply save your changes to "hot reload" in a Flutter IDE). - // Notice that the counter didn't reset back to zero; the application - // is not restarted. primarySwatch: Colors.blue, - // This makes the visual density adapt to the platform that you run - // the app on. For desktop platforms, the controls will be smaller and - // closer together (more dense) than on mobile platforms. visualDensity: VisualDensity.adaptivePlatformDensity, ), - home: MyHomePage(title: 'Flutter Demo Home Page'), + home: MyHomePage(title: 'Time Progress Calculator'), ); } } @@ -34,15 +25,6 @@ class MyApp extends StatelessWidget { class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - final String title; @override @@ -50,68 +32,172 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - int _counter = 0; + final String _startDateKey = "startDate"; + final String _endDateKey = "endDate"; + DateTime startDate = DateTime(2000); + DateTime endDate = DateTime(2100); - void _incrementCounter() { + void _retrieveDates() async { + final SharedPreferences prefs = await SharedPreferences.getInstance(); setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; + startDate = DateTime.fromMillisecondsSinceEpoch( + prefs.getInt(_startDateKey) ?? DateTime(2000).millisecondsSinceEpoch); + endDate = DateTime.fromMillisecondsSinceEpoch( + prefs.getInt(_endDateKey) ?? DateTime(2100).millisecondsSinceEpoch); }); } + void _selectStartDate(BuildContext context) async { + final DateTime picked = await showDatePicker( + context: context, + initialDate: startDate, + firstDate: DateTime(2000), + lastDate: DateTime(2100)); + if (picked != null && picked != startDate) { + final SharedPreferences prefs = await SharedPreferences.getInstance(); + prefs.setInt(_startDateKey, picked.millisecondsSinceEpoch); + setState(() { + startDate = picked; + }); + } + } + + void _selectEndDate(BuildContext context) async { + final DateTime picked = await showDatePicker( + context: context, + initialDate: endDate, + firstDate: DateTime(2000), + lastDate: DateTime(2100)); + if (picked != null && picked != endDate) { + final SharedPreferences prefs = await SharedPreferences.getInstance(); + prefs.setInt(_endDateKey, picked.millisecondsSinceEpoch); + setState(() { + endDate = picked; + }); + } + } + + int _getAmountOfDaysDone() { + return DateTime.now().difference(startDate).inDays; + } + + int _getAmountOfDaysLeft() { + return endDate.difference(DateTime.now()).inDays; + } + + int _getAmountOfAllDays() { + return endDate.difference(startDate).inDays; + } + + double _getPercent() { + final int wholeDuration = _getAmountOfAllDays(); + final int currentDuration = _getAmountOfDaysDone(); + final double onePercent = wholeDuration / 100; + return currentDuration / onePercent / 100; + } + + @override + void initState() { + super.initState(); + _retrieveDates(); + } + @override Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. return Scaffold( appBar: AppBar( - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. title: Text(widget.title), ), - body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. + body: Container( + margin: const EdgeInsets.all(5.0), child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Invoke "debug painting" (press "p" in the console, choose the - // "Toggle Debug Paint" action from the Flutter Inspector in Android - // Studio, or the "Toggle Debug Paint" command in Visual Studio Code) - // to see the wireframe for each widget. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). mainAxisAlignment: MainAxisAlignment.center, children: [ - Text( - 'You have pushed the button this many times:', + Expanded( + flex: 1, + child: Row( + children: [ + Expanded( + flex: 2, + child: Text( + "Start Date:", + textAlign: TextAlign.right, + ), + ), + Expanded( + flex: 2, + child: Text("${startDate.toLocal()}".split(" ")[0]), + ), + Expanded( + flex: 2, + child: RaisedButton( + onPressed: () => _selectStartDate(context), + child: Text("Change"), + color: Colors.lightBlueAccent, + ), + ), + Spacer(flex: 1) + ], + ), ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headline4, + Expanded( + flex: 1, + child: Row( + children: [ + Expanded( + flex: 2, + child: Text( + "End Date:", + textAlign: TextAlign.right, + ), + ), + Expanded( + flex: 2, + child: Text("${endDate.toLocal()}".split(" ")[0]), + ), + Expanded( + flex: 2, + child: RaisedButton( + onPressed: () => _selectEndDate(context), + child: Text("Change"), + color: Colors.lightBlueAccent, + ), + ), + Spacer(flex: 1) + ], + ), + ), + Expanded( + flex: 5, + child: CircularPercentIndicator( + radius: 100, + lineWidth: 10, + percent: _getPercent(), + progressColor: Colors.green, + backgroundColor: Colors.red, + center: Text("${(_getPercent() * 100).floor()} %"), + ), + ), + Expanded( + flex: 1, + child: LinearPercentIndicator( + padding: EdgeInsets.symmetric(horizontal: 15.0), + percent: _getPercent(), + leading: Text("${_getAmountOfDaysDone()} Days"), + center: Text("${(_getPercent() * 100).floor()} %"), + trailing: Text("${_getAmountOfDaysLeft()} Days"), + progressColor: Colors.green, + backgroundColor: Colors.red, + lineHeight: 25, + ), + ), + Expanded( + flex: 1, + child: Text("${_getAmountOfAllDays()} Days"), ), ], ), ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. ); } }