import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; /// Which standard control family this screen renders. enum ChatControlsStyle { material, cupertino } /// Cupertino under a Cupertino app or an iOS/macOS Material theme, Material /// everywhere else. The shell decides the app class, so a bare Cupertino app /// never has to grow a Material ancestor just for this screen. ChatControlsStyle chatControlsStyleOf(BuildContext context) { if (context.findAncestorWidgetOfExactType() == null) { return ChatControlsStyle.cupertino; } final platform = Theme.of(context).platform; return platform == TargetPlatform.iOS || platform == TargetPlatform.macOS ? ChatControlsStyle.cupertino : ChatControlsStyle.material; } class ChatBarAction extends StatelessWidget { const ChatBarAction({ super.key, required this.label, required this.onPressed, required this.materialIcon, required this.cupertinoIcon, }); final String label; final VoidCallback onPressed; final IconData materialIcon; final IconData cupertinoIcon; @override Widget build(BuildContext context) { if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) { return Semantics( label: label, button: true, child: CupertinoButton( padding: EdgeInsets.zero, onPressed: onPressed, child: Icon(cupertinoIcon), ), ); } return IconButton( tooltip: label, onPressed: onPressed, icon: Icon(materialIcon), ); } } class ChatSearchField extends StatelessWidget { const ChatSearchField({ super.key, required this.controller, required this.onChanged, }); final TextEditingController controller; final ValueChanged onChanged; @override Widget build(BuildContext context) { if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) { return Padding( padding: const EdgeInsets.all(16), child: CupertinoSearchTextField( controller: controller, placeholder: 'Search chats', onChanged: onChanged, ), ); } return Padding( padding: const EdgeInsets.all(16), child: TextField( controller: controller, onChanged: onChanged, decoration: const InputDecoration( prefixIcon: Icon(Icons.search), labelText: 'Search chats', ), ), ); } } /// A grouped Cupertino card for a message that may wrap onto several lines. /// /// [CupertinoListTile] forces its title to one ellipsised line, which cut long /// no-match queries and server reasons. This layout keeps the native card but /// lets the message wrap, with its action on a separate button below. class ChatCupertinoMessage extends StatelessWidget { const ChatCupertinoMessage({ super.key, required this.message, this.icon, this.actionLabel, this.onAction, }); final String message; final IconData? icon; final String? actionLabel; final VoidCallback? onAction; @override Widget build(BuildContext context) { return Semantics( container: true, explicitChildNodes: true, child: CupertinoListSection.insetGrouped( children: [ Padding( padding: const EdgeInsets.all(16), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (icon != null) ...[ Icon( icon, color: CupertinoColors.secondaryLabel.resolveFrom(context), ), const SizedBox(width: 12), ], Expanded( // Cupertino widgets normally supply their own text style. // This card is also mounted under a bare MaterialApp (the // iOS/macOS theme path), where Text would otherwise inherit // MaterialApp's "missing Material" fallback style. child: DefaultTextStyle.merge( style: CupertinoTheme.of(context).textTheme.textStyle, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(message), if (actionLabel != null && onAction != null) Align( alignment: AlignmentDirectional.centerStart, child: CupertinoButton( padding: const EdgeInsets.only(top: 8), onPressed: onAction, child: Text(actionLabel!), ), ), ], ), ), ), ], ), ), ], ), ); } } class ChatErrorNotice extends StatelessWidget { const ChatErrorNotice({ super.key, required this.message, required this.onRetry, }); final String message; final VoidCallback onRetry; @override Widget build(BuildContext context) { if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) { return ChatCupertinoMessage( message: message, icon: CupertinoIcons.exclamationmark_circle, actionLabel: 'Retry', onAction: onRetry, ); } return MaterialBanner( content: Text(message), actions: [TextButton(onPressed: onRetry, child: const Text('Retry'))], ); } } class ChatLoadingIndicator extends StatelessWidget { const ChatLoadingIndicator({super.key}); @override Widget build(BuildContext context) { return Center( child: chatControlsStyleOf(context) == ChatControlsStyle.cupertino ? const CupertinoActivityIndicator() : const CircularProgressIndicator(), ); } }