Reputation: 361
May I ask simple question that I'd like to define about color as externanl code from main dart as folloes. How ever it's not allowed and flutter says "A value of type 'Color' can't be returned from the method 'build' because it has a return type of 'Widget'"
I guess it's because that Color Class is not Widget and it could not be retured as widget.
class MainColor extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Color(0xFF4BB574);
}
}
I also understand that it can treat as funtion like
Color mainColor(){
return const Color(0xFF4BB574);
}
But how can I define as class like above and treat as external file ?
Upvotes: 1
Views: 192
Reputation: 1546
In here Color, type is 'Color' not Widget.You should return Widget type in build method.If you want to use Color,in different section you can define like this,
Class AppColors{
static const Color yourColorName = Color(0xFF4BB574);
}
your define color, can be access in the widget like follows
class TestWidget extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Container(
color: AppColors.yourColorName,
child:Text("Your widget"));
}
}
Upvotes: 2