isn't a valid override of 'StyleHook.textStyle - flutter

Error is
The method 'Style.textStyle' has fewer positional arguments than those
of overridden method 'StyleHook.textStyle'. TextStyle textStyle(Color
color)
'Style.textStyle' ('TextStyle* Function(Color*)*') isn't a valid
override of 'StyleHook.textStyle' ('TextStyle Function(Color,
String?)').
Flutter Channel stable, 3.0.0
environment:
sdk: ">=2.7.2 <3.0.0"
Here is the problem #override TextStyle textStyle(Color color) { return TextStyle(color: color);
class Style extends StyleHook {
#override
double get activeIconSize => 28;
#override
double get activeIconMargin => 10;
#override
double get iconSize => 20;
#override
TextStyle textStyle(Color color) {
return TextStyle(color: color);
}
}

I had a similar problem using convex_bottom_bar.
The solution was posted at the following URL and I followed it and it solved the problem.
Error: The method 'Style.textStyle' has fewer positional arguments than
I looked at the StyleHook definition and found that a String type was optionally specified, so I changed it to accept it as shown below, and the error did not occur.
class Style extends StyleHook {
#override
double get activeIconSize => 30;
#override
double get activeIconMargin => 5;
#override
double get iconSize => 24;
#override
TextStyle textStyle(Color color, String s) {
return TextStyle(
fontSize: 11,
color: Colors.white,
);
}
}

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

Error: The method 'Style.textStyle' has fewer positional arguments than

Error is
The method 'Style.textStyle' has fewer positional arguments than those
of overridden method 'StyleHook.textStyle'. TextStyle textStyle(Color
color)
Here is the problem #override TextStyle textStyle(Color color) { return TextStyle(color: color);
class Style extends StyleHook {
#override
double get activeIconSize => 28;
#override
double get activeIconMargin => 10;
#override
double get iconSize => 20;
#override
TextStyle textStyle(Color color) {
return TextStyle(color: color);
}
}
Your error says exactly what's wrong. StyleHook expects a Color and a String? as argument for the textStyle function. You extend StyleHook and override that function but are not providing the same arguments for it. It needs to look like
TextStyle textStyle(Color color, String? s) {

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 I want to convert NumberFormat form to double

Below is the code where the error occurs.
I want to convert [NumberFormat][1] form to double. What should I do?
I want to display it on the screen with the currency symbol, and get the result as a double .
Sample source:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class ProductEditor extends StatefulWidget {
const ProductEditor({Key? key}) : super(key: key);
#override
_ProductEditorState createState() => _ProductEditorState();
}
class _ProductEditorState extends State<ProductEditor> {
final NumberFormat euNumFormat =
NumberFormat.currency(locale: 'eu_EU', symbol: '€');
TextEditingController _ctrPrice = TextEditingController();
double _price = 25000.00;
#override
void initState() {
super.initState();
_ctrPrice.text = euNumFormat.format(_price); //
}
#override
Widget build(BuildContext context) {
return Container(
width: 500,
height: 300,
child: TextField(
controller: _ctrPrice,
onSubmitted: (String price) {
setState(() {
final double value = double.tryParse(_ctrPrice.text
.substring(0, _ctrPrice.text.length - 2)
.replaceAll(',', '')) ??
0.0;
print('price:${_ctrPrice.value.toString()} ${value}');
final formattedPrice = euNumFormat.format(value);
_ctrPrice.value = TextEditingValue(
text: formattedPrice,
selection: TextSelection.collapsed(offset: formattedPrice.length),
);
});
},
),
);
}
}
You can create a substring by removing symbol and , from there, then parse it.
final double retailPrice = double.tryParse(
_ctrPrice.substring(0, _ctrPrice.length - 2).replaceAll(",", ''),
) ??
0.0;
This solution only work on specific number format('eu_EU').

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