build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
final effectiveStyle = style ?? NZMarkdownStyle.defaultStyle(context);
final lines = data.split('\n');
final List<Widget> widgets = [];
String? codeBlock;
List<String>? currentList;
for (int i = 0; i < lines.length; i++) {
final line = lines[i];
// 逻辑处理:优先检测代码块状态
if (line.trim().startsWith('```')) {
if (codeBlock == null) {
codeBlock = ''; // 进入代码块采集模式
} else {
widgets.add(_buildCodeBlock(codeBlock, effectiveStyle));
codeBlock = null; // 结束代码块采集模式
}
continue;
}
if (codeBlock != null) {
codeBlock += '$line\n';
continue;
}
// 逻辑处理:检测并采集列表项
if (line.trim().startsWith('- ') || line.trim().startsWith('* ')) {
currentList ??= [];
currentList.add(line.trim().substring(2));
continue;
} else if (currentList != null) {
// 如果当前行不是列表且采集器不为空,渲染之前采集的列表
widgets.add(_buildList(currentList, effectiveStyle));
currentList = null;
}
// 逻辑处理:处理空行(段落间距)
if (line.trim().isEmpty) {
widgets.add(const SizedBox(height: 12));
continue;
}
// 逻辑处理:处理各种块级元素
if (line.startsWith('# ')) {
widgets.add(_buildHeading(line.substring(2), effectiveStyle.h1!));
} else if (line.startsWith('## ')) {
widgets.add(_buildHeading(line.substring(3), effectiveStyle.h2!));
} else if (line.startsWith('### ')) {
widgets.add(_buildHeading(line.substring(4), effectiveStyle.h3!));
} else if (line.startsWith('> ')) {
widgets.add(_buildBlockquote(line.substring(2), effectiveStyle));
} else if (line.trim() == '---' || line.trim() == '***') {
widgets.add(const Divider(height: 32));
} else {
// 默认为普通段落,支持行内样式解析
widgets.add(_buildParagraph(line, effectiveStyle));
}
}
// 处理文件末尾未闭合的列表项
if (currentList != null) {
widgets.add(_buildList(currentList, effectiveStyle));
}
return Padding(
padding: effectiveStyle.padding ?? EdgeInsets.zero,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: widgets,
),
);
}