I am new to flutter, I want to create classes that encompass some functionality of my code. Example here, I have a class that manages my notifications.
As you can see I set the status of my notifications to be able to use them more easily. I have a success status, an error, etc... but for now I have to rewrite all the code in each method.
How do I set the appearance of a default notification and then inject this code into my methods? In each method I want to be able to change the title and description but I don't want to change the colors manually. They must be defined in my method.
My class:
import 'package:flutter/material.dart';
import 'package:flushbar/flushbar.dart';
import 'package:app/utils/colors.dart';
class MyInfoBar extends Flushbar {
final String title;
final String description;
MyInfoBar({
@required this.title,
@required this.description,
});
Future information(BuildContext context)
{
return Flushbar(
flushbarPosition: FlushbarPosition.TOP,
duration: Duration(seconds: 3),
icon: Icon(
Icons.info_outline,
size: 28.0,
color: Colors.white,
),
shouldIconPulse : false,
backgroundColor: MyColors.colorContrastPurple,
titleText: Text(title, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, color: Colors.white)),
messageText: Text(description, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.0, color: Colors.white)),
).show(context);
}
Future success(BuildContext context)
{
return Flushbar(
flushbarPosition: FlushbarPosition.TOP,
duration: Duration(seconds: 3),
icon: Icon(
Icons.check_circle_outline,
size: 28.0,
color: Colors.white,
),
shouldIconPulse : false,
backgroundColor: MyColors.colorContrastGreen,
titleText: Text(title, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, color: Colors.white)),
messageText: Text(description, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.0, color: Colors.white)),
).show(context);
}
Future error(BuildContext context)
{
return Flushbar(
flushbarPosition: FlushbarPosition.TOP,
duration: Duration(seconds: 3),
icon: Icon(
Icons.error_outline,
size: 28.0,
color: Colors.white,
),
shouldIconPulse : false,
backgroundColor: MyColors.colorContrastRed,
titleText: Text(title, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0, color: Colors.white)),
messageText: Text(description, style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14.0, color: Colors.white)),
).show(context);
}
}
And when I want to use it:
MyInfoBar(title: "title", description: "description",).information(context);
Is this the right way to go?
You are almost there!
class MyInfoBar extends Flushbar {
// No need to store the values
MyInfoBar({
@required String title,
@required String description,
FlushbarPosition flushbarPosition = FlushbarPosition.TOP, // This is a default value. If you do not pass this property to the constructor, FlushbarPosition.TOP will be considered
Duration duration = const Duration(seconds: 3), // Same here but notice the const keyword. This is because default values should always be constant
bool shouldIconPulse = false,
// Other properties here and their default values. If you specify no default value, they will be null.
}) : super( // Now send these properties to the parent which is Flushbar
titleText: Text(title), // add default styling if you want
messageText: Text(description),
duration: duration,
shouldIconPulse: shouldIconPulse,
// Etc.
);
// So what we are doing here is creating a Flushbar with some default properties and naming it MyInfoBar.
// Now whenever you instantiate MyInfoBar, you get the default notification and you can of course adjust the properties.
// Static methods are cleaner in this context as in your example,
// you are instantiating this class but only using it to instantiate
// a new Flushbar thus eliminating the purpose of the first object you created.
static Future success({
@required String title,
@required String description,
@required BuildContext context
}) {
return MyInfoBar(title: title, description: description).show(context); // Other properties get the default values as you have not specified any here.
}
// Similarly you can define other methods.
}
Now to access it:
MyInfoBar.success(title: "Hello World", description: "Beep boop!", context: context);
I wrote this code here directly so there may be some typos but, I hope you get the idea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.