Home > Mobile >  How to use String Icon name in inside the icons in flutter?
How to use String Icon name in inside the icons in flutter?

Time:03-20

How to use String name in Icon data. Flutter doesn't allow to make changes in Icons only allows Material Design Icons only I tried several ways like data[index] but it didn't work

import 'package:flutter/material.dart';


void main() async {
  runApp(MaterialApp(home: GetxLearn()));
}

class GetxLearn extends StatefulWidget {


  @override
  _GetxLearnState createState() => _GetxLearnState();
}

class _GetxLearnState extends State<GetxLearn> {
  List<String> data = [
    "add",
    "assistant_direction",
    "tablet",
    "new_label",
    "add",
  ];

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
        home: Scaffold(
      body: ListView.builder(
          itemCount: data.length,
          itemBuilder: (BuildContext context, int index) {
            return Center(
                child: IconButton(
              icon: const Icon(
                Icons.new_label,
                //Icons.data[index] ====> need this type
              ),
              onPressed: () {},
            ));
          }),
    ));
  }
}

CodePudding user response:

one way is that you can generate images to font icon (Generate to font). save ttf file in assets. pass unicode data to json (like "e90a").

Icon(IconData(int.parse('0x${e90a}',
    fontFamily: 'family name given in the link above'));

CodePudding user response:

Icons.add is defined as

static const IconData add = IconData(0xe047, fontFamily: 'MaterialIcons');

add is a static variable inside Icons.

You can create a list of int that will hold codePoint of Icon.

 List<int> data = [
    0xe047,
    Icons.face.codePoint,
    ....
  ];

And use like

 IconData(data[index], fontFamily: 'MaterialIcons'),

You can get IconCode from flutter.dev or you can use like Icons.face.codePoint,

  • Related