import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../sessions/chat_adaptive.dart'; /// A collapsed block opened with the host's own disclosure control. /// /// [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, }); final String label; final WidgetBuilder childBuilder; @override State createState() => _NativeDisclosureState(); } class _NativeDisclosureState extends State { bool _expanded = false; void _toggle() => setState(() => _expanded = !_expanded); @override Widget build(BuildContext context) { if (chatControlsStyleOf(context) != ChatControlsStyle.cupertino) { return ExpansionTile( title: Text(widget.label), 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), ], ); } }