【问题标题】:how to add a specific property to Widget without wrapping it as a child using extension in flutterhow to add a specific property to Widget without wrapping it as a child using extension in flutter
【发布时间】:2022-12-02 02:10:07
【问题描述】:

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 .

【问题讨论】:

  • what problem facing now?
  • I want to write an extension which evaluates to what i want is code. What i have now is the above one
  • you can simply check over it inside the extension method, if(this == Container)...

标签: flutter widget extension-methods


【解决方案1】:

try this

extension Hello on Widget {
  paddingAll(int x) {
  return Padding(
  padding: EdgeInsets.all(x.toDouble()),
    child: this
);
}
}

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2022-11-09
    • 2022-12-26
    • 2022-12-01
    • 2022-12-02
    • 2022-12-02
    • 2022-12-01
    • 2022-12-27
    • 2022-12-26
    相关资源
    最近更新 更多