Reputation: 334
I know that in Flutter composition is preferred over inheritance but for one use case I need my class to extend Text
, since parameter only accepts Text
classes & not all Widgets
Thing is I need to deal with States in this new class there for I was wondering if I can have
this class NeedsToBeStateful extends Text {}
be something of Text extends StatefulWidget
rather than class Text extends StatelessWidget
I would also need the Text
data
to be updated by the State
class NeedsToBeStateful extends Text {
NeedsToBeStateful(
{super.data = dataUpdatedByState}
);
@override
Text build(BuildContext context) {
return Text(
data,
);
}
}
I was thinking of doing something like this but it didn't work
class NeedsToBeStateful extends Text with StatefulWidget{
NeedsToBeStateful(
{super.data = dataUpdatedByState}
);
@override
Text build(BuildContext context) {
return Text(
data,
);
}
}
Upvotes: 1
Views: 619
Reputation: 23164
Not sure if this works or if it's something you can work with, but maybe something like this is possible
class MyText extends Text {
MyText(super.data);
@override
Widget build(BuildContext context) {
return MyState(child: super.build(context));
}
}
class MyState extends StatefulWidget {
final Widget child;
const MyState({Key? key, required this.child}) : super(key: key);
@override
State<MyState> createState() => _State();
}
class _State extends State<MyState> {
@override
Widget build(BuildContext context) {
return widget.child;
}
}
Upvotes: 1