Reputation: 24980
I am using extension to give padding to the widget
Extension:
extension Hello on Widget {
paddingAll(int x) {
return Container(
padding: const EdgeInsets.all(x.toDouble()),
child: this,
);
}
Use Case :
Container( child: Text("Hello")).paddingAll(40);
But this evaluates to :
return Container(
padding: const EdgeInsets.all(20),
child: Container(
child: Text("Hello"),
));
What I want is :
return Container(
padding: const EdgeInsets.all(20),
child: Text("Hello"),
);
How to achieve this via extension function
? If any further more simplified method ,please suggest the same .
Upvotes: 0
Views: 70
Reputation: 29
you can do like this
extension Hello on Container { // <==== change Widget to Container
paddingAll(int x) {
return Container(
padding: EdgeInsets.all(x.toDouble()),
child: child, // <==== here
);
}
}
this ===> Container
child ====> child of Container ====> Text
Upvotes: -2
Reputation: 13348
try this
extension Hello on Widget {
paddingAll(int x) {
return Padding(
padding: EdgeInsets.all(x.toDouble()),
child: this
);
}
}
then you should call like this
Container(color: Colors.red, child:Text("Padding").paddingAll(20))
Container(color: Colors.red, child: Text("Normal")).paddingAll(20)
this is the difference
Upvotes: 1