Home > database >  How to customize a reusable Widget in Flutter
How to customize a reusable Widget in Flutter

Time:12-03

i'm using "SubCategoryTiles" widget all over the app. initially i was using same Group Icon but now i want to use different-different category_Icon wherever i use this widget. So i want to know how to do it. See the code and also the image with error i'm getting.

class SubCategoryTiles extends StatelessWidget {
  const SubCategoryTiles({
    required this.titleText,
    required this.onTapHandler,
    required this.category_Icon,
  });

  final Widget titleText;
  final VoidCallback onTapHandler;
  final IconData category_Icon;
  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: const CircleAvatar(
        backgroundColor: Colors.white,
        child: category_Icon,
        
        // Icon(
        //   Icons.group,
        //   color: Colors.deepOrange,
        // ),
      ),
      title: titleText,
      trailing: const Icon(Icons.arrow_right),
      onTap: onTapHandler,
    );
  }
}

enter image description here

CodePudding user response:

Wrap your category_Icon in a Icon widget

child: Icon(category_Icon),

IconData is not a widget, but an Icon is.

CodePudding user response:

The should be like this:

class SubCategoryTiles extends StatelessWidget {
  const SubCategoryTiles({
    required this.titleText,
    required this.onTapHandler,
    required this.category_Icon,
  });

  final Widget titleText;
  final VoidCallback onTapHandler;
  final IconData category_Icon;
  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: CircleAvatar(
        backgroundColor: Colors.white,
        child: Icon(
          category_Icon,
          color: Colors.deepOrange,
        ),

        // Icon(
        //   Icons.group,
        //   color: Colors.deepOrange,
        // ),
      ),
      title: titleText,
      trailing: const Icon(Icons.arrow_right),
      onTap: onTapHandler,
    );
  }
}

// This is how it should be added when you require it.

category_Icon: Icons.add_box,

  • Related