class IconContent extends StatelessWidget {
const IconContent({Key? key, required this.icon, required this.label})
: super(key: key);
final IconData icon;
final String label;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
icon,
size: 80.0,
),
SizedBox(
height: 15.0,
),
Text(
label,
style: TextStyle(
fontSize: 18.0,
color: Color(
0xFF8D8E98,
),
),
),
],
);
}
}
Im getting the error stated below multiple times. Can someone help me resolve them.
Arguments of a constant creation must be constant expressions.\nTry making the argument a valid constant, or use 'new' to call the constructor.
This is the code Im getting the errors on. Removing const didnt do anything :(
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
const Color midNightLight = Color(0xFF1D1E33);
class InputPage extends StatefulWidget {
const InputPage({super.key});
@override
State<InputPage> createState() => _InputPageState();
}
class _InputPageState extends State<InputPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Center(
child: Text('BMI Calculator'),
),
),
body: SafeArea(
child: Column(
children: [
Expanded(
child: Row(
// ignore: prefer_const_literals_to_create_immutables
children: [
// ignore: prefer_const_constructors
Expanded(
flex: 2,
// ignore: prefer_const_constructors
child: ReusableCard(
colour: midNightLight,
// ignore: prefer_const_constructors
cardChild: IconContent(
icon: FontAwesomeIcons.mars,
label: 'MALE',
),
),
),
// ignore: prefer_const_constructors
Expanded(
flex: 2,
// ignore: prefer_const_constructors
child: ReusableCard(
colour: midNightLight,
// ignore: prefer_const_constructors
cardChild: new IconContent(
icon: FontAwesomeIcons.venus,
label: 'FEMALE',
),
),
)
],
),
),
Expanded(
child: Row(
// ignore: prefer_const_literals_to_create_immutables
children: [
// ignore: prefer_const_constructors
Expanded(
flex: 2,
// ignore: prefer_const_constructors
child: ReusableCard(
colour: midNightLight,
),
),
],
),
),
Expanded(
child: Row(
// ignore: prefer_const_literals_to_create_immutables
children: [
// ignore: prefer_const_constructors
Expanded(
flex: 2,
// ignore: prefer_const_constructors
child: ReusableCard(
colour: midNightLight,
),
),
// ignore: prefer_const_constructors
Expanded(
flex: 2,
// ignore: prefer_const_constructors
child: ReusableCard(
colour: midNightLight,
),
)
],
),
),
],
),
),
);
}
}
class IconContent extends StatelessWidget {
const IconContent({Key? key, required this.icon, required this.label})
: super(key: key);
final IconData icon;
final String label;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Icon(
icon,
size: 80.0,
),
SizedBox(
height: 15.0,
),
Text(
label,
style: TextStyle(
fontSize: 18.0,
color: Color(
0xFF8D8E98,
),
),
),
],
);
}
}
class ReusableCard extends StatelessWidget {
const ReusableCard({
Key? key,
required this.colour,
this.cardChild,
}) : super(key: key);
final Color? colour;
final Widget? cardChild;
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(15.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: colour,
),
child: cardChild,
);
}
}
CodePudding user response:
label
will get on runtime, therefore it cant be const
class IconContent extends StatelessWidget {
const IconContent({
Key? key,
required this.icon,
required this.label,
}) : super(key: key);
final IconData icon;
final String label;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//remove const from here
Icon(
icon,
size: 80.0,
),
SizedBox(
height: 15.0,
),
Text(
label,
style: TextStyle(
fontSize: 18.0,
color: Color(
0xFF8D8E98,
),
),
),
],
);
}
}
CodePudding user response:
Just remove the const
from Column's
children list. your lable
and icon
variable is not const and its value changes in different state because of that you can't use const
for Column's
children list. You also need to delete all those const
from Row's
children inside Expanded
too.