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) {
if (text.isEmpty) {
return Text('');
} else {
//print('text: $text');
List<InlineSpan> children = [];
if (pre != null) {
children.add(pre!);
}
Set set = Set();
bool containsNewLine = text.contains(r'\n');
log('Contains new line: $containsNewLine');
List<String> linesList = [];
if (containsNewLine) {
linesList = text.split(r'\n');
log("lines=${linesList.length}: $linesList");
} else {
linesList.add(text);
}
// Apply modifications into all lines
for (int k = 0; k < linesList.length; k++) {
log('Line ${k + 1}: ${linesList[k]}');
// split into array
List<String> spanList =
linesList[k].split(RegExp(chars ?? r"[*~/_\\]"));
log("len=${spanList.length}: $spanList");
if (spanList.length == 1) {
log("trivial");
if (style == null) {
children.add(TextSpan(text: ''));
// If no last line:
if (k < linesList.length - 1) children.add(TextSpan(text: '\n'));
} else {
children.add(TextSpan(text: linesList[k], style: style));
// If no last line:
if (k < linesList.length - 1) children.add(TextSpan(text: '\n'));
}
} else {
int i = 0;
bool acceptNext = true;
String? cmd;
void wrap(String v) {
log("wrap: $v set=$set");
Map<String, String> map = {};
if (cmd != null) {
var pairs = cmd.split(';');
for (var pair in pairs) {
var a = pair.split(':');
if (a.length == 2) {
map[a[0].trim()] = a[1].trim();
} else {
throw "attribute value is missing a value (e.g., you passed {key} but not {key:value}";
}
}
log("attributes: $map");
}
// TextDecorationStyle "values" is ignored
TextDecorationStyle? _textDecorationStyle;
if (map.containsKey('decorationStyle')) {
if (map['decorationStyle'] == 'dashed')
_textDecorationStyle = TextDecorationStyle.dashed;
if (map['decorationStyle'] == 'double')
_textDecorationStyle = TextDecorationStyle.double;
if (map['decorationStyle'] == 'dotted')
_textDecorationStyle = TextDecorationStyle.dotted;
if (map['decorationStyle'] == 'solid')
_textDecorationStyle = TextDecorationStyle.solid;
if (map['decorationStyle'] == 'wavy')
_textDecorationStyle = TextDecorationStyle.wavy;
}
TextStyle ts;
ts = style!.copyWith(
color: map.containsKey('color')
? parseColor(map['color']!)
: style!.color,
decoration: set.contains('_')
? TextDecoration.underline
: TextDecoration.none,
fontStyle:
set.contains('/') ? FontStyle.italic : FontStyle.normal,
fontWeight:
set.contains('*') ? FontWeight.bold : FontWeight.normal,
fontSize: map.containsKey('fontSize')
? double.parse(map['fontSize']!)
: style!.fontSize,
fontFamily: map.containsKey('fontFamily')
? '${map['fontFamily']}'
: style!.fontFamily,
backgroundColor: map.containsKey('backgroundColor')
? parseColor(map['backgroundColor']!)
: style!.backgroundColor,
decorationColor: map.containsKey('decorationColor')
? parseColor(map['decorationColor']!)
: style!.decorationColor,
decorationStyle: _textDecorationStyle ?? style!.decorationStyle,
decorationThickness: map.containsKey('decorationThickness')
? double.parse(map['decorationThickness']!)
: style!.decorationThickness,
height: map.containsKey('height')
? double.parse(map['height']!)
: style!.height,
letterSpacing: map.containsKey('letterSpacing')
? double.parse(map['letterSpacing']!)
: style!.letterSpacing,
wordSpacing: map.containsKey('wordSpacing')
? double.parse(map['wordSpacing']!)
: style!.wordSpacing,
);
if (map.containsKey('pop') ||
map.containsKey('push') ||
map.containsKey('repl') ||
map.containsKey('http')) {
// print("BBB cmd=$cmd");
// GestureDetector
// children.add(WidgetSpan(child: Text('****')));
// children.add(WidgetSpan(
// child: GestureDetector(
// child: Text('CLICK'),
// onTap: () async {
// //print("TAPPED");
// },
// )));
// assert(context != null, 'must pass context if using route links');
children.add(TextSpan(
text: v,
// Beware! This class is only safe because the TapGestureRecognizer is not given a deadline and therefore never allocates any resources.
// In any other situation -- setting a deadline, using any of the less trivial recognizers, etc -- you would have to manage the gesture recognizer's lifetime
// and call dispose() when the TextSpan was no longer being rendered.
// Since TextSpan itself is @immutable, this means that you would have to manage the recognizer from outside
// the TextSpan, e.g. in the State of a stateful widget that then hands the recognizer to the TextSpan.
style: ts));
} else {
children.add(TextSpan(text: v, style: ts));
}
}
void toggle(String m) {
if (m == r'\') {
String c = linesList[k].substring(i + 1, i + 2);
log("quote: i=$i: $c");
wrap(c);
acceptNext = false;
} else {
if (acceptNext) {
if (set.contains(m)) {
log("REM: $m");
set.remove(m);
} else {
log("ADD: $m");
set.add(m);
}
}
acceptNext = true;
}
}
for (var v in spanList) {
log("========== $v ==========");
cmd = null; //TRY
if (v.isEmpty) {
if (i < linesList[k].length) {
String m = linesList[k].substring(i, i + 1);
toggle(m);
i++;
}
} else {
int adv = v.length;
if (v[0] == '{') {
log("link: $v");
int close = v.indexOf('}');
if (close > 0) {
cmd = v.substring(1, close);
log("AAA cmd=$cmd");
v = v.substring(close + 1);
log("remaining: $v");
}
}
wrap(v);
i += adv;
if (i < linesList[k].length) {
String m = linesList[k].substring(i, i + 1);
log("*** format: $m");
toggle(m);
i++;
}
}
}
if ((fussy ?? false) && set.isNotEmpty) {
throw 'simple_rich_text: not closed: $set'; //TODO: throw real error?
}
// If no last line:
if (k < linesList.length - 1) children.add(TextSpan(text: '\n'));
}
}
if (post != null) {
children.add(post!);
}
return RichText(
maxLines: this.maxLines ?? null,
overflow: this.textOverflow ?? TextOverflow.clip,
text: TextSpan(children: children),
textAlign: this.textAlign ?? TextAlign.start,
textScaleFactor:
this.textScaleFactor ?? MediaQuery.of(context).textScaleFactor);
} // else
}