We have a widget class that has a final property of this.type
. Currently this is a String
and if the String
matches one of the available types it returns the correct content via that widget’s builder.
Is there a way that instead of asking for a String
, we ask for one of the available potential options.
Eg. Only Block
, Fixed
or None
are acceptable strings. Can we make sure that the use of this widget
can only accept those terms.
CodePudding user response:
You can use enums. First declare an enum using the enum
keyword:
enum AcceptableOptions { Block, Fixed, None }
Then in your widget, use AcceptableOptions
instead of String
class TestWidget extends StatelessWidget {
const TestWidget({
required AcceptableOptions option,
});
@override
Widget build(BuildContext context) {
return Container();
}
}
TestWidget(option:AcceptableOptions.Block)