WidgetPath.create constructor

WidgetPath.create(
  1. BuildContext? context, {
  2. bool shorten = true,
  3. bool hash = false,
  4. String exclude = excl,
})

Implementation

WidgetPath.create(this.context,
    {this.shorten = true, this.hash = false, String exclude = excl}) {
  if (context == null) {
    return;
  }

  final StringBuffer path = StringBuffer();
  final RegExp re = RegExp(exclude, multiLine: true);
  final List<dynamic> stk = [];
  final Widget widget = context!.widget;

  String widgetName = '$sep${widget.runtimeType.toString()}';
  Widget? child;

  this.path = '';

  context?.visitAncestorElements((ancestor) {
    final Widget parentWidget = ancestor.widget;
    String prt = parentWidget.runtimeType.toString();
    final String art = '${ancestor.runtimeType.toString()}\n$prt';

    if (stk.isEmpty) {
      parent = ancestor;
    }
    final String? parentPath = pathCache[parentWidget];
    if (parentPath != null) {
      path.write(parentPath);
      return false;
    }
    if (!re.hasMatch(art)) {
      if (child != null) {
        final int index = siblingPosition(ancestor, child!);
        if (index != -1) {
          prt += '_$index';
        }
      }
      stk.add(parentWidget);
      stk.add(makeShorter(prt));
    }
    child = ancestor.widget;
    return true;
  });

  for (int index = stk.length; index > 0;) {
    path.write('$sep${stk[--index]}');
    pathCache[stk[--index]] = path.toString();
  }

  path.write(widgetName);
  this.path = path.toString();
  parentWidgetType = parent!.widget.runtimeType.toString();

  tlLogger.v(
      'Widget path added: ${widget.runtimeType.toString()}, path: $this.path, digest: ${widgetDigest()}');
}