Home > Enterprise >  How can I define the type of FontAwesomeIcons?
How can I define the type of FontAwesomeIcons?

Time:09-19

I'm Preparing self defined function as followings:

import 'package:font_awesome_flutter/font_awesome_flutter.dart';
Container awesomeText({required IconDataRegular icon, required String text}) {
  return Container(
    margin: const EdgeInsets.fromLTRB(0, 5, 0, 0),
    child: Row(children: [
      Container(
        margin: EdgeInsets.only(top: 5),
        child: const FaIcon(
          // FontAwesomeIcons.lightbulb,
          icon,//here
          size: 15,
          color: Color.fromARGB(255, 102, 102, 102),
        ),
      ),
      Container(
        margin: const EdgeInsets.fromLTRB(5, 2, 0, 0),
        child: Text(
          // "This is Test",
          text,
          style: MyText().style(),
        ),
      ),
    ]),
  );
}

I Set argument as icon and it doesn't work with error like Arguments of a constant creation must be constant expressions.Try making the argument a valid constant, or use 'new' to call the constructor, however it works using FontAwesomeIcons.lightbulb instead.

Is the type in first argument IconDataRegular is wrong or should I define other type instead ?

Note Finally, Im gonna use this function like: awesomeText(FontAwesomeIcons.lightbulb,"This is Test.")

CodePudding user response:

You just need to remove the const keyword

Your code:

 child: const FaIcon(
            icon, //here
            size: 15,
            color: Color.fromARGB(255, 102, 102, 102),
          ),

Change to. just remove const keyword

 child: FaIcon(
            icon, //here
            size: 15,
            color: Color.fromARGB(255, 102, 102, 102),
          ),

Because your passing value as function argument so value is no more constant that's why error is showing

  • Related