import 'package:didvan/config/design_config.dart'; import 'package:didvan/config/theme_data.dart'; import 'package:didvan/constants/app_icons.dart'; import 'package:didvan/models/view/app_bar_data.dart'; import 'package:didvan/pages/home/comments/comments_state.dart'; import 'package:didvan/pages/home/comments/widgets/comment_item.dart'; import 'package:didvan/widgets/animated_visibility.dart'; import 'package:didvan/widgets/didvan/icon_button.dart'; import 'package:didvan/widgets/didvan/scaffold.dart'; import 'package:didvan/widgets/didvan/text.dart'; import 'package:didvan/widgets/shimmer_placeholder.dart'; import 'package:didvan/widgets/state_handlers/sliver_state_handler.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; class Comments extends StatefulWidget { final Map pageData; const Comments({ Key? key, required this.pageData, }) : super(key: key); @override _CommentsState createState() => _CommentsState(); } class _CommentsState extends State { final _focusNode = FocusNode(); @override void initState() { final state = context.read(); state.itemId = widget.pageData['id']; state.isRadar = widget.pageData['isRadar']; state.onCommentAdded = widget.pageData['onCommentAdded']; Future.delayed( Duration.zero, () => state.getComments(), ); super.initState(); } @override Widget build(BuildContext context) { return Material( child: Stack( children: [ DidvanScaffold( backgroundColor: Theme.of(context).colorScheme.surface, appBarData: AppBarData( hasBack: true, title: 'نظرات', subtitle: widget.pageData['title'], ), padding: const EdgeInsets.only(left: 16, right: 16, bottom: 92), slivers: [ Consumer( builder: (context, state, child) => SliverStateHandler( onRetry: state.getComments, state: state, itemPadding: const EdgeInsets.symmetric(vertical: 16), childCount: state.comments.length, placeholder: const _CommentPlaceholder(), builder: (context, state, index) => Comment( focusNode: _focusNode, comment: state.comments[index], ), ), ), ], ), Positioned( left: 0, right: 0, bottom: MediaQuery.of(context).viewInsets.bottom, child: _MessageBox(focusNode: _focusNode), ), ], ), ); } } class _MessageBox extends StatefulWidget { final FocusNode focusNode; const _MessageBox({Key? key, required this.focusNode}) : super(key: key); @override State<_MessageBox> createState() => _MessageBoxState(); } class _MessageBoxState extends State<_MessageBox> { final _controller = TextEditingController(); @override Widget build(BuildContext context) { final state = context.watch(); return Column( children: [ AnimatedVisibility( duration: DesignConfig.lowAnimationDuration, isVisible: state.replyingTo != null, child: Container( padding: const EdgeInsets.all(8), decoration: BoxDecoration( color: Theme.of(context).colorScheme.surface, border: Border( top: BorderSide( color: Theme.of(context).colorScheme.border, ), ), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ if (state.replyingTo != null) DidvanText( 'پاسخ به ${state.replyingTo!.fullName}:', color: Theme.of(context).colorScheme.caption, style: Theme.of(context).textTheme.caption, ), const Spacer(), DidvanIconButton( gestureSize: 24, color: Theme.of(context).colorScheme.caption, icon: DidvanIcons.close_regular, onPressed: () { state.commentId = null; state.replyingTo = null; state.update(); }, ), ], ), ), ), Container( decoration: BoxDecoration( color: Theme.of(context).colorScheme.surface, border: Border( top: BorderSide( color: Theme.of(context).colorScheme.border, ), ), ), child: Row( children: [ DidvanIconButton( onPressed: () => _onSend(state), icon: DidvanIcons.send_solid, size: 24, color: Theme.of(context).colorScheme.focusedBorder, ), Expanded( child: TextField( focusNode: widget.focusNode, controller: _controller, keyboardType: TextInputType.multiline, textInputAction: TextInputAction.send, style: Theme.of(context).textTheme.bodyText2, onEditingComplete: () {}, onSubmitted: (value) => _onSend(state), decoration: InputDecoration( border: InputBorder.none, hintText: 'پیام خود را ارسال کنید', hintStyle: Theme.of(context).textTheme.caption!.copyWith( color: Theme.of(context).colorScheme.disabledText), ), onChanged: (value) => state.text = value, ), ), ], ), ), ], ); } void _onSend(CommentsState state) { if (state.text.replaceAll(' ', '').isNotEmpty) { state.addComment(); _controller.text = ''; } } } class _CommentPlaceholder extends StatelessWidget { const _CommentPlaceholder({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ const ShimmerPlaceholder( height: 24, width: 24, borderRadius: DesignConfig.highBorderRadius, ), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: const [ ShimmerPlaceholder( height: 20, width: 100, ), ShimmerPlaceholder( height: 14, width: 100, ), ], ), const SizedBox(height: 12), const ShimmerPlaceholder( height: 16, width: double.infinity, ), const SizedBox(height: 8), const ShimmerPlaceholder( height: 16, width: 200, ), const SizedBox(height: 8), Row( mainAxisAlignment: MainAxisAlignment.end, children: const [ ShimmerPlaceholder( height: 24, width: 48, ), SizedBox(width: 8), ShimmerPlaceholder( height: 24, width: 48, ), ], ), ], ), ), ], ); } }