首页 > 解决方案 > Flutter 从小部件生成 JSON

问题描述

我想让我的应用程序动态化。有没有办法可以在任何颤振应用程序中从我的小部件生成 JSON?

我找到了仅用于从 JSON 解析到小部件的解决方案,但我需要相反的方式。

标签: flutterdartmobile

解决方案


老实说,我不确定这是否是最好的方法......但我认为你可以使用类似下面的东西来手动生成 JSON ......

myPrint(dynamic widget){
  if(widget is Column || widget is Row || widget is Stack){
    for(dynamic w in widget.children){
      print(widget.toString());
      myPrint(w);
    }
  }else{
    print(widget.toString());
    try {
      myPrint(widget.child);
    } catch (e) {
    }
  }
}

例如下面的小部件...

var myWidget = Column(
  children: [
    Center(
      child: Text("word" , style: TextStyle(fontSize: 25),),
    ),
  ],
);   

像这样打印...

Column(direction: vertical, mainAxisAlignment: start, crossAxisAlignment: center)

Center(alignment: center)

Text("word", inherit: true, size: 25.0)

推荐阅读