skip to Main Content

...

I want to declare a string which has a value of a color in a random widget…
why it is not woorking?

3

Answers


  1. 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})
    
    Login or Signup to reply.
  2. The child of a FloatingActionButton is declared as:

    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.

    Login or Signup to reply.
  3. 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,

    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:

    Color bgColor = Colors.green;
    

    and will be used as:

    FloatingActionButton(
       backgroundColor: bgColor,
       onPressed: (){},
       child: Text('Button')
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search