Home > OS >  display an icon string from environment variables file using flutter
display an icon string from environment variables file using flutter

Time:06-08

I'm working on a flutter project and I have an icon in my environment variable file like that :ICON1=Icon(IconData(U 0E318)) And I want to display it in my bottom navigation bar using this code :

BottomNavigationBarItem(
            icon: dotenv.env['ICON1'] ?? 'ICON1 not found',
            label: 'Accueil',
          ),

But it gave me this error : The argument type 'String' can't be assigned to the parameter type 'Widget'. Is there a way I can display it. Any help is highly appreciated.

CodePudding user response:

The BottomNavigationBarItem expects a Widget in the icon.

So, You can do it by using Icon class constants according to official Flutter docs.

BottomNavigationBarItem(
        icon: Icon(IconData(int.parse(Icon(dotenv.env['ICON1'] ?? 0xf04b6 ), fontFamily: 'MaterialIcons')),
        label: 'Accueil',
      ),

IF you already have Icon(IconData(U 0E318)) in the env you can simply cast is as Icon like

icon: dotenv.env['ICON1] as Icon

CodePudding user response:

First of all, all values stored in a .env file are loaded as string values, So you can't just put it as a widget there.

What you can do is store the codePoint of an Icon, which you can in return use with the IconData constructor to get a IconData object.

Get a codePoint value of an Icon like this:

print(Icons.abc.codePoint);

Once you got the value, put it inside the .env file

And next time you load it, pass the value inside the int.parse() function

int codePoint = int.parse(dotenv.env['my_icon'])

And then you can use that icon like this:

icon: Icon(IconData(codePoint))
  • Related