FontWeight implementation error after flutter upgrade to 3.1.0 - flutter

Missing concrete implementation of 'getter FontWeight.value'.
Try implementing the missing method, or make the class abstract.
'''
enum TitleWeight implements FontWeight {
regular(FontWeight.w400),
medium(FontWeight.w500),
semiBold(FontWeight.w600),
bold(FontWeight.w700),
extraBold(FontWeight.w800),
black(FontWeight.w900);
final FontWeight weight;
const TitleWeight(this.weight);
}
'''

This was simply solved by implementing the value Function, as such
/// Cinzel
enum TitleWeight implements FontWeight {
regular(FontWeight.w400),
medium(FontWeight.w500),
semiBold(FontWeight.w600),
bold(FontWeight.w700),
extraBold(FontWeight.w800),
black(FontWeight.w900);
#override
int get value => _weight.value;
final FontWeight _weight;
const TitleWeight(this._weight);
}

Related

How can i use theme(x) extinction inside another theme extinction(y)

I need to use theme extinction for colors for example inside theme extension for LargeBodyTextStyle
#immutable
class BodyLargeStyle extends ThemeExtension<BodyLargeStyle> {
final Color? color;
final double? fontSize;
final double? lineHeight;
final FontWeight? fontWeight;
final String? fontFamily;
const BodyLargeStyle({
required this.color,
required this.fontSize,
required this.lineHeight,
required this.fontWeight,
required this.fontFamily,
});
#override
ThemeExtension<BodyLargeStyle> copyWith() {
// TODO: implement copyWith
throw UnimplementedError();
}
#override
ThemeExtension<BodyLargeStyle> lerp(
ThemeExtension<BodyLargeStyle>? other, double t) {
// TODO: implement lerp
throw UnimplementedError();
}
static BodyLargeStyle bodyLargeStyle = BodyLargeStyle(
color: Theme.of(context).extension<CustomColors>()!.color,
fontSize: 12,
lineHeight: 1,
fontWeight: FontWeight.w400,
fontFamily: "any",
);
}
here I need to implement something like this but I don't have the context and I need to get use
of how theme Extention useful when changing from light to dark via verse

Flutter apply method to static variable

How do you apply a method to a static variable from a class. There are certain built in classes in flutter that appear to do something like this.
class UITextStyle {
static const TextStyle body = TextStyle(fontSize: 17);
addColor(Color color) {
TextStyle style = this as TextStyle;
style.merge(TextStyle(color: color));
}
}
Which can then be called like this:
UITextStyle.body.addColor(Color.fromRGBA(0,0,0,1));
However I cannot call that method like that as firstly it is not static, and secondly if it were I would not be able to call it after declaring .body first and would only be able to call it at UITextStyle.addColor(...).
How is this achieved?
you can try this solution , the point is that addColor function is not defined to the TextStyle Type , so to achieve that you need to add this function to the TextStyle class by this extension :
extension TextStyleEx on TextStyle{
TextStyle addColor(Color color) {
return merge(TextStyle(color: color,fontWeight: FontWeight.w600));
}
}
and make this method return TextStyle so you can get instance from the merged ones , cause your static object is final so you can not receive new value on it.
and leave your class like this
class UITextStyle {
static const TextStyle body = TextStyle(fontSize: 17);
}
use this class and the saved static object to get new TextStyle with the old and the new TextStyles.
for test run this in main , will clear the previous example :
TextStyle mergedStyles = UITextStyl.body.addColor(Colors.black);
print(mergedStyles);
Thanks to the comments from #pskink I was eventually able to get this functioning.
class UITextStyle {
const UITextStyle(this.style);
final TextStyle style;
static const body = UITextStyle(TextStyle(fontSize: 17));
addColor(Color color) {
TextStyle textStyle = style;
return textStyle.merge(TextStyle(color: color));
}
}
In Dart extensions can have static members.
extension UITextStyle on TextStyle {
static const body = TextStyle(fontSize: 17);
TextStyle addColor(Color color) {
return this.merge(TextStyle(color: color));
}
}
UITextStyle.body.addColor(Color.fromRGBO(0, 0, 0, 1));

Dart: avoid getters/methods duplication for proxy object

I have proxy object companyCustomColors:
class CustomColors {
final CompanyCustomColors companyCustomColors;
CustomColors(BuildContext context)
: companyCustomColors =
Theme.of(context).extension<CompanyCustomColors>() ?? defaultColors;
Color get vipColor => companyCustomColors.vipColor;
Color get linksColor => companyCustomColors.linksColor;
Color get linkPressedColor => companyCustomColors.linkPressedColor;
}
Is it possible to use some Dart features (proxy, mixin, delegate) to get rid of these getters (vipColor, linksColor, linkPressedColor), but still have IDE autocomplete suggestions for CustomColors?
This object used like this one:
Text('sample',
style: TextStyle(
color: CustomColors(context).vipColor,
height: lineHeight,
),
)
Other classes used in this example:
class CompanyCustomColors extends ThemeExtension<CompanyCustomColors> {
const CompanyCustomColors({
required this.vipColor,
required this.linksColor,
required this.linkPressedColor,
});
final Color vipColor;
final Color linksColor;
final Color linkPressedColor;
}
const CompanyCustomColors defaultColors = CompanyCustomColors(
vipColor: AppColors.orange,
linksColor: AppColors.blue,
linkPressedColor: AppColors.blue_pressed,
);

Flutter: add a simple wrapper to the Text widget

I came from a React world and trying to get my head around Flutter and Dart.
I'm using the Text widget with the same parameters a lot, so it seems reasonable to think of a way to reuse code. I created a wrapper that uses it:
import 'package:flutter/material.dart';
TextStyle getThemeProperty(type, TextTheme textTheme) {
switch (type) {
case 'headline1':
return textTheme.headline1;
case 'headline2':
return textTheme.headline2;
case 'headline3':
return textTheme.headline3;
default:
return textTheme.bodyText2;
}
}
class CustomText extends StatelessWidget {
const CustomText({Key key, this.type, this.text, this.color}) : super(key: key);
final type;
final text;
final color;
#override
Widget build(BuildContext context) {
var textTheme = Theme.of(context).textTheme;
var style = getThemeProperty(type, textTheme);
if (this.color != null) style.color = this.color;
return Text(
this.text,
style: style,
);
}
}
// Usage
CustomText(
text: 'Some Heading',
type: 'headline2',
color: Colors.black
)
The idea is to set the color if the color property is passed as a parameter, but Dart's compiler doesn't like it. It throws me the error: ''color' can't be used as a setter because it's final.
Try finding a different setter, or making 'color' non-final.'
I'm planning to do the same to fontWeight and textAlign properties as well. How am I able to make this work, I mean, to add new props to the style object on demand?
The reason why the dart compiler is unhappy is just because the color property of the TextStyle is declared as final. Therefore to use a new color, you have to create a new instance of the TextStyle.
Luckily, the TextStyle class comes with a copyWith method that returns an edited copy of your TextStyle
final type;
final text;
final color;
#override
Widget build(BuildContext context) {
var textTheme = Theme.of(context).textTheme;
var style = getThemeProperty(type, textTheme);
return Text(
this.text,
// Added this...
style: style.copyWith(color: color ?? style.color),
);
}
As a side note, when making reusable widgets, it's always a good idea to type your parameters. This is because any type of variable can be used. So instead of passing a String for text, you may pass an int
// DON'T DO THIS
final type;
final text;
final color;
// DO THIS
final String type;
final String text;
final Color color;
Also adding the this keyword to reference a variable in a class without variable shadowing is unnecessary.
// DON'T
this.text
// DO
text

How to pre-define propeties in classes Flutter

I have created a CardText as a stateless widget and I will use it whenever I would be needing it. But I have a problem. As y'all can see, there are properties that I haven't marked as #required. What I want is these properties have a pre-defined value. Like, suppose the color property, it should be 0xFFFFFFFF until and unless I want somewhere to be as 0xFF000000. But these are final properties that can't be assigned on the basis of ??= method. Yes, I know, marking these properties as #required will require me to define each and every property whenever I call it. But having a pre-defined value will help me a lot to save time and a few lines of code.
Well, any expert out there, I don't know how to express the problem, so feel free to change the title. Thank you.
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
class CardText extends StatelessWidget {
final String data;
final int color;
final int fontSize;
final FontWeight fontWeight;
const CardText(
this.data, {
this.color,
this.fontSize,
this.fontWeight,
});
#override
Widget build(BuildContext context) {
return Text(
data,
style: GoogleFonts.openSans(
textStyle: TextStyle(
fontSize: fontSize,
fontWeight: fontWeight,
color: Color(color),
),
),
);
}
}
If your arguments are optional then you can give default it right away, like following
const CardText({
this.data,
this.color = 0xFFFFFFFF,
this.fontSize = 14,
this.fontWeight,
})
You can use the : colon syntax:
const CardText(
this.data, {
this.color,
this.fontSize,
this.fontWeight,
}) : color = 0xFFFFFFFF, data = "data"
The code after the colon will be executed before the code inside the curly brackets. From the linked question
The part after : is called "initializer list. It is a ,-separated list
of expressions that can access constructor parameters and can assign
to instance fields, even final instance fields. This is handy to
initialize final fields with calculated values.