Flutter : Countdown widget that displays days, hours, minutes and seconds - flutter

I'm trying to build an Expanded Widget that would display Days, Hours, Minutes and Seconds remaining (see screenshot).
How can I achieve this?
I've gone through a whole bunch of flutter documentation but yet was unable to correctly create it..
screenshot of the widget I am trying to build
Thanks for the help!

You can copy paste run full code below
You can use package https://pub.dev/packages/flutter_countdown_timer
code snippet
CountdownTimer(
endTime: 1594829147719,
defaultDays: "==",
defaultHours: "--",
defaultMin: "**",
defaultSec: "++",
daysSymbol: "days",
hoursSymbol: "hrs ",
minSymbol: "min ",
secSymbol: "sec",
daysTextStyle: TextStyle(fontSize: 30, color: Colors.red),
hoursTextStyle: TextStyle(fontSize: 30, color: Colors.orange),
minTextStyle: TextStyle(fontSize: 30, color: Colors.lightBlue),
secTextStyle: TextStyle(fontSize: 30, color: Colors.pink),
daysSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.green),
hoursSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.amberAccent),
minSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.black),
secSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.deepOrange))
working demo
full code
import 'package:flutter/material.dart';
import 'package:flutter_countdown_timer/countdown_timer.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter 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;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CountdownTimer(
endTime: 1594829147719,
defaultDays: "==",
defaultHours: "--",
defaultMin: "**",
defaultSec: "++",
daysSymbol: "days",
hoursSymbol: "hrs ",
minSymbol: "min ",
secSymbol: "sec",
daysTextStyle: TextStyle(fontSize: 30, color: Colors.red),
hoursTextStyle: TextStyle(fontSize: 30, color: Colors.orange),
minTextStyle: TextStyle(fontSize: 30, color: Colors.lightBlue),
secTextStyle: TextStyle(fontSize: 30, color: Colors.pink),
daysSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.green),
hoursSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.amberAccent),
minSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.black),
secSymbolTextStyle:
TextStyle(fontSize: 20, color: Colors.deepOrange)),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

Related

Flutter theme not being applied to checkbox

I have a checkbox that is not accepting the theme data shown below:
final acceptTerms = Checkbox(
value: _acceptIsChecked,
tristate: false,
onChanged: (bool? acceptIsChecked) {
setState(() {
_acceptIsChecked = acceptIsChecked!;
});
},
);
My theme for the checkbox inside MaterialApp:
theme: ThemeData(
primarySwatch: createMaterialColor(Color(0xFFFF0000)),
backgroundColor: Colors.black,
fontFamily: 'Arial',
textTheme: const TextTheme(
headline1: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
headline2: TextStyle(fontSize: 15.0, fontStyle: FontStyle.italic),
),
primaryColor: Colors.white,
checkboxTheme: CheckboxThemeData(
checkColor: MaterialStateProperty.all(Colors.white),
fillColor: MaterialStateProperty.all(Colors.orange),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(45)
)
)
),
I'm expecting to see the checkbox fill color to be orange and the radius to be round vs square but it's defaulting to a blue fill color and staying square.
I am unable to figure out why. I can add the properties directly to the checkbox widget but I'd rather use a theme but currently it seems that isn't possible?
Full code from the register.dart file
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class RegisterPage extends StatefulWidget {
RegisterPage({Key, key, this.title}) : super(key: key);
final String? title;
#override
_RegisterPageState createState() => _RegisterPageState();
}
class _RegisterPageState extends State<RegisterPage> {
TextStyle style =
const TextStyle(fontSize: 20.0);
final _scaffoldKey = GlobalKey<ScaffoldState>();
bool _waiting = false;
final _singleton = Singleton();
bool _acceptIsChecked = false;
#override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
#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.
void showInSnackBar(BuildContext context, String value) {
var sb = SnackBar(content: Text(value));
_scaffoldKey.currentState!.showSnackBar(sb);
}
Widget okButton = MaterialButton(
color: Theme.of(context).primaryColor,
child: const Text("OK", style: TextStyle(color: Colors.white)),
onPressed: () {
Navigator.of(context).pushReplacementNamed('/login');
},
);
List<Widget> _buildPage(BuildContext context) {
AlertDialog accountCreatedAlert = AlertDialog(
title: const Text("Success"),
backgroundColor: Theme.of(context).backgroundColor,
contentTextStyle: TextStyle(color: Theme.of(context).primaryColor),
content: const Text("Account Created! Please login."),
actions: [
okButton,
],
);
final acceptTerms = Checkbox(
value: _acceptIsChecked,
activeColor: Colors.white,
checkColor: Color(0xFF4C4184),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
tristate: false,
onChanged: (bool? acceptIsChecked) {
setState(() {
_acceptIsChecked = acceptIsChecked!;
});
},
);
var page = SingleChildScrollView(
child: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Image.asset(
"assets/logo.svg",
width: 400,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Transform.scale(
scale: 2.0,
child: Theme(
data: ThemeData(unselectedWidgetColor: Colors.grey),
child: acceptTerms
),
),
const SizedBox(width: 10),
const Text("Accept Terms of Service?",
style: TextStyle(color: Colors.white))
],
),
],
),
),
),
);
var l = List<Widget>.empty(growable: true);
l.add(page);
if (_waiting) {
var modal = Stack(
children: const [
Opacity(
opacity: 0.3,
child: ModalBarrier(dismissible: false, color: Colors.grey),
),
],
);
l.add(modal);
}
return l;
}
return Scaffold(
resizeToAvoidBottomInset : false,
body: Container(
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/background.png"),
fit: BoxFit.cover,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Stack(children: _buildPage(context))
]
)
),
);
}
}
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
backgroundColor: Colors.black,
fontFamily: 'Arial',
textTheme: const TextTheme(
headline1: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
headline2: TextStyle(fontSize: 15.0, fontStyle: FontStyle.italic),
),
primaryColor: Colors.white,
checkboxTheme: CheckboxThemeData(
checkColor: MaterialStateProperty.all(Colors.white),
fillColor: MaterialStateProperty.all(Colors.orange),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(45)),
),
),
home: const ShowCheckBox(),
);
}
}
class ShowCheckBox extends StatefulWidget {
const ShowCheckBox({Key? key}) : super(key: key);
#override
State<ShowCheckBox> createState() => _ShowCheckBoxState();
}
class _ShowCheckBoxState extends State<ShowCheckBox> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Transform.scale(
scale: 3,
child: Checkbox(
onChanged: (_) {},
value: true,
splashRadius: 50,
tristate: false,
),
),
),
);
}
}
The same code is working for me -
Upgrade your flutter sdk.
Open terminal and run the following command:
flutter upgrade
Image
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
backgroundColor: Colors.black,
fontFamily: 'Arial',
textTheme: const TextTheme(
headline1: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
headline2: TextStyle(fontSize: 15.0, fontStyle: FontStyle.italic),
),
primaryColor: Colors.white,
checkboxTheme: CheckboxThemeData(
checkColor: MaterialStateProperty.all(Colors.white),
fillColor: MaterialStateProperty.all(Colors.orange),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(45)),
),
),
home: const ShowCheckBox(),
);
}
}
class ShowCheckBox extends StatefulWidget {
const ShowCheckBox({Key? key}) : super(key: key);
#override
State<ShowCheckBox> createState() => _ShowCheckBoxState();
}
class _ShowCheckBoxState extends State<ShowCheckBox> {
final Widget acceptTerms = Checkbox(
onChanged: (_) {},
value: true,
splashRadius: 50,
tristate: false,
);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Transform.scale(
scale: 3,
child: acceptTerms,
),
),
);
}
}
This code is almost similar like your code, then to its working properly.Do one thing copy my code, paste it in your machine and then run it, that's only the solution I guess.
Output

Why is the Text widget 's line height changes from English to Chinese?

With this code
Center(
child: Text(
'hello 你好',
style: TextStyle(backgroundColor: Colors.red, fontSize: 24),
),
)
It seems the Chinese will got a larger height than English ? Can i make them have a same height ?
The different text height is caused by different font family.
Would you change font that supports Chinese and English?
https://fonts.google.com/?subset=chinese-simplified&preview.text=%E4%BD%A0%E5%A5%BDHello&preview.text_type=custom
Or if you want to make a text height same, below is the example.
Using 'strutStyle' parameter in Text, althouhg it is not convenient, you can modifying each text height is same.
https://medium.com/#najeira/control-text-height-using-strutstyle-4b9b5151668b
https://github.com/flutter/flutter/issues/38875
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter 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> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _buildBody() {
return Column(
children: [
Center(
child: Text(
'hello 你好',
style: TextStyle(backgroundColor: Colors.red, fontSize: 24),
),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text(
"hello",
style: TextStyle(
fontSize: 24,
),
strutStyle: StrutStyle(
height: 1.5,
fontSize: 24,
),
),
color: Colors.red,
),
Container(
child: Text(
"你好",
style: TextStyle(
fontSize: 24,
),
strutStyle: StrutStyle(
height: 1.5,
fontSize: 24,
),
),
color: Colors.red,
),
],
),
],
);
}
}

Show dialog Prompt like in web page in Flutter

I want to show a dialog box like a notification/prompt Like this the image shown here is it possible to show in flutter
You can copy paste run full code below
You can use package https://pub.dev/packages/flushbar
In pubspec.yaml for workaround issue
flushbar:
git:
url: https://github.com/AndreHaueisen/flushbar.git
ref: 13c55a8
code snippet
flush = Flushbar(
title: "Hey Ninja",
message:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry",
flushbarPosition: FlushbarPosition.TOP,
flushbarStyle: FlushbarStyle.FLOATING,
reverseAnimationCurve: Curves.decelerate,
forwardAnimationCurve: Curves.elasticOut,
backgroundColor: Colors.red,
boxShadows: [
BoxShadow(
color: Colors.blue[800], offset: Offset(0.0, 2.0), blurRadius: 3.0)
],
backgroundGradient:
LinearGradient(colors: [Colors.blueGrey, Colors.black]),
isDismissible: true,
working demo
full code
import 'package:flutter/material.dart';
import 'package:flushbar/flushbar.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter 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;
Flushbar flush;
void _incrementCounter() {
flush = Flushbar(
title: "Hey Ninja",
message:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry",
flushbarPosition: FlushbarPosition.TOP,
flushbarStyle: FlushbarStyle.FLOATING,
reverseAnimationCurve: Curves.decelerate,
forwardAnimationCurve: Curves.elasticOut,
backgroundColor: Colors.red,
boxShadows: [
BoxShadow(
color: Colors.blue[800], offset: Offset(0.0, 2.0), blurRadius: 3.0)
],
backgroundGradient:
LinearGradient(colors: [Colors.blueGrey, Colors.black]),
isDismissible: true,
//duration: Duration(seconds: 10),
icon: Icon(
Icons.camera,
color: Colors.greenAccent,
),
showProgressIndicator: true,
progressIndicatorBackgroundColor: Colors.blueGrey,
titleText: Text(
"Hello Hero",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.yellow[600],
fontFamily: "ShadowsIntoLightTwo"),
),
messageText: Column(
children: <Widget>[
Text(
"You killed that giant monster in the city. Congratulations!",
style: TextStyle(
fontSize: 18.0,
color: Colors.green,
fontFamily: "ShadowsIntoLightTwo"),
),
Row(children: <Widget>[
FlatButton(
onPressed: () {
flush.dismiss();
},
child: Text(
"NO THANKS",
style: TextStyle(color: Colors.amber),
),
),
FlatButton(
onPressed: () {
flush.dismiss();
},
child: Text(
"ALLOW",
style: TextStyle(color: Colors.amber),
),
)
])
],
),
)..show(context);
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

Weird error message when running flutter run

Yesterday I updated my flutter version for the beta channel from 1.14.6 to 1.15.17. My app was running successfully yesterday. Now, the command flutter run gives me error message
How can I solve it??
There is an open issue of flushbar
https://github.com/AndreHaueisen/flushbar/issues/113#issuecomment-599857258
This is workaround you can use before release
dependencies:
flutter:
sdk: flutter
flushbar:
git:
url: https://github.com/AndreHaueisen/flushbar.git
ref: 13c55a8
full test code
import 'package:flutter/material.dart';
import 'package:flushbar/flushbar.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter 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;
void _incrementCounter() {
Flushbar(
title: "Hey Ninja",
message:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry",
flushbarPosition: FlushbarPosition.TOP,
flushbarStyle: FlushbarStyle.FLOATING,
reverseAnimationCurve: Curves.decelerate,
forwardAnimationCurve: Curves.elasticOut,
backgroundColor: Colors.red,
boxShadows: [
BoxShadow(
color: Colors.blue[800], offset: Offset(0.0, 2.0), blurRadius: 3.0)
],
backgroundGradient:
LinearGradient(colors: [Colors.blueGrey, Colors.black]),
isDismissible: true,
duration: Duration(seconds: 10),
icon: Icon(
Icons.check,
color: Colors.greenAccent,
),
mainButton: FlatButton(
onPressed: () {
print("clap");
},
child: Text(
"CLAP",
style: TextStyle(color: Colors.amber),
),
),
showProgressIndicator: true,
progressIndicatorBackgroundColor: Colors.blueGrey,
titleText: Text(
"Hello Hero",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.yellow[600],
fontFamily: "ShadowsIntoLightTwo"),
),
messageText: Text(
"You killed that giant monster in the city. Congratulations!",
style: TextStyle(
fontSize: 18.0,
color: Colors.green,
fontFamily: "ShadowsIntoLightTwo"),
),
)..show(context);
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

How do I change the color of raisedButton onPressed?

I'm trying to change the color of the raisedButton when pressed, I have been having trouble with this for a while, the code I have right now for the whole button widget is
children: <Widget>[
Expanded(
child: Container(
child: new SizedBox(
width: 15,
height: 60,
child: new RaisedButton(
color: pressed ? Color(0xFF1D1E33) : Colors.blue,
hoverColor: Colors.blueAccent,
focusColor: Colors.blueAccent,
child: Text(
'Male',
style: TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold,
color: Colors.white),
textAlign: TextAlign.center,
),
onPressed: () => setState((){pressed = !pressed;})),
),
I also had a boolean as shown below but I received an error when running the code saying "Failed assertion: boolean expression must not be null"
bool get pressed => null;
set pressed(bool pressed) {}
You can copy paste run full code below
code snippet
bool _pressed = false;
bool get pressed {
return _pressed;
}
set pressed(bool pressed) {
_pressed = pressed;
}
working demo
full code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter 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;
bool _pressed = false;
bool get pressed {
return _pressed;
}
set pressed(bool pressed) {
_pressed = pressed;
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: new SizedBox(
width: 100,
height: 60,
child: new RaisedButton(
color: pressed ? Color(0xFF1D1E33) : Colors.blue,
hoverColor: Colors.blueAccent,
focusColor: Colors.blueAccent,
child: Text(
'Male',
style: TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold,
color: Colors.white),
textAlign: TextAlign.center,
),
onPressed: () => setState(() {
pressed = !pressed;
})))),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}