【发布时间】:2022-11-20 13:40:17
【问题描述】:
I am trying to create a RoundedInputField as a StatelessWidget. I am still learning both Dart and Flutter but I am a bit stuck at the moment. So where it all started is that I want to optionally choose a prefixIcon from outside the class. I created a helper function buildInputDecorator in order to handle creation of InputDecoration based on iconData is set or not. I have a couple of compilation errors which I am not sure how to tackle. I have added the errors as cmets.
My code is:
import 'package:flutter/material.dart';
class RoundedInputField extends StatelessWidget {
final IconData? iconData;
const RoundedInputField({super.key, this.iconData});
InputDecoration buildInputDecorator(String hint) {
if (iconData != null) {
return const InputDecoration(
hintText: hint, //Invalid constant value.dart(invalid_constant)
prefixIcon: Icon(iconData), //Arguments of a constant creation must be constant expressions.
);
}
return const InputDecoration(
hintText: hint, //Invalid constant value.dart(invalid_constant)
);
}
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
color: Color.fromRGBO(73, 152, 203, 1),
),
padding: const EdgeInsets.fromLTRB(10.0, 0, 0, 10.0),
child: const TextField(
decoration: buildInputDecorator("Email"), //Invalid constant value.dart(invalid_constant)
),
);
}
}
【问题讨论】:
-
try remove
const -
Removing const produces the same error messages. Is this impossible to achieve without using a StatefullWidget?
标签: dart