简体   繁体   English

Flutter - 基于 XML 文件的动态页面设计(通过 REST API)

[英]Flutter - Dynamic page design based on a XML file (over a REST API)

I have worked with dart and flutter for a few months.我与 dart 和 flutter 合作了几个月。 I need a little support regarding to the following task and how to start a project like this/ how to find a solution.我需要一些关于以下任务以及如何启动这样的项目/如何找到解决方案的支持。

The aim is to develop Flutter as an application which gets the page structure of every page from the XML file.目的是将 Flutter 开发为从 XML 文件中获取每个页面的页面结构的应用程序。 The page is structured in sections and those are structured in columns and rows.该页面以部分为结构,而这些部分以列和行为结构。 The app has predefined standard styles (styles for buttons labels etc...) which are callable in the XML file with the name.该应用程序具有预定义的标准 styles(按钮标签的样式等......),可在 XML 文件中使用名称进行调用。

The following example represents how a section in the XML file could look like.以下示例表示 XML 文件中的部分的外观。

<SectionTop>
<Column1>
 <Row1>
  <label>
    <title>label text</title>
    <algin>center</algin>
    </style>defaultLabelStyle</style>
  </label>
 </Row1>
 <Row2>
  <Button>
    <title>add Customer</title>
    <algin>center</algin>
    </style>defaultButton_Blue</style>
  </Button>
 </Row2>
</Column1>
</SectionTop>

I hope you can help me, thanks in advance:)我希望你能帮助我,在此先感谢:)

There is a package called Dynamic Widget however it uses its own schema and is json.有一个名为Dynamic Widget的 package 但是它使用自己的模式并且是 json。 If you are wanting control over the xml yourself and its schema then you will need to build something like the Dynamic Widget package如果您想自己控制 xml 及其架构,那么您将需要构建Dynamic Widget package 之类的东西

There is also Xml Layout package that could get you started in thinking of how to put it together.还有Xml 布局package 可以让您开始思考如何将它们组合在一起。

And also Flutter XML Layout Helper还有Flutter XML 布局助手

There is also a plugin for VSCode which you could look at as to how it is done还有一个VSCode插件,你可以看看它是如何完成的

You can use this package您可以使用此 package

https://pub.dev/packages/xml https://pub.dev/packages/xml

To read xml from file从文件中读取 xml

final document = XmlDocument.parse(File('bookshelf.xml').readAsStringSync());

To read xml from api从 api 读取 xml

//get response string user http or dio package
final document = XmlDocument.parse(responseString);

elements on the xml document can be iterated and widgets can be populated in build method as follows xml 文档上的元素可以迭代,并且可以在构建方法中填充小部件,如下所示

@override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: _getChildren(document.descendants),
      ),
    );
  }

  _getChildren(Iterable<XmlNode> descendants) {
    List<Widget> widgets = List();
    for (XmlNode node in descendants) {
      if (node.text == 'Column') {
        widgets.add(Column(
          children: _getChildren(node.descendants),
        ));
      } else if (node.text == 'Row') {
        widgets.add(Row(
          children: _getChildren(node.descendants),
        ));
      } else if (node.text == 'label') {
        String text = node.descendants
            .firstWhere((element) => element.text == 'title')
            .innerText;
        String align = node.descendants
                .firstWhere((element) => element.text == 'align')
                .innerText ??
            'center';

        widgets.add(Text(text,
            textAlign: align == 'center'
                ? TextAlign.center
                : align == 'start'
                    ? TextAlign.start
                    : TextAlign.end));
      }
      //add more if else for other elements
    }
    return widgets;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM