import 'package:flutter/material.dart'; import '../../../ui/adaptive/floating_bar_layout.dart'; import '../domain/change_wording.dart'; import '../domain/file_change.dart'; import '../domain/unified_patch.dart'; import 'capture_section.dart' show changeKindIcon; import 'change_style.dart'; import 'diff_highlight.dart'; import 'patch_view.dart'; /// Every file of a diff one below the other, like a pull request's Files /// changed tab: each file has a header that collapses or expands its patch. /// [headerKeys] (one per file) let the page jump between files. class StackedDiffView extends StatelessWidget { const StackedDiffView({ super.key, required this.files, required this.collapsed, required this.onToggle, required this.controller, required this.headerKeys, }); final List files; final Set collapsed; final ValueChanged onToggle; final ScrollController controller; final List headerKeys; @override Widget build(BuildContext context) { return SingleChildScrollView( key: const ValueKey('stacked-diff'), controller: controller, padding: withTopBarClearance( context, const EdgeInsets.fromLTRB(8, 8, 8, 96), ), child: Align( alignment: Alignment.topCenter, child: ConstrainedBox( constraints: const BoxConstraints(maxWidth: 1100), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ for (var index = 0; index < files.length; index++) Padding( padding: const EdgeInsets.only(bottom: 12), child: _FileSection( key: ValueKey('stacked-diff-file-${files[index].path}'), headerKey: headerKeys[index], index: index, file: files[index], expanded: !collapsed.contains(index), onToggle: () => onToggle(index), ), ), ], ), ), ), ); } } class _FileSection extends StatelessWidget { const _FileSection({ super.key, required this.headerKey, required this.index, required this.file, required this.expanded, required this.onToggle, }); final GlobalKey headerKey; final int index; final FileChange file; final bool expanded; final VoidCallback onToggle; @override Widget build(BuildContext context) { final mono = nativeDiffText(DefaultTextStyle.of(context).style); final stat = changeStat(file.added, file.removed); final header = Semantics( key: headerKey, button: true, expanded: expanded, label: '${expanded ? 'Collapse' : 'Expand'} ${file.path}', excludeSemantics: true, onTap: onToggle, child: InkWell( onTap: onToggle, child: Padding( padding: const EdgeInsets.fromLTRB(8, 8, 12, 8), child: Row( children: [ Icon(expanded ? Icons.expand_more : Icons.chevron_right), const SizedBox(width: 4), Icon(changeKindIcon(file.kind), size: 20), const SizedBox(width: 8), Expanded( child: Text( file.path, style: mono, maxLines: 1, overflow: TextOverflow.ellipsis, ), ), if (stat.isNotEmpty) ...[ const SizedBox(width: 12), ChangeStatText(stat, style: mono), ], ], ), ), ), ); return Card.outlined( margin: EdgeInsets.zero, clipBehavior: Clip.antiAlias, child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ header, if (expanded) ...[ const Divider(height: 1), DiffFileIndex(index: index, child: _body(context)), ], ], ), ); } Widget _body(BuildContext context) { final reason = noPatchReasonMessage(file.noPatchReason); final message = file.binary && reason == null ? binaryDiffMessage : !file.hasPatch ? patchUnavailableReason(file) : null; if (message != null) { return Padding(padding: const EdgeInsets.all(12), child: Text(message)); } return PatchView(file: file); } }