flutter foundation的TextTreeConfiguration

TextTreeConfiguration这个类没有方法,只有一些属性,它是做什么用的呢,我们先看一下:

 <root_name>: <root_description>
  │ <property1>
  │ <property2>
  │ ...
  │ <propertyN>
  ├─<child_name>: <child_description>
  │ │ <property1>
  │ │ <property2>
  │ │ ...
  │ │ <propertyN>
  │ │
  │ └─<child_name>: <child_description>
  │     <property1>
  │     <property2>
  │     ...
  │     <propertyN>
  │
  └─<child_name>: <child_description>'
    <property1>
    <property2>
    ...
    <propertyN>

是不是很熟悉,就是我们在devtool里看到的Flutter Inspector看到的呈现。
属性分别有:
prefixLineOne(第一行的前缀)
prefixOtherLines(其他行前缀)
prefixLastChildLineOne(在第一行添加前缀,以显示最后一个节点有这种样式)
prefixOtherLinesRootNode(如果这个节点是根节点,其他行有这个前缀)
linkCharacter(这个是链接符号,类似"└─"这种,从父到子的链接)
propertyPrefixIfChildren(如果节点是子节点,就在每个property前面添加类似于linkCharacter的prefix)
propertyPrefixNoChildren(如果改节点没有子节点,就在每个property前面添加的前缀,一般默认是空格的)
-----------------------以上是required
lineBreak(换行,一般默认值是"\n")
lineBreakProperties (即上面展示的property是换行,还是在一行展示,上面展示的就是换行的)
afterName(节点名字后面的添加的内容,一般就是如上所示的“:”)
afterDescriptionIfBody(如果有主体情况下,description后面添加的文本)
afterDescription(description后面添加文本)
beforeProperties(在Properties前面添加文本)
afterProperties(在Properties后面添加文本)
mandatoryAfterProperties(在Properties后面强制添加文本,即使没有properties)
propertySeparator(property之间的分割符)
bodyIndent(这个是对body 内所有行加一个前缀,除了name和description,所有的内容都是body)
footer(在非根节点的最后添加一行文本,即注脚)
showChildren(是否显示该节点的所有children)
addBlankLineIfNoChildren(如果没有子节点,是否在末尾添加一个空白行)
isNameOnOwnLine(name相对于description是否要独占一行)
isBlankLineBetweenPropertiesAndChildren(是否在properties和children之间添加一个空白行)
beforeName(在name前面添加前缀)
suffixLineOne(在第一行后面添加文本)
mandatoryFooter(即使是根节点,在最后添加一行文本,即注脚)
childLinkSpace(如果这个节点是父节点的最后一个节点,所以它的body就不需要绘制链接符了)

下面说一下我们常看到的几种TextTreeConfiguration示例化对象:

  1. 一般默认的就是sparseTextConfiguration对象,示例化的配置如下:
final TextTreeConfiguration sparseTextConfiguration = TextTreeConfiguration(
  prefixLineOne:            '├─',
  prefixOtherLines:         ' ',
  prefixLastChildLineOne:   '└─',
  linkCharacter:            '│',
  propertyPrefixIfChildren: '│ ',
  propertyPrefixNoChildren: '  ',
  prefixOtherLinesRootNode: ' ',
);

效果展示:

 <root_name>: <root_description>
  │ <property1>
  │ <property2>
  │ ...
  │ <propertyN>
  ├─<child_name>: <child_description>
  │ │ <property1>
  │ │ <property2>
  │ │ ...
  │ │ <propertyN>
  │ │
  │ └─<child_name>: <child_description>
  │     <property1>
  │     <property2>
  │     ...
  │     <propertyN>
  │
  └─<child_name>: <child_description>'
    <property1>
    <property2>
    ...
    <propertyN>

其实和最上面的图一样

  1. dashedTextConfiguration对象基本和sparseTextConfiguration一样,只是在父节点和子节点的链接上是虚线的。示例化的配置如下:
final TextTreeConfiguration dashedTextConfiguration = TextTreeConfiguration(
  prefixLineOne:            '╎╌',
  prefixLastChildLineOne:   '└╌',
  prefixOtherLines:         ' ',
  linkCharacter:            '╎',
  // Intentionally not set as a dashed line as that would make the properties
  // look like they were disabled.
  propertyPrefixIfChildren: '│ ',
  propertyPrefixNoChildren: '  ',
  prefixOtherLinesRootNode: ' ',
);

效果图如下:

 <root_name>: <root_description>
  │ <property1>
  │ <property2>
  │ ...
  │ <propertyN>
  ├─<normal_child_name>: <child_description>
  ╎ │ <property1>
  ╎ │ <property2>
  ╎ │ ...
  ╎ │ <propertyN>
  ╎ │
  ╎ └─<child_name>: <child_description>
  ╎     <property1>
  ╎     <property2>
  ╎     ...
  ╎     <propertyN>
  ╎
  ╎╌<dashed_child_name>: <child_description>
  ╎ │ <property1>
  ╎ │ <property2>
  ╎ │ ...
  ╎ │ <propertyN>
  ╎ │
  ╎ └─<child_name>: <child_description>
  ╎     <property1>
  ╎     <property2>
  ╎     ...
  ╎     <propertyN>
  ╎
  └╌<dashed_child_name>: <child_description>'
    <property1>
    <property2>
    ...
    <propertyN>
  1. denseTextConfiguration对象在水平方向最小化的减少了空格,并且请注意properties的展示:
final TextTreeConfiguration denseTextConfiguration = TextTreeConfiguration(
  propertySeparator: ', ',
  beforeProperties: '(',
  afterProperties: ')',
  lineBreakProperties: false,
  prefixLineOne:            '├',
  prefixOtherLines:         '',
  prefixLastChildLineOne:   '└',
  linkCharacter:            '│',
  propertyPrefixIfChildren: '│',
  propertyPrefixNoChildren: ' ',
  prefixOtherLinesRootNode: '',
  addBlankLineIfNoChildren: false,
  isBlankLineBetweenPropertiesAndChildren: false,
);

效果如下:

 <root_name>: <root_description>(<property1>; <property2> <propertyN>)
 ├<child_name>: <child_description>(<property1>, <property2>, <propertyN>)
 └<child_name>: <child_description>(<property1>, <property2>, <propertyN>)

4.transitionTextConfiguration这个对叶节点周围做一个配置,如下展示的就是在节点周围画一个清晰的边框。

final TextTreeConfiguration transitionTextConfiguration = TextTreeConfiguration(
  prefixLineOne:           '╞═╦══ ',
  prefixLastChildLineOne:  '╘═╦══ ',
  prefixOtherLines:         ' ║ ',
  footer:                   ' ╚═══════════',
  linkCharacter:            '│',
  // Subtree boundaries are clear due to the border around the node so omit the
  // property prefix.
  propertyPrefixIfChildren: '',
  propertyPrefixNoChildren: '',
  prefixOtherLinesRootNode: '',
  afterName:                ' ═══',
  // Add a colon after the description if the node has a body to make the
  // connection between the description and the body clearer.
  afterDescriptionIfBody: ':',
  // Members are indented an extra two spaces to disambiguate as the description
  // is placed within the box instead of along side the name as is the case for
  // other styles.
  bodyIndent: '  ',
  isNameOnOwnLine: true,
  // No need to add a blank line as the footer makes the boundary of this
  // subtree unambiguous.
  addBlankLineIfNoChildren: false,
  isBlankLineBetweenPropertiesAndChildren: false,
);

效果如下:

  <parent_node>
  ╞═╦══ <name> ═══
  │ ║  <description>:
  │ ║    <body>
  │ ║    ...
  │ ╚═══════════
  ╘═╦══ <name> ═══
    ║  <description>:
    ║    <body>
    ║    ...
    ╚═══════════
  1. errorTextConfiguration这个是在一个节点周围画边框而不管它的父节点:
final TextTreeConfiguration errorTextConfiguration = TextTreeConfiguration(
  prefixLineOne:           '╞═╦',
  prefixLastChildLineOne:  '╘═╦',
  prefixOtherLines:         ' ║ ',
  footer:                   ' ╚═══════════',
  linkCharacter:            '│',
  // Subtree boundaries are clear due to the border around the node so omit the
  // property prefix.
  propertyPrefixIfChildren: '',
  propertyPrefixNoChildren: '',
  prefixOtherLinesRootNode: '',
  beforeName:               '══╡ ',
  suffixLineOne:            ' ╞══',
  mandatoryFooter:          '═════',
  // No need to add a blank line as the footer makes the boundary of this
  // subtree unambiguous.
  addBlankLineIfNoChildren: false,
  isBlankLineBetweenPropertiesAndChildren: false,
);

效果如下:

══╡ <name>: <description> ╞═════════════════════════════════════
<body>
...
 ├─<normal_child_name>: <child_description>
 ╎ │ <property1>
 ╎ │ <property2>
 ╎ │ ...
 ╎ │ <propertyN>
 ╎ │
 ╎ └─<child_name>: <child_description>
 ╎     <property1>
 ╎     <property2>
 ╎     ...
 ╎     <propertyN>
 ╎
 ╎╌<dashed_child_name>: <child_description>
 ╎ │ <property1>
 ╎ │ <property2>
 ╎ │ ...
 ╎ │ <propertyN>
 ╎ │
 ╎ └─<child_name>: <child_description>
 ╎     <property1>
 ╎     <property2>
 ╎     ...
 ╎     <propertyN>
 ╎
 └╌<dashed_child_name>: <child_description>
════════════════════════════════════════════════════════════════

6.whitespaceTextConfiguration这个就是把很多属性设置成空格,很好理解。对象的配置如下:

final TextTreeConfiguration whitespaceTextConfiguration = TextTreeConfiguration(
  prefixLineOne: '',
  prefixLastChildLineOne: '',
  prefixOtherLines: ' ',
  prefixOtherLinesRootNode: '  ',
  bodyIndent: '',
  propertyPrefixIfChildren: '',
  propertyPrefixNoChildren: '',
  linkCharacter: ' ',
  addBlankLineIfNoChildren: false,
  // Add a colon after the description and before the properties to link the
  // properties to the description line.
  afterDescriptionIfBody: ':',
  isBlankLineBetweenPropertiesAndChildren: false,
);

效果如下:

 <parent_node>
   <name>: <description>:
     <properties>
     <children>
   <name>: <description>:
     <properties>
     <children>

7.flatTextConfiguration对象的配置参数基本就是空字符串,配置如下:

final TextTreeConfiguration flatTextConfiguration = TextTreeConfiguration(
  prefixLineOne: '',
  prefixLastChildLineOne: '',
  prefixOtherLines: '',
  prefixOtherLinesRootNode: '',
  bodyIndent: '',
  propertyPrefixIfChildren: '',
  propertyPrefixNoChildren: '',
  linkCharacter: '',
  addBlankLineIfNoChildren: false,
  // Add a colon after the description and before the properties to link the
  // properties to the description line.
  afterDescriptionIfBody: ':',
  isBlankLineBetweenPropertiesAndChildren: false,
);

效果如下:

<parent_node>
<name>: <description>:
<properties>
<children>
<name>: <description>:
<properties>
<children>
  1. singleLineTextConfiguration这个对象就是展示结果就是一个字符串,配置如下:
final TextTreeConfiguration singleLineTextConfiguration = TextTreeConfiguration(
  propertySeparator: ', ',
  beforeProperties: '(',
  afterProperties: ')',
  prefixLineOne: '',
  prefixOtherLines: '',
  prefixLastChildLineOne: '',
  lineBreak: '',
  lineBreakProperties: false,
  addBlankLineIfNoChildren: false,
  showChildren: false,
  propertyPrefixIfChildren: '  ',
  propertyPrefixNoChildren: '  ',
  linkCharacter: '',
  prefixOtherLinesRootNode: '',
);

效果如下:

<name>: <description>(<property1>, <property2>, ..., <propertyN>)
  1. errorPropertyTextConfiguration这个对象就是把name设为一行,把body部分只设为另一行,配置如下:
final TextTreeConfiguration errorPropertyTextConfiguration = TextTreeConfiguration(
  propertySeparator: ', ',
  beforeProperties: '(',
  afterProperties: ')',
  prefixLineOne: '',
  prefixOtherLines: '',
  prefixLastChildLineOne: '',
  lineBreak: '\n',
  lineBreakProperties: false,
  addBlankLineIfNoChildren: false,
  showChildren: false,
  propertyPrefixIfChildren: '  ',
  propertyPrefixNoChildren: '  ',
  linkCharacter: '',
  prefixOtherLinesRootNode: '',
  afterName: ':',
  isNameOnOwnLine: true,
);

效果如下:

 <name>:
   <description>(<property1>, <property2>, ..., <propertyN>)
  1. shallowTextConfiguration这个对象渲染一个node,用很浅和很简单的方式,配置如下:
final TextTreeConfiguration shallowTextConfiguration = TextTreeConfiguration(
  prefixLineOne: '',
  prefixLastChildLineOne: '',
  prefixOtherLines: ' ',
  prefixOtherLinesRootNode: '  ',
  bodyIndent: '',
  propertyPrefixIfChildren: '',
  propertyPrefixNoChildren: '',
  linkCharacter: ' ',
  addBlankLineIfNoChildren: false,
  // Add a colon after the description and before the properties to link the
  // properties to the description line.
  afterDescriptionIfBody: ':',
  isBlankLineBetweenPropertiesAndChildren: false,
  showChildren: false,
);

效果如下:

 <name>: <description>
   <property1>
   <property2>
   <propertyN>

这篇文章主要就是分析了TextTreeConfiguration的源码,还有它实例出来的对象,其中有很多是我们在开发过程都会看到的,这是解读flutter foundation的diagnostic中的一篇。以上内容,如有错漏之处,还望斧正。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容