How to achieve CSS class-like styling of buttons in flutter? - flutter

I am new to flutter, and I want to reduce code duplication when it comes to UI.
Let's say that I chose some theme. ElevatedButton will have color of that theme.
Now, I want to have some buttons that will by styled alternatively (e.g. 'Cancel' buttons to be grey or 'Accept' buttons to be bright green).
Normally I do like this:
ElevatedButton(
style: ElevatedButton.styleFrom(primary: Colors.grey[100]),
onPressed: () {
// onPressed logic here
},
child: const Text(
'Cancel',
style: TextStyle(color: Colors.black54),
),
),
but if I have multiple Cancel buttons, this code would be repeated:
style: ElevatedButton.styleFrom(primary: Colors.grey[100]), and
style: TextStyle(color: Colors.black54),
What is "Flutter" way to achieve it so I can have "Cancel" or "Accepted" style defined in one place? Should I create my own widget CancelButton? or is there better way?

ElevatedButton(
style: Styles.cancelButtonStyle(),
onPressed: () {
// onPressed logic here
},
child: const Text(
'Cancel',
style: Styles.cancelButtonTextStyle(),
),
),
Your Custom Style Class
class Styles{
static TextStyle cancelButtonTextStyle(){
return TextStyle(
fontSize: 16,
color: Colors.black54
);
}
static TextStyle acceptButtonTextStyle(){
return TextStyle(
fontSize: 16,
color: Colors.black54
);
}
static ButtonStyle cancelButtonStyle(){
return ElevatedButton.styleFrom(primary: Colors.grey[100]);
}
static ButtonStyle acceptButtonStyle(){
return ElevatedButton.styleFrom(primary: Colors.green[700]);
}
}

Related

Reset State of Slide_to_act widget in flutter

I had a problem when I slide my slide_to_act widget it works correctly and it push me to another page. but when I use nav.pop ( when I turn back to old page ) my slide_to_act button looks done. I want to reset it.
SlideAction(
height: 70,
sliderRotate: false,
alignment: Alignment.centerRight,
onSubmit: () {
slideAnimated_push(
context,
ConfirmPage(
total: summedPrice.toInt(), slevee: slevee));
},
innerColor: Colors.white,
outerColor: StGreen,
elevation: 1,
text: 'Slide to Confirm',
textStyle: const TextStyle(
fontFamily: 'RaleWay',
fontSize: 20,
color: Colors.white,
),
),[enter image description here][1]
May be you can reset it after going to the other page.

How can i affect a single TimerButton to resend OTP in a lists builder with multiple TimerButton on flutter?

Am using timer_button package. the issue is there are possibilities where a user might have multiple transactions on his list so if he chooses to resend the otp for a particular transaction, all the other buttons also become disabled as well. How can i ensure that its only the clicked button that responds to the action?
TimerButton(
buttonType: ButtonType
.RaisedButton,
label: "Resend OTP",
timeOutInSeconds:
10,
onPressed: () {
setState(() {
ResendOTP(snapshot
.data[index]
.invoiceNumber);
});
},
disabledColor:
Colors.grey,
color: appbarColor,
disabledTextStyle:
new TextStyle(
fontSize:
15.0),
activeTextStyle:
new TextStyle(
fontSize:
15.0,
color: Colors
.white),
)

How to pass multiple parameters to a function calling onTap of SfCalender in Flutter?

I am using SfCalender and want to pass one more parameter to a callBack of onTap
SfCalendar(
dataSource: _getDataSource(snapshot.data.data),
view: CalendarView.month,
showNavigationArrow: true,
monthViewSettings: MonthViewSettings(
showAgenda: true,
agendaStyle: AgendaStyle(
backgroundColor: Colors.transparent,
appointmentTextStyle: TextStyle(
color: Colors.white, fontSize: 13, fontStyle: FontStyle.italic),
dateTextStyle: TextStyle(
color: primaryColor, fontSize: 13, fontStyle: FontStyle.italic),
),
),
controller: _calendarController,
onTap: _openForm(array),
)
_openForm(CalendarTapDetails details) {
//code
}
Currently if I dont pass any argument to _openForm, CalenderDetails details have the values but I want to pass one array along with CalenderDetails details. How can I do that?
I don't know the SfCalendar library (and it is closed source), but I think you are trying to do that:
onTap: (details) => _openForm(details, array);
// ...
_openForm(CalendarTapDetails details, List myArray) {
// code
}

Dart - How to a change the color of my raisedButton after clicking in dart?

so i've been struggling to change the color of my button from green to red for my flutter app. Most of the online resources are confusing me. This is my following code.
new RaisedButton(key:null, onPressed:buttonPressed,
color: Colors.green,
child:
new Text(
"10:00 A.M. - 11:00 A.M.",
style: new TextStyle(fontSize:15.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
)
),void buttonPressed(){
}
I want to click it and it turn green, or even better. Click it, so it turns gray. Then click another button that states "confirm", and it would make all the grey buttons that have been clicked red. Regardless, I'm just trying to understand how to make the button change color after being clicked.
Basically, what you need to do is,
Make RaisedButton color a class-level variable instead of constant as you have now.
Initialize that variable to some color value in initState method.
In the onPressed handler of RaisedButton, do the following:
Change that variable to some color value of your liking.
Trigger Repaint (so that the new color gets painted) i.e. call setState method.
Last but not least, you'll need to have all the code above part of a stateful widget.
Hopefully, it's clear enough, let us know if you'd need some code sample as well.
bool turnToRed = false;
bool colorIsGreen = true;
RaisedButton(key:null, onPressed: (){
setState((){
colorIsGreen = !colorIsGreen;
});
},
color: colorIsGreen ? Colors.green :turnToRed ? Colors.red : Colors.grey,
child:
new Text(
"10:00 A.M. - 11:00 A.M.",
style: new TextStyle(fontSize:15.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
)
),
RaisedButton(key:null, onPressed: (){
setState((){
turnToRed = !turnToRed;
});
},
color: Colors.red
child:
new Text(
"Confirm",
style: new TextStyle(fontSize:15.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
fontFamily: "Roboto"),
)
),

How to define custom text theme in flutter?

How to make my own text theme style?
I only find the default text theme like this but it's not enough.
textTheme: TextTheme(
body1: TextStyle(),
body2: TextStyle(),
button: TextStyle(),
caption: TextStyle(),
display1: TextStyle(),
display2: TextStyle(),
display3: TextStyle(),
display4: TextStyle(),
headline: TextStyle(),
overline: TextStyle(),
subhead: TextStyle(),
subtitle: TextStyle(),
title: TextStyle(),
),
I want for example have a text with line through then some other have underline etc
I was thinking to override the body2 for underline style then how to define another style for line through?
Kind Regards
You can create a class to hold your style and then call it from anywhere in your app.
class CustomTextStyle {
static TextStyle display5(BuildContext context) {
return Theme.of(context).textTheme.display4.copyWith(fontSize: 192.0);
}
}
And the use it as
Text(
'Wow',
style: CustomTextStyle.display5(context),
),
Look at question Flutter: Define custom TextStyles for use throughout the app that contains the complete answer referred here.
Here is an alternative using the extension method with the lineThrough:
extension CustomStyles on TextTheme {
TextStyle get error => const TextStyle(decoration: TextDecoration.lineThrough, fontSize: 20.0, color: Colors.blue, fontWeight: FontWeight.bold);
And then you can use it like this:
Text("your text", style: Theme.of(context).textTheme.error)
Since the release of dart 2.7, you can also use an extension method:
extension CustomStyles on TextTheme {
TextStyle get error {
return TextStyle(
fontSize: 18.0,
color: Colors.red,
fontWeight: FontWeight.bold,
);
}
}
Then you can use it like this:
Text(error, style: Theme.of(context).textTheme.error)
You can set the TextStyle() options for further customization. For the specs you need set the decoration option. For underlining:
TextStyle(decoration: TextDecoration.underline)
and for line through:
TextStyle(decoration: TextDecoration.lineThrough)