I want to declare a string which has a value of a color in a random widget… why it is not woorking?
3
FloatingActionButton takes a named parameter.
The problem isnt directly the declared variable but the way you try to pass the argument.
If you use e.g a Text widget it would be working.
Text( gColor )
If you plan to pass a color you need to specify a color type and use it in the TextStyle of the TextWidget. Take an suitable type:
Text(String data, {Key? key, TextStyle? style, StrutStyle? strutStyle, TextAlign? textAlign, TextDirection? textDirection, Locale? locale, bool? softWrap, TextOverflow? overflow, double? textScaleFactor, int? maxLines, String? semanticsLabel, TextWidthBasis? textWidthBasis, TextHeightBehavior? textHeightBehavior, Color? selectionColor})
See the docs for the action button https://api.flutter.dev/flutter/material/FloatingActionButton-class.html
FloatingActionButton({Key? key, Widget? child, String? tooltip, Color? foregroundColor, Color? backgroundColor, Color? focusColor, Color? hoverColor, Color? splashColor, Object? heroTag = const _DefaultHeroTag(), double? elevation, double? focusElevation, double? hoverElevation, double? highlightElevation, double? disabledElevation, required VoidCallback? onPressed, MouseCursor? mouseCursor, bool mini = false, ShapeBorder? shape, Clip clipBehavior = Clip.none, FocusNode? focusNode, bool autofocus = false, MaterialTapTargetSize? materialTapTargetSize, bool isExtended = false, bool? enableFeedback})
The child of a FloatingActionButton is declared as:
child
FloatingActionButton
final Widget? child;
This means it needs to be a Widget, not a String. You need to put it in a Text widget, or find some other way to turn your String into a Widget.
Text
It don’t work like this, the backgroundColor is a property of FloatingActionButton so you can’t just put the properties and it’s value in a String,
backgroundColor
Rather you need to store the value in the variable and give it to the Widget’s property,
Here in your case we’ll store Colors.green in the ‘Color’ typed variable and give it to the backgroundColor property:
Colors.green
Color bgColor = Colors.green;
and will be used as:
FloatingActionButton( backgroundColor: bgColor, onPressed: (){}, child: Text('Button') )
Click here to cancel reply.
3
Answers
FloatingActionButton takes a named parameter.
The problem isnt directly the declared variable but the way you try to pass the argument.
If you use e.g a Text widget it would be working.
If you plan to pass a color you need to specify a color type and use it in the TextStyle of the TextWidget. Take an suitable type:
See the docs for the action button
https://api.flutter.dev/flutter/material/FloatingActionButton-class.html
The
child
of aFloatingActionButton
is declared as:This means it needs to be a Widget, not a String. You need to put it in a
Text
widget, or find some other way to turn your String into a Widget.It don’t work like this, the
backgroundColor
is a property ofFloatingActionButton
so you can’t just put the properties and it’s value in a String,Rather you need to store the value in the variable and give it to the Widget’s property,
Here in your case we’ll store
Colors.green
in the ‘Color’ typed variable and give it to thebackgroundColor
property:and will be used as: