directory_tree
Turn a list of file paths into an immutable, indexed directory tree, then
show it: flattened rows for a virtualized list, search with highlighting,
selection with tri-state checkboxes, diffs for animated lists, and text
output like the tree command.
The package is pure Dart. It never touches the file system unless you import
the optional scanner, so it runs the same on the VM, in Flutter apps, and on
the web (JavaScript and WebAssembly). Use it for file explorers, IDE-style
side panels, file pickers, repository viewers, and CLI tree printers.
The same paths in any order give an equal tree with the same node order and
the same node ids, so ids can key widget state, expansion, and selection
across rebuilds. Order matters only when two paths name the same node (such
as README.md and readme.md in a case-insensitive tree): the later one
wins and keeps its spelling.
Coding agents can learn the 2.0.0 API and the upgrade from 1.x from the
plugin for AI coding assistants (Claude
Code, Codex, and Antigravity).
Install
dart pub add directory_tree
In a Flutter project, flutter pub add directory_tree does the same.
import 'package:directory_tree/directory_tree.dart';
Quick start
Build a tree from paths and print it:
final tree = DirectoryTree.fromPaths([
'/repo/lib/src/tree.dart',
'/repo/lib/main.dart',
'/repo/test/tree_test.dart',
'/repo/README.md',
]);
print(tree.render());
repo/
├── lib/
│ ├── src/
│ │ └── tree.dart
│ └── main.dart
├── test/
│ └── tree_test.dart
└── README.md
Directories come before files, and names sort naturally without regard to
case. lib and src were never listed: a directory that contains an entry
is created for you.
A UI shows the tree as rows. flatten returns the visible rows for an
expansion state, and toggleNode updates them when the user clicks an
arrow:
var rows = tree.flatten(expansion: const ExpansionState.collapsed());
rows = rows.toggleNode(rows.first.id); // the user opened "repo"
for (final row in rows) {
final arrow = row.hasChildren ? (row.isExpanded ? 'v ' : '> ') : ' ';
print('${' ' * row.depth}$arrow${row.label}');
}
v repo
> lib
> test
README.md
TreeRows is an unmodifiable List<TreeRow>, so in Flutter it plugs into
ListView.builder(itemCount: rows.length, ...), with row.id as the key.
The examples below keep using this tree.
Core concepts
Paths and PathPolicy
Each raw path is parsed into a canonical TreePath. Parsing is pure string
work and does not depend on the host, so POSIX paths, Windows drive paths,
and UNC paths parse the same on Linux, macOS, Windows, and the web:
print(TreePath.parse(r'C:\work\..\repo\')); // C:/repo
print(TreePath.parse(r'\\server\share\a.txt').root); // //server/share/
print(TreePath.parse('a//./b/')); // a/b
print(TreePath.tryParse('../outside')); // null
Separators become /, empty and . segments are dropped, .. is resolved,
and drive letters are uppercased. A relative path that climbs above its
start is invalid. The full rules are on PathPolicy.parse.
A PathPolicy decides three things for a whole tree, through
TreeOptions.paths:
caseSensitive (default true): whether README.md and readme.md are
different nodes. Use PathPolicy.caseInsensitive for trees that mirror
Windows or default macOS volumes.
backslashIsSeparator (default true): set it to false only when your
POSIX file names contain literal backslashes. DirectoryScanner.scanTree
does this for you off Windows.
unicodeNormalizer (default none): a function applied to every raw path,
typically NFC normalization, so names created on macOS (often NFD) match
names from other sources.
const paths = ['/Repo/README.md', '/repo/readme.md'];
print(DirectoryTree.fromPaths(paths).fileCount); // 2
final folded = DirectoryTree.fromPaths(
paths,
options: const TreeOptions(paths: PathPolicy.caseInsensitive),
);
print(folded.fileCount); // 1
print(folded.files.single.path); // /repo/readme.md
A case-insensitive tree keeps a display spelling for each node; only
identity ignores case. Case is ignored with Unicode simple case folding,
pinned to Unicode 18.0.0 and identical on the VM, JavaScript, and
WebAssembly, so ΟΔΟΣ and οδος are one node while İstanbul and
istanbul stay two (İ has no simple folding). Search (TreeQuery) and
the default sibling order fold case the same way.
Entries with typed data
DirectoryTree.fromPaths is the quick way in. To attach data to nodes, build
from TreeEntry values with a type argument: