Countdown timer in flutter - flutter

I want to make a flutter countdown timer to display remaining registration time (DD-HH-MM).
Example:
What would be the most efficient way of accomplishing this?
Thank you

Change below code as your design and requirement
DateTime startTime = DateTime(2020,03,04);
Duration remaining = DateTime.now().difference(DateTime.now());
Timer t;
int days=0,hrs =0, mins=0;
#override
void initState(){
super.initState();
startTimer();
}
startTimer()async{
t = Timer.periodic(Duration(seconds:1),(timer){
setState((){
remaining = DateTime.now().difference(startTime);
mins = remaining.inMinutes;
hrs = mins>=60 ? mins~/60:0;
days = hrs>=24 ? hrs~/24: 0;
hrs = hrs%24;
mins = mins%60;
});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Registration closes in',
style: TextStyle(color: Colors.black, fontSize: 20),
textAlign: TextAlign.center),
),
Row(
mainAxisAlignment:MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('$days',
style: TextStyle(color: Colors.black, fontSize: 20),
textAlign:TextAlign.center,),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('$hrs',
style: TextStyle(color: Colors.black, fontSize: 20),
textAlign:TextAlign.center,),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('$mins',
style: TextStyle(color: Colors.black, fontSize: 20),
textAlign:TextAlign.center,),
),
),
]),
Row(
mainAxisAlignment:MainAxisAlignment.spaceEvenly,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Days',
style: TextStyle(color: Colors.black, fontSize: 16),
textAlign:TextAlign.center,),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Hours',
style: TextStyle(color: Colors.black, fontSize: 16),
textAlign:TextAlign.center,),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Minutes',
style: TextStyle(color: Colors.black, fontSize: 16),
textAlign:TextAlign.center,),
),
),
]),
],
)));
}

Related

Widgets in tabbarview take extra bottom spacing.How to justify their heights according to content?

I'm having two tabs in my tabbar and card widget which makes up both of tabbarviews. I am using Slivers in this case.CustomScrollView is my parent widget. I am displaying Texts in these views but they take extra empty spacing from bottom which I want to get rid of.If these widgets are placed outside tabbarview they work fine and justify height according to their content.The last widget I've added outside of tabbarview and as you can see it works fine.
Here is my code:
import 'package:flutter/material.dart';
class TabviewIssue extends StatefulWidget {
#override
_TabviewIssueState createState() => _TabviewIssueState();
}
class _TabviewIssueState extends State<TabviewIssue>with TickerProviderStateMixin {
TabController _tabController;
#override
void initState() {
super.initState();
this._tabController = TabController(length: 2, vsync: this);
}
String htmlText = 'So vitamins and nutrients are optimally preserved, flavors unfold particularly well. In order to keep the cat food fresh, a special flavor protection foil in the packaging ensures that the oxygen content in the pack is reduced, oxidation is inhibited and the flavors in the cat food are preserved. True love Landlust cat food has exactly the energy content that your cat needs. It provides optimal nutrients and is very digestible. Cat food from True Love is healthy food that tastes cats.';
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(child: SizedBox(height: 50,)),
SliverToBoxAdapter(
child: Container(
color: Colors.grey.shade100,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: TabBar(
indicator: BoxDecoration(color: Colors.grey.shade400),
labelColor: Colors.white,
unselectedLabelColor: Colors.black,
indicatorColor: Colors.grey,
tabs: <Widget>[
Tab(
child: Text("Product Features",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500))),
Tab(
child: Text("Reviews",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500))),
],
controller: this._tabController,
),
)),
),
SliverFillRemaining(
child: TabBarView(
controller: _tabController,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
color: Colors.grey.shade200,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children: <Widget>[
Text("Features",
style: TextStyle(
fontSize: 17, fontWeight: FontWeight.bold)),
Text( htmlText),
]),
)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
color: Colors.grey.shade200,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children: <Widget>[
Text("Features",
style: TextStyle(
fontSize: 17, fontWeight: FontWeight.bold)),
Text( htmlText),
]),
)),
),
]),
),
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
color: Colors.grey.shade200,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children: <Widget>[
Text("Features",
style: TextStyle(
fontSize: 17, fontWeight: FontWeight.bold)),
Text( htmlText),
]),
)),
),
)
],
),
),
);
}
}
class TabviewIssue extends StatefulWidget {
#override
_TabviewIssueState createState() => _TabviewIssueState();
}
class _TabviewIssueState extends State<TabviewIssue>
with TickerProviderStateMixin {
TabController _tabController;
#override
void initState() {
super.initState();
this._tabController = TabController(length: 2, vsync: this);
}
String htmlText =
'So vitamins and nutrients are optimally preserved, flavors unfold particularly well. In order to keep the cat food fresh, a special flavor protection foil in the packaging ensures that the oxygen content in the pack is reduced, oxidation is inhibited and the flavors in the cat food are preserved. True love Landlust cat food has exactly the energy content that your cat needs. It provides optimal nutrients and is very digestible. Cat food from True Love is healthy food that tastes cats.';
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(
child: SizedBox(
height: 50,
)),
SliverToBoxAdapter(
child: Container(
color: Colors.grey.shade100,
child: Padding(
padding: const EdgeInsets.all(10.0),
child: TabBar(
indicator: BoxDecoration(color: Colors.grey.shade400),
labelColor: Colors.white,
unselectedLabelColor: Colors.black,
indicatorColor: Colors.grey,
tabs: <Widget>[
Tab(
child: Text("Product Features",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500))),
Tab(
child: Text("Reviews",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.w500))),
],
controller: this._tabController,
),
)),
),
SliverFillRemaining(
child: TabBarView(controller: _tabController, children: <Widget>[
//TAB 1
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ListView(
shrinkWrap: true,
children: <Widget>[
Card(
color: Colors.grey.shade200,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children: <Widget>[
Text("Features",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.bold)),
Text(htmlText),
]),
)),
Card(
color: Colors.grey.shade200,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children: <Widget>[
Text("Features",
style: TextStyle(
fontSize: 17,
fontWeight: FontWeight.bold)),
Text(htmlText + htmlText),
]),
)),
],
),
Expanded(
child: Container(
child: Text('All remaining space'),
color: Colors.red,
),
)
],
),
//TAB 2
ListView(
children: <Widget>[
Card(
color: Colors.grey.shade200,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children: <Widget>[
Text("Features",
style: TextStyle(
fontSize: 17, fontWeight: FontWeight.bold)),
Text(htmlText),
]),
)),
Card(
color: Colors.grey.shade200,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(children: <Widget>[
Text("Features",
style: TextStyle(
fontSize: 17, fontWeight: FontWeight.bold)),
Text(htmlText + htmlText),
]),
)),
],
),
]),
),
],
),
),
);
}
}

flutter Error when using color swatch inside PopupMenuItem

I have the following code:
class _StandardCardState extends State<StandardCard> {
#override
Widget build(BuildContext context) {
return Container(
child: Card(
margin: EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
children: <Widget>[
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
children: <Widget>[
Text(
'L*: ',
style: TextStyle(
fontSize: 18.0, color: Colors.grey[600]),
),
Text(
widget.standard.L.toString(),
style: TextStyle(
fontSize: 18.0, color: Colors.grey[600]),
),
],
),
SizedBox(height: 6.0),
Row(
children: <Widget>[
//some children
],
),
SizedBox(height: 6.0),
Row(
children: <Widget>[
//some children
],
),
],
),
),
PopupMenuButton<WhyFarther>(
onSelected: (WhyFarther result) {
setState(() {
var _selection = result;
});
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<WhyFarther>>[
const PopupMenuItem<WhyFarther>(
value: WhyFarther.harder,
child: Text(
'Working a lot harder',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.smarter,
child: Text(
'Being a lot smarter',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.selfStarter,
child: Text(
'Being a self-starter',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.tradingCharter,
child: Text(
'Placed in charge of trading charter',
style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
),
),
],
)
],
),
),
),
);
}
}
which is then still called through another function by a ListView.builder. when I try to use Swatches to change the Text Style inside a popupMenuItem, I get the error ´'Invalid constant value'´.
Why does this throw an error while the text widget above doesn't? And how can I avoid that and how do I use swatches inside PopupMenuButtons then?
i only had to remove the ´const´ before the PopupMenuItem... Sometimes reading when copycoding helps a lot.

How to avoid code repetition for multiple Cards in a ListView

I have a ListView which consists of 3 Cards. Each Card has a Text and different fontSize. How can I write a single code for one Card and call it in the ListView for the remaining 2 Cards with different Text and fontSize.
This will help me save extra lines of Code. The code below
Widget homeBody = Container(
child: ListView(
children: <Widget>[
SizedBox(
height: 200.0,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
margin: EdgeInsets.all(8.0),
child: InkWell(
splashColor: Colors.blueGrey,
onTap: () {},
child: Column(
// crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'About',
style: TextStyle(
fontSize: 32.0, fontWeight: FontWeight.bold),
),
Text(
'Vishwapreneur',
style: TextStyle(
fontSize: 32.0, fontWeight: FontWeight.bold),
),
],
),
),
),
),
SizedBox(
height: 200.0,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
margin: EdgeInsets.all(8.0),
child: InkWell(
splashColor: Colors.blueGrey,
onTap: () {},
child: Row(
// crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Smart Sociothon',
style: TextStyle(
fontSize: 32.0, fontWeight: FontWeight.bold),
),
// Text(
// 'Sociothon',
// style: TextStyle(
// fontSize: 32.0, fontWeight: FontWeight.bold),
// ),
],
),
),
),
),
SizedBox(
height: 200.0,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
margin: EdgeInsets.all(8.0),
child: InkWell(
splashColor: Colors.blueGrey,
onTap: () {},
child: Column(
// crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Entrepreneurship',
style: TextStyle(
fontSize: 30.0, fontWeight: FontWeight.bold),
),
Text(
'Development Cell',
style: TextStyle(
fontSize: 30.0, fontWeight: FontWeight.bold),
),
],
),
),
),
),
],
),
);
Thank you in Advance.
Pass your text along with textSize to your card.
The example below will give you an idea
ListView list(){
Map<String, double> items = new Map();
items["A"] = 14.0;
items["B"] = 24.0;
items["C"] = 32.0;
return new ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return myCard(items.keys.toList()[index], items.values.toList()[index]);
},
);
}
Card myCard(String text, double textSize){
return new Card(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Text(text, style: TextStyle(fontSize: textSize),),
),
);
}

Flutter: How to place a button in the middle of 2 containers?

UI Layout
If the purple area is inside an Expanded widget, how would I go about to position the button? I've implemented that UI by setting a fixed height to the purple container but haven't been able to understand how to achieve the same effect if the height is variable, depending on the content.
I was able to get close by using Stack and Positioned widgets but then the button is not really clickable. If anyone can give me a general idea on how to achieve the desired goal I would be thankful.
Here's my attempt (demo case)
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 1,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.redAccent,
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Stack(
children: <Widget> [
Padding(
padding: const EdgeInsets.only(bottom: 40.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.book,
color: Colors.white,
size: 40.0
),
Container(
width: 90.0,
child: new Divider(color: Colors.white),
),
Text(
"Some random text -",
style: TextStyle(color: Colors.white, fontSize: 25.0),
),
Padding(
padding: const EdgeInsets.only(top: 8.0, right: 70.0),
child: Row(
children: <Widget>[
Flexible(
child: Text(
'More random text that can vary a lot!',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Row(
children: <Widget>[
Text(
'Random text ',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Row(
children: <Widget>[
Text(
'Random',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
],
),
),
Positioned(
bottom: 0.0,
left: MediaQuery.of(context).size.width - 100.0,
child: FractionalTranslation(
translation: const Offset(0.0, 0.8),
child: FloatingActionButton(
backgroundColor: Colors.white,
foregroundColor: Colors.redAccent,
child: new Icon(
Icons.map
),
onPressed: () {
},
),
),
),
]
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0, left: 8.0),
child: Row(
children: <Widget>[
Flexible(
child: Text(
"Random text",
style: new TextStyle(
fontFamily: 'Raleway',
fontSize: 16.0,
color: Colors.black38
)
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Random text - long text",
style: new TextStyle(
fontFamily: 'Raleway',
fontSize: 18.0
)
),
),
],
)
],
)
),
),
],
);
Explaining what I described in the comments:
You can use the FractionalTranslation widget to shift your FAB half way down.
FractionalTranslation(translation: Offset(0, .5), child: FloatingActionButton(...))
This requires you to have it positioned at the bottom of the purple area (referring to your screenshot) beforehand, but I assume that you have that figured out.

Flutter - How do I get different results using the same future?

I am trying to get a different Times set by the user on each button. But show the same time for all of them. Whenever I change it, it changes the time of all their text. How do I do it, please explain?
This is my code:
import 'package:flutter/material.dart';
import 'package:flutter_duration_picker/flutter_duration_picker.dart';
import 'dart:async';
class EndurancePage extends StatefulWidget {
#override
_EndurancePageState createState() => _EndurancePageState();
}
class _EndurancePageState extends State<EndurancePage> {
Duration _duration = Duration();
Future<Null> _selectTime(BuildContext context) async {
final Duration picked =
await showDurationPicker(context: context, initialTime: _duration);
if (picked != null && picked != _duration) {
print('Time Selected: ${_duration.toString()}');
setState(() {
_duration = picked;
});
} }
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 100.0, bottom: 30.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Excer",
textAlign: TextAlign.center,
style:
const TextStyle(fontSize: 35.0, color: Colors.white),
),
Text(
"sices",
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 35.0,
color: Colors.white,
fontWeight: FontWeight.bold),
),
]),
),
Padding(
padding: EdgeInsets.only(bottom: 20.0),
child: Text(
"Select a few and set their duration.",
style: const TextStyle(color: Colors.white70, fontSize: 17.0),
),
),
SizedBox(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0),
child: Container(
height: 1.0,
color: Colors.yellowAccent,
),
),
),
//---------------LIST OF EVENTS---------------
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 50.0, top: 20.0, bottom: 10.0),
child: Text(
"Push UPS",
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: FlatButton(
onPressed: () {
_selectTime(context);
},
child: new Icon(
Icons.timelapse,
color: Colors.white,
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0, right: 20.0),
child: Text(
'${_duration.inMinutes.toString()} min',
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 50.0, top: 20.0, bottom: 10.0),
child: Text(
"Pull UPS",
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: FlatButton(
onPressed: () {
_selectTime(context);
},
child: new Icon(
Icons.timelapse,
color: Colors.white,
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0, right: 20.0),
child: Text(
'${_duration.inMinutes.toString()} min',
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 50.0, top: 20.0, bottom: 10.0),
child: Text(
"What's Today?",
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: FlatButton(
onPressed: () {
_selectTime(context);
},
child: new Icon(
Icons.timelapse,
color: Colors.white,
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0, right: 20.0),
child: Text(
'${_duration.inMinutes.toString()} min',
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
],
),
],
),
),
);
}
}
So, I start the app and the initial time is 0 initial time
Then I set the time on the first button setting time
Now it changes the time on all buttons final state
how do I get different times for different buttons?
The problem is that your are using just one variable and when you change that variable all Text widgets get updated with the same value.
You need to use three duration variables, one for each value.
The simplest way is the following:
import 'package:flutter/material.dart';
import 'package:flutter_duration_picker/flutter_duration_picker.dart';
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: EndurancePage(),
);
}
}
class EndurancePage extends StatefulWidget {
#override
_EndurancePageState createState() => _EndurancePageState();
}
class _EndurancePageState extends State<EndurancePage> {
Duration _duration1 = Duration();
Duration _duration2 = Duration();
Duration _duration3 = Duration();
Future<Null> _selectTime(BuildContext context, int timer) async {
if (timer == 1) {
final Duration picked =
await showDurationPicker(context: context, initialTime: _duration1);
if (picked != null && picked != _duration1) {
setState(() {
_duration1 = picked;
});
}
} else if (timer == 2) {
final Duration picked =
await showDurationPicker(context: context, initialTime: _duration2);
if (picked != null && picked != _duration2) {
setState(() {
_duration2 = picked;
});
}
} else if (timer == 3) {
final Duration picked =
await showDurationPicker(context: context, initialTime: _duration3);
if (picked != null && picked != _duration3) {
setState(() {
_duration3 = picked;
});
}
}
}
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 100.0, bottom: 30.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Excer",
textAlign: TextAlign.center,
style:
const TextStyle(fontSize: 35.0, color: Colors.white),
),
Text(
"sices",
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 35.0,
color: Colors.white,
fontWeight: FontWeight.bold),
),
]),
),
Padding(
padding: EdgeInsets.only(bottom: 20.0),
child: Text(
"Select a few and set their duration.",
style: const TextStyle(color: Colors.white70, fontSize: 17.0),
),
),
SizedBox(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0),
child: Container(
height: 1.0,
color: Colors.yellowAccent,
),
),
),
//---------------LIST OF EVENTS---------------
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 50.0, top: 20.0, bottom: 10.0),
child: Text(
"Push UPS",
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: FlatButton(
onPressed: () {
_selectTime(context, 1);
},
child: new Icon(
Icons.timelapse,
color: Colors.white,
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0, right: 20.0),
child: Text(
'${_duration1.inMinutes.toString()} min',
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 50.0, top: 20.0, bottom: 10.0),
child: Text(
"Pull UPS",
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: FlatButton(
onPressed: () {
_selectTime(context, 2);
},
child: new Icon(
Icons.timelapse,
color: Colors.white,
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0, right: 20.0),
child: Text(
'${_duration2.inMinutes.toString()} min',
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 50.0, top: 20.0, bottom: 10.0),
child: Text(
"What's Today?",
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: FlatButton(
onPressed: () {
_selectTime(context, 3);
},
child: new Icon(
Icons.timelapse,
color: Colors.white,
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0, right: 20.0),
child: Text(
'${_duration3.inMinutes.toString()} min',
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
],
),
],
),
),
);
}
}
Another option, passing the duration variable as a parameter would be the following:
import 'package:flutter/material.dart';
import 'package:flutter_duration_picker/flutter_duration_picker.dart';
import 'dart:async';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: EndurancePage(),
);
}
}
class EndurancePage extends StatefulWidget {
#override
_EndurancePageState createState() => _EndurancePageState();
}
class _EndurancePageState extends State<EndurancePage> {
Duration _duration1 = Duration();
Duration _duration2 = Duration();
Duration _duration3 = Duration();
Future<Duration> _selectTime(BuildContext context, Duration duration) async {
final Duration picked =
await showDurationPicker(context: context, initialTime: duration);
if (picked != null && picked != duration) {
setState(() {});
return picked;
}
return null;
}
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 100.0, bottom: 30.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Excer",
textAlign: TextAlign.center,
style:
const TextStyle(fontSize: 35.0, color: Colors.white),
),
Text(
"sices",
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 35.0,
color: Colors.white,
fontWeight: FontWeight.bold),
),
]),
),
Padding(
padding: EdgeInsets.only(bottom: 20.0),
child: Text(
"Select a few and set their duration.",
style: const TextStyle(color: Colors.white70, fontSize: 17.0),
),
),
SizedBox(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 25.0),
child: Container(
height: 1.0,
color: Colors.yellowAccent,
),
),
),
//---------------LIST OF EVENTS---------------
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 50.0, top: 20.0, bottom: 10.0),
child: Text(
"Push UPS",
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: FlatButton(
onPressed: () async {
_duration1 = await _selectTime(context, _duration1);
},
child: new Icon(
Icons.timelapse,
color: Colors.white,
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0, right: 20.0),
child: Text(
'${_duration1.inMinutes.toString()} min',
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 50.0, top: 20.0, bottom: 10.0),
child: Text(
"Pull UPS",
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: FlatButton(
onPressed: () async {
_duration2 = await _selectTime(context, _duration2);
},
child: new Icon(
Icons.timelapse,
color: Colors.white,
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0, right: 20.0),
child: Text(
'${_duration2.inMinutes.toString()} min',
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 50.0, top: 20.0, bottom: 10.0),
child: Text(
"What's Today?",
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0),
child: FlatButton(
onPressed: () async {
_duration3 = await _selectTime(context, _duration3);
},
child: new Icon(
Icons.timelapse,
color: Colors.white,
),
),
),
Padding(
padding: EdgeInsets.only(top: 8.0, right: 20.0),
child: Text(
'${_duration3.inMinutes.toString()} min',
style: const TextStyle(color: Colors.white, fontSize: 25.0),
),
),
],
),
],
),
),
);
}
}