Files
pipelines/app/lib/features/chat/ui/transcript/native_disclosure.dart
T

83 lines
2.6 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../sessions/chat_adaptive.dart';
/// A block opened with the host's own disclosure control (collapsed by default).
///
/// [childBuilder] runs only while the block is open, so a closed disclosure
/// never encodes or lays out its body.
class NativeDisclosure extends StatefulWidget {
const NativeDisclosure({
super.key,
required this.label,
required this.childBuilder,
this.initiallyExpanded = false,
});
final String label;
final WidgetBuilder childBuilder;
final bool initiallyExpanded;
@override
State<NativeDisclosure> createState() => _NativeDisclosureState();
}
class _NativeDisclosureState extends State<NativeDisclosure> {
late bool _expanded = widget.initiallyExpanded;
void _toggle() => setState(() => _expanded = !_expanded);
@override
Widget build(BuildContext context) {
if (chatControlsStyleOf(context) != ChatControlsStyle.cupertino) {
return ExpansionTile(
title: Text(widget.label),
initiallyExpanded: widget.initiallyExpanded,
maintainState: false,
// A transcript block starts at the row edge, like its answer text; the
// tile's own padding would indent it, and its body defaults to centred,
// which floats a short body in the middle of the row.
tilePadding: EdgeInsets.zero,
expandedAlignment: AlignmentDirectional.centerStart,
expandedCrossAxisAlignment: CrossAxisAlignment.stretch,
children: [Builder(builder: widget.childBuilder)],
);
}
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// One node carries the label, the button action and the state; the
// Cupertino button's own node would otherwise drop the state.
Semantics(
container: true,
button: true,
expanded: _expanded,
label: widget.label,
onTap: _toggle,
child: ExcludeSemantics(
child: CupertinoButton(
padding: EdgeInsets.zero,
onPressed: _toggle,
child: Row(
children: [
Expanded(
child: Text(widget.label, textAlign: TextAlign.start),
),
Icon(
_expanded
? CupertinoIcons.chevron_up
: CupertinoIcons.chevron_down,
),
],
),
),
),
),
if (_expanded) widget.childBuilder(context),
],
);
}
}