I pass IconData as parameter of a Widget but I got an error says Type 'Icon' is not a subtype of type 'IconData' in type case. I try so many questions but couldn't help me with my error.
I have tried this question:
How to solve "IconData' is not a subtype of type 'Widget'"?
Category.dart
import 'package:flutter/material.dart';
class Catgory extends StatelessWidget {
final String Txt;
const Catgory({super.key, required this.Txt});
@override
Widget build(BuildContext context) {
return Scaffold(
body: category_card(name: Txt),
);
}
}
double defaultRadius = 8.0;
class category_card extends StatefulWidget {
final String name;
const category_card({super.key, required this.name});
@override
State<category_card> createState() => _category_cardState();
}
class _category_cardState extends State<category_card> {
@override
Widget build(BuildContext context) {
return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: MyColorsSample.primaryDark),
onPressed: () => Navigator.pop(context),
),
title: Text('طالع بینی ماه '+widget.name, style: const TextStyle(color: MyColorsSample.primaryDark),),
backgroundColor: Colors.white,
elevation: 0,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(8),
scrollDirection: Axis.vertical,
child: Row(
children: [
Column(
children: [
Row(
children: [
// gradientCardSample("کلی", 12, context, IconData(Icon(Icons.ac_unit) as int)),
const SizedBox(width: 10,),
gradientCardSample("عشق", 12, context, const Icon(Icons.book, size: 20, color: Colors.blue,) as IconData),
]
//
),
]
),
],
),
),
),
);
}
}
Widget gradientCardSample(String title, int hint, BuildContext mcontext, IconData icon) {
return Builder(
builder: (context)=> InkWell(
splashColor: Colors.black.withAlpha(30),
onTap: () {
debugPrint('Card tapped.');
// Navigator.of(mcontext).push(MaterialPageRoute(builder: (mcontext) => const Catgory(Txt: "hi")));
},
child: Container(
height: 120,
width: 180,
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
gradient: const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [ Color(0xFF846AFF), Color(0xFF755EE8), Colors.blueAccent,Colors.blue,],
),
borderRadius: radius(16)),
clipBehavior: Clip.hardEdge,
child: Row(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 10,),
Text(title, style: const TextStyle(fontSize: 20, color: Colors.white, )),
],
),
//, style: boldTextStyle(color: Colors.white, size: 20)
const Spacer(),
Icon(
icon
),
],
),
),
),
);
}
/// returns Radius
BorderRadius radius([double? radius]) {
return BorderRadius.all(radiusCircular(radius ?? defaultRadius));
}
/// returns Radius
Radius radiusCircular([double? radius]) {
return Radius.circular(radius ?? defaultRadius);
}
}
You cast Icon as IconData. But Icon isn't an IconData so you can't cast it. Actually you don't need to cast it just remove Icon widget and only pass IconData.
This line causes the problem:
gradientCardSample("عشق", 12, context, const Icon(Icons.book, size: 20, color: Colors.blue,) as IconData),
You can fix the problem by replacing this code block with this:
gradientCardSample("عشق", 12, context, Icons.book),
Because you want IconData at gradientCardSample. But you call this widget by passing Icon widget. You have to pass IconData here.