PostsView.dart

mail@pastecode.io avatar
unknown
dart
2 years ago
72 kB
2
Indexable
Never
import 'package:cached_network_image/cached_network_image.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:chewie/chewie.dart';
import 'package:dio/dio.dart';
import 'package:dots_indicator/dots_indicator.dart';
import 'package:fancy_shimmer_image/fancy_shimmer_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:indexed_list_view/indexed_list_view.dart';
import 'package:lazy_loading_list/lazy_loading_list.dart';
import 'package:mobile/app/Dto/like_dto.dart';
import 'package:mobile/app/Dto/post_report_dto.dart';
import 'package:mobile/app/Dto/provider_comment_model.dart';
import 'package:mobile/app/Dto/provider_follow_dto.dart';
import 'package:mobile/app/Utilities/methods.dart';
import 'package:mobile/app/global_widgets/circular_block_button_widget.dart';
import 'package:mobile/app/global_widgets/favorite_button.dart';
import 'package:mobile/app/global_widgets/text_field_widget.dart';
import 'package:mobile/app/models/new/app_user_model.dart';
import 'package:mobile/app/models/one_signal_device.dart';
import 'package:mobile/app/models/responses/get_all_posts_response.dart';
import 'package:mobile/app/models/responses/get_all_provider_likes.dart';
import 'package:mobile/app/models/responses/get_all_provier_following.dart';
import 'package:mobile/app/models/responses/provider_posts_model.dart';
import 'package:mobile/app/modules/account/controllers/account_controller.dart';
import 'package:mobile/app/modules/account/controllers/posts_comments_controller.dart';
import 'package:mobile/app/modules/account/controllers/posts_likes_controller.dart';
import 'package:mobile/app/modules/account/widgets/post_comments.dart';
import 'package:mobile/app/modules/account/widgets/post_likes.dart';
// import 'package:mobile/app/modules/account/views/posts_view.dart';
import 'package:mobile/app/modules/account/views/comments_list.dart';
import 'package:mobile/app/modules/account/views/likes.dart';
import 'package:mobile/app/modules/root/controllers/root_controller.dart';
import 'package:mobile/app/modules/root/views/root_view.dart';
import 'package:mobile/app/other_provider_account/controller/other_provider_controller.dart';
import 'package:mobile/app/other_provider_account/views/another_provider_account_view.dart';
import 'package:mobile/app/providers/api_provider.dart';
import 'package:mobile/app/routes/app_pages.dart';
import 'package:mobile/app/services/auth_service.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
import 'package:readmore/readmore.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
import 'package:video_player/video_player.dart';
import 'package:mobile/common/ui.dart';
import 'package:visibility_detector/visibility_detector.dart';
import 'package:volume_watcher/volume_watcher.dart';

class PostsView extends GetView<AccountController>{
  AppUser _currentUser = Get.find<AuthService>().appUser.value;

  Widget build (BuildContext context){
    controller.itemScrollController = ItemScrollController();
    return Scaffold(
      extendBodyBehindAppBar: false,
      body: Obx(
          () => controller.providerGridPosts.length <= 0
              ? Center(
                  child: CupertinoActivityIndicator(
                  radius: 10,
                ))
              : Padding(
                padding: EdgeInsets.only(top: 25),
                child: Column(
                  children: [
                    Expanded(
                        flex: 1,
                        child: Container(
                          color: Get.theme.primaryColor,
                          // margin: EdgeInsets.only(left: 8, right: 8, top: 35),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              IconButton(
                              icon: new Icon(Icons.arrow_back_ios,
                                  color: Get.theme.hoverColor),
                              onPressed: () {
                                // controller.vController?.pause();
                                Get.back();
                              },
                            ),
                              TextButton(
                                onPressed: () async {
                                  
                                    controller.itemScrollController
                                      .scrollTo(index: 1, duration: Duration(milliseconds: 300));
                                  },
                                  
                                      
                                
                                child: Text(
                                  "Posts".tr,
                                  style: Get.theme.textTheme.headline5
                                      .merge(
                                        TextStyle(
                                            color: context.theme.hoverColor,
                                            fontWeight: FontWeight.w600),
                                      )
                                      .copyWith(fontSize: 21.0),
                                ),
                              ),
                              IconButton(
                                icon: _currentUser.id != null
                                    ? SvgPicture.asset(
                                        'assets/svg/Icon-notifications.svg',
                                      )
                                    : Icon(
                                        Icons.notifications_active,
                                      ),
                                onPressed: () async {
                                  // await OneSignal.shared
                                  //     .clearOneSignalNotifications();
                                  Get.toNamed(Routes.NOTIFICATIONS);
                                },
                              ),
                            ],
                          ),
                        ),
                      ),
                      Expanded(
                        flex: 9,
                        child: Container(
                          decoration: BoxDecoration(),
                          child: IgnorePointer(
                            ignoring: controller
                                .commentsController.text.isNotEmpty,
                            child: ScrollablePositionedList.builder(
                              initialScrollIndex: Get.arguments,
                              padding: EdgeInsets.zero,
                              itemScrollController: controller.itemScrollController,
                              // minCacheExtent: Get.height * 5,
                              itemCount: controller.providerGridPosts
                                  .where(
                                    (p0) => p0.url != null,
                                  )
                                  .length,
                              // cacheExtent: Get.height,
                              itemBuilder: (BuildContext context, int index) {
                                if (index<0 || index >= controller.providerGridPosts.length) return null;
                                else
                                return listItem(
                                    controller.providerGridPosts
                                        .where((p0) => p0.url != null)
                                        .toList()[index], context);
                              },
                            ),
                          ),
                        ),
                      ),
                //       Container(
                //         height: controller.isLoading.value ? 50.0 : 0,
                //         child: controller.isLoading.value ? Center(
                //   child: CupertinoActivityIndicator(
                //   radius: 8,
                // )):SizedBox()
                //       ),
                  ],
                ),
                )
    ));
  }
}

Widget listItem(PostsItems e, BuildContext context) {
    AppUser _currentUser = Get.find<AuthService>().appUser.value;
    return PostItem(
      e: e,
      currentUser: _currentUser,
      rootController: Get.put(RootController()),
      accountController: Get.put(AccountController()),
      postLikesController: Get.put(PostsLikesController()),
      postCommentsController: Get.put(PostsCommentsController()),
    );
  }


class PostItem extends StatelessWidget {
  const PostItem({
    Key key,
    @required AppUser currentUser,
    @required this.rootController,
    @required this.e,
    @required this.postLikesController,
    @required this.postCommentsController,
        this.accountController,
  })  : _currentUser = currentUser,
        super(key: key);

  final AccountController accountController;
  final PostsItems e;
  final RootController rootController;
  final PostsCommentsController postCommentsController;
  final PostsLikesController postLikesController;
  final AppUser _currentUser;
  @override
  Widget build(BuildContext context) {
    dynamic like = e.isLiked;
    RegExp regExp1 = new RegExp("[a-zA-Z]");
    return e.url.isNotEmpty
        ? Container(
            color: Get.theme.primaryColor,
            width: Get.width,
            child: Column(
              children: [
                Column(
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              InkWell(
                                onTap: () async {
                                   
                                    Get.back();
                                  
                                },
                                child: Container(
                                  margin:
                                      EdgeInsets.only(top: 10.0, bottom: 5.0),
                                  padding: EdgeInsets.all(5.0),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: <Widget>[
                                      Stack(
                                        children: [
                                          e.providerProfileUrl != null
                                              ? ClipRRect(
                                                  borderRadius:
                                                      new BorderRadius.circular(
                                                          50.0),
                                                  child: CircleAvatar(
                                                    backgroundColor:
                                                        Ui.parseColor(
                                                            "#BEBEBE"),
                                                    radius: 22,
                                                    backgroundImage:
                                                        CachedNetworkImageProvider(
                                                      '${e.providerProfileUrl}',
                                                      cacheKey:
                                                          e.providerProfileUrl,
                                                    ),
                                                  ),
                                                )
                                              : ClipRRect(
                                                  borderRadius:
                                                      new BorderRadius.circular(
                                                          50.0),
                                                  child: Image.asset(
                                                    'assets/img/def_user.jpg',
                                                    width: 60,
                                                    height: 60,
                                                  ),
                                                ),
                                          Positioned(
                                            top: e.providerProfileUrl == null
                                                ? 8
                                                : 0,
                                            right: e.providerProfileUrl == null
                                                ? 8
                                                : 0,
                                            child: InkWell(
                                              onTap: () {},
                                              child: e.isLicenseActive == true
                                                  ? CircleAvatar(
                                                      backgroundColor:
                                                          Get.theme.focusColor,
                                                      radius: 8,
                                                      child: Icon(
                                                        Icons.check,
                                                        size: 12,
                                                        color: Get
                                                            .theme.primaryColor,
                                                      ),
                                                    )
                                                  : SizedBox(),
                                            ),
                                          ),
                                        ],
                                      ),
                                      SizedBox(
                                        width: 5.0,
                                      ),
                                      Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        mainAxisAlignment:
                                            MainAxisAlignment.start,
                                        mainAxisSize: MainAxisSize.min,
                                        children: <Widget>[
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.start,
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                mainAxisAlignment:
                                                    MainAxisAlignment.start,
                                                children: [
                                                  Text(
                                                    e.providerName,
                                                    textScaleFactor: 1.0,
                                                    style: TextStyle(
                                                      color: Colors.black,
                                                      fontSize: 14.0,
                                                      fontWeight:
                                                          FontWeight.bold,
                                                    ),
                                                  ),
                                                  SizedBox(
                                                    height: 4,
                                                  ),
                                                  Directionality(
                                                    textDirection:
                                                        TextDirection.ltr,
                                                    child: Text(
                                                      "${e.providerUserName}",
                                                      textScaleFactor: 1.0,
                                                      style: TextStyle(
                                                        color:
                                                            Get.theme.hintColor,
                                                        fontSize: 11.0,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                      ),
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ).marginOnly(top: 8, bottom: 8),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                               IconButton(
                                      icon: SvgPicture.asset(
                                        'assets/svg/options.svg',
                                        height: 20,
                                      ),
                                      onPressed: () {
                                        showModalBottomSheet(
                                            shape: RoundedRectangleBorder(
                                              borderRadius:
                                                  BorderRadius.vertical(
                                                      top: Radius.circular(
                                                          15)),
                                            ),
                                            context: context,
                                            builder: (context) {
                                              return Container(
                                                  height: Get.height * 0.21,
                                                  width: Get.width,
                                                  decoration: BoxDecoration(
                                                      borderRadius:
                                                          BorderRadius.vertical(
                                                              top: Radius
                                                                  .circular(
                                                                      20))),
                                                  child: Column(
                                                    children: [
                                                      SvgPicture.asset(
                                                        'assets/svg/bottomSheetGrip.svg',
                                                        height: 25,
                                                        width: 50,
                                                        color: Colors
                                                            .grey[400],
                                                      ).paddingOnly(
                                                          bottom: 20),
                                                      Column(
                                                        children: [
                                                          InkWell(
                                                            onTap:
                                                                () async {
                                                                  Get.back();
                                                              showDialog(
                                    context: context,
                                    builder: (context) {
                                      TextEditingController newDescription =
                                          TextEditingController(
                                        text:
                                            accountController.postDescription == 'null'
                                                ? ''
                                                : accountController.postDescription,
                                      );
                                      // TextEditingController()..text = "${controller.postDescription.toString().isEmpty ? ' ' : controller.postDescription.toString() }";

                                      return new Scaffold(
                                        backgroundColor: Colors.transparent,
                                        body: Center(
                                          child: Container(
                                            decoration: BoxDecoration(
                                                color: Get.theme.primaryColor,
                                                borderRadius: BorderRadius.all(
                                                    Radius.circular(25))),
                                            width: Get.width * 0.8,
                                            height: Get.height * 0.35,
                                            child: Padding(
                                              padding:
                                                  const EdgeInsets.symmetric(
                                                      vertical: 18.0),
                                              child: Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.center,
                                                children: [
                                                  Row(
                                                    children: [
                                                      Expanded(
                                                          child: Divider(
                                                        color: Colors.grey,
                                                      )),
                                                      Text(
                                                        "Edit my post".tr,
                                                        style: Get.textTheme
                                                            .headline5,
                                                      ).paddingSymmetric(
                                                          horizontal: 5),
                                                      Expanded(
                                                          child: Divider(
                                                        color: Colors.grey,
                                                      )),
                                                    ],
                                                  ),
                                                  SizedBox(
                                                    height: 15,
                                                  ),
                                                  TextFieldWidget(
                                                    controller: newDescription,
                                                    hintText:
                                                        "enter new description"
                                                            .tr,
                                                    style:
                                                        Get.textTheme.bodyText1,
                                                    isFirst: true,
                                                  ),
                                                  SizedBox(
                                                    height: 15,
                                                  ),
                                                  Row(
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .spaceEvenly,
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment
                                                            .center,
                                                    children: [
                                                      MaterialButton(
                                                        color: Get
                                                            .theme.focusColor,
                                                        shape: RoundedRectangleBorder(
                                                            borderRadius:
                                                                BorderRadius
                                                                    .circular(
                                                                        10)),
                                                        onPressed: () {
                                                          Get.back();
                                                        },
                                                        child: Text(
                                                          "Cancel".tr,
                                                          style: Get.textTheme
                                                              .bodyText1
                                                              .copyWith(
                                                            color: Get.theme
                                                                .primaryColor,
                                                          ),
                                                        ),
                                                      ),
                                                      MaterialButton(
                                                        color: Get
                                                            .theme.focusColor,
                                                        shape: RoundedRectangleBorder(
                                                            borderRadius:
                                                                BorderRadius
                                                                    .circular(
                                                                        10)),
                                                        onPressed: () async {
                                                          ProviderPostsModel
                                                              posts =
                                                              ProviderPostsModel(
                                                            providerId:
                                                                _currentUser.id,
                                                            description:
                                                                newDescription
                                                                    .text,
                                                            id: accountController
                                                                .postId,
                                                            IgnoreFile: true,
                                                          );
                                                          await ApiProvider(
                                                                  httpClient:
                                                                      Dio())
                                                              .editProviderPosts(
                                                                  posts, null);
                                                          // accountController.getAll(accountController.pageIndex.value);
                                                          accountController.getAllGridPosts();
                                                          accountController.providerGridPosts.refresh();
                                                          // Get.to(RootView());
                                                          Get.back();
                                                        },
                                                        child: Text(
                                                          "Save".tr,
                                                          style: Get.textTheme
                                                              .bodyText1
                                                              .copyWith(
                                                            color: Get.theme
                                                                .primaryColor,
                                                          ),
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ),
                                        ),
                                      );
                                    },
                                  );
                                                            },
                                                            child:
                                                                Container(
                                                              width:
                                                                  Get.width,
                                                              child: Text(
                                                                "Edit post".tr,
                                                                style: Get
                                                                    .textTheme
                                                                    .headline4
                                                                    .copyWith(
                                                                        fontSize:
                                                                            16,
                                                                        fontWeight:
                                                                            FontWeight.w600),
                                                              ),
                                                            ),
                                                          ),
                                                          Divider(
                                                                color: Colors
                                                                    .black,
                                                              ).paddingSymmetric(
                                                                  vertical: 5),
                                                          InkWell(
                                                            onTap: (){
                                                        ApiProvider(httpClient: Dio())
                                                        .deleteProviderPosts(accountController.postId);
                                                        accountController.providerPosts.removeWhere(
                                                        (element) =>
                                                        element.id == accountController.postId);
                                                        ApiProvider(httpClient: Dio())
                                                        .deleteProviderPosts(accountController.postId);
                                                        accountController.providerGridPosts.removeWhere(
                                                        (element) =>
                                                        element.id == accountController.postId);
                                                        accountController.changeAccountViewHeight(0);
                                                        Get.back();
                                                            },
                                                            child: Container(
                                                                width:
                                                                    Get.width,
                                                                child: Text(
                                                                  "Delete post".tr,
                                                                  style: Get
                                                                      .textTheme
                                                                      .headline4
                                                                      .copyWith(
                                                                          fontSize:
                                                                              16,
                                                                          fontWeight:
                                                                              FontWeight.w600),
                                                                ),
                                                              ),
                                                          ),
                                                          
                                                        ],
                                                      ).paddingSymmetric(
                                                          horizontal: 30)
                                                    ],
                                                  ));
                                            });
                                      },
                                    )
                                  
                            ],
                          ),
                          e.url.length <= 2
                              ? e.url.first.contains("mp4") ||
                                      e.url.first.contains('MOV') ||
                                      e.url.first.contains('m3u8')
                                  ? VideoWidget(
                                      url: e.url.first,
                                    )
                                  : Column(
                                      mainAxisSize: MainAxisSize.min,
                                      children: [
                                        CarouselSlider.builder(
                                          itemCount: e.url.length,
                                          itemBuilder:
                                              (context, index, realIndex) {
                                            return FancyShimmerImage(
                                              imageUrl: e.url[index],
                                              width: Get.width,
                                              boxFit: BoxFit.fitWidth
                                            );
                                          },
                                          options: CarouselOptions(
                                            viewportFraction: 1.0,
                                            enableInfiniteScroll: false,
                                            aspectRatio: 1 / 1,
                                            onPageChanged: (index, reason) {
                                              accountController.onPageChanged(
                                                  index, reason, e.id);
                                            },
                                          ),
                                        ),
                                      ],
                                    )
                              : CarouselSlider.builder(
                                  itemCount: e.url.length,
                                  itemBuilder: (context, index, realIndex) {
                                    return FancyShimmerImage(
                                      imageUrl: e.url[index],
                                      width: Get.width,
                                      boxFit: BoxFit.cover,
                                    );
                                  },
                                  options: CarouselOptions(
                                      viewportFraction: 1.0,
                                      enableInfiniteScroll: false,
                                      aspectRatio: 1 / 1,
                                      onPageChanged: (index, reason) {
                                        accountController.onPageChanged(
                                            index, reason, e.id);
                                      }),
                                ),
                        ],
                      ),
                    ),
                    e.url.length >= 2 &&
                            !e.url.first.contains("mp4") &&
                            !e.url.first.contains('MOV') &&
                            !e.url.first.contains('m3u8')
                        ? Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              DotsIndicator(
                                dotsCount: e.url.length,
                                position: e.index.toDouble(),
                                decorator: DotsDecorator(
                                  color: Colors.grey[300], // Inactive color
                                  activeColor: Colors.grey[600],
                                ),
                              ),
                            ],
                          ).marginOnly(bottom: 10.0)
                        : SizedBox(),
                    SizedBox(
                      height: 8,
                    ),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Icon(
                            Icons.access_time,
                            color: Colors.black.withOpacity(0.7),
                            size: 15.0,
                          ),
                          SizedBox(
                            width: 5.0,
                          ),
                          Text(
                            AppMethodsAndUtilities()
                                .getVerboseDateTimeRepresentation(
                                    e.createdDate),
                            style: TextStyle(
                                color: Colors.black.withOpacity(0.7),
                                fontSize: 13),
                          ),
                        ]).paddingOnly(left: 6, right: 6),
                    e.description != null
                        ? Directionality(
                            textDirection: TextDirection.ltr,
                            child: Container(
                                child: Row(
                              mainAxisAlignment: e.description != null &&
                                      !regExp1.hasMatch(e.description)
                                  ? MainAxisAlignment.end
                                  : MainAxisAlignment.start,
                              children: [
                                Flexible(
                                  child: ReadMoreText(
                                    e.description,
                                    style: Get.textTheme.bodyText2.copyWith(
                                      fontWeight: FontWeight.w600,
                                      fontSize: 12,
                                    ),
                                    trimLines: 1,
                                    colorClickableText: Colors.black,
                                    trimMode: TrimMode.Line,
                                    textDirection:
                                        !regExp1.hasMatch(e.description)
                                            ? TextDirection.rtl
                                            : TextDirection.ltr,
                                    trimCollapsedText: 'more'.tr,
                                    trimExpandedText: 'less'.tr,
                                    lessStyle: TextStyle(
                                      color: Colors.grey[400],
                                      fontSize: 14,
                                      fontWeight: FontWeight.normal,
                                    ),
                                    moreStyle: TextStyle(
                                      color: Colors.grey[400],
                                      fontSize: 14,
                                      fontWeight: FontWeight.normal,
                                    ),
                                  ),
                                ),
                              ],
                            )).paddingOnly(
                                top: 10, left: 22, right: 22, bottom: 10),
                          )
                        : SizedBox()
                  ],
                ),

                // Don't Touch Pls...
                Container(
                  margin: EdgeInsets.only(right: 5.0, left: 5.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          // love icon
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              FavoriteButton(
                                iconDisabledColor: Colors.black87,
                                isFavorite: like,
                                iconSize: 35,
                                valueChanged: (like) async {
                                  print('Is Favorite : $like');
                                  if (!e.isLiked) {
                                    LikeDto likeDto = LikeDto(
                                      providerId: e.providerId,
                                      requesterId: _currentUser.id,
                                      isLike: true,
                                      postId: e.id,
                                    );
                                    await ApiProvider(httpClient: Dio())
                                        .providerAddLikes(likeDto)
                                        .then((value) {
                                      accountController.providerGridPosts
                                          .where(
                                              (element) => element.id == e.id)
                                          .first
                                          .likeCount++;
                                    });
                                    accountController.isHeartAnimating.value =
                                        true;
                                    accountController.providerGridPosts.refresh();
                                    accountController.likes.refresh();
                                    e.isLiked = true;
                                    await ApiProvider(httpClient: Dio())
                                        .getAllProviderLikes(e.id);
                                  } else {
                                    print('unlike');

                                    await postLikesController
                                        .getAllProviderLikes(e.id);

                                    var likeId = postLikesController.likes.value
                                        .where((element) =>
                                            element.requesterId ==
                                            _currentUser.id)
                                        .last
                                        .id;

                                    await ApiProvider(httpClient: Dio())
                                        .providerFeedDeleteLike(likeId)
                                        .then((value) {
                                      accountController.providerGridPosts
                                          .where(
                                              (element) => element.id == e.id)
                                          .first
                                          .likeCount--;
                                    });
                                    accountController.isHeartAnimating.value =
                                        false;
                                    accountController.providerGridPosts.refresh();
                                    postLikesController.likes.refresh();
                                    e.isLiked = false;
                                  }
                                },
                              ),
                              SizedBox(
                                width: 12.0,
                              ),
                              InkWell(
                                child: Text(
                                  "${e.likeCount}",
                                  style: Get.textTheme.bodyText1
                                      .copyWith(fontWeight: FontWeight.bold),
                                ),
                                onTap: () async {
                                  Get.lazyPut(() => PostsLikesController());
                                  await postLikesController
                                      .getAllProviderLikes(e.id)
                                      .then((value) {
                                    Get.to(
                                      PostLikes(),
                                    );
                                  });
                                },
                              ),
                            ],
                          ),

                          SizedBox(width: 3.0),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              IconButton(
                                icon: SvgPicture.asset(
                                  "assets/svg/instagram-comment.svg",
                                  width: 19,
                                ),
                                onPressed: () async {
                                  Get.lazyPut(() => PostsCommentsController());
                                  await accountController
                                      .getAllComments(e.id)
                                      .then((value) {
                                    postCommentsController.setPostId(e.id);
                                    postCommentsController
                                        .setPostOwner(e.providerId);
                                    accountController.inFeed.value = false;
                                    Get.to(
                                      PostComments(),
                                    ).then(
                                      (value) =>
                                          accountController.inFeed.value = true,
                                    );
                                  });
                                },
                              ),
                              Text(
                                "${e.commentCount}",
                                style: Get.textTheme.bodyText1
                                    .copyWith(fontWeight: FontWeight.bold),
                              ),
                            ],
                          ),
                        ],
                      ),
                      e.postPrice != null
                          ? Flexible(
                              child: Ui.getPrice(e.postPrice)
                                  .paddingOnly(right: 20))
                          : SizedBox(),
                    ],
                  ),
                ),
                e.comments != null && e.comments.isNotEmpty
                    ? Container(
                        margin: EdgeInsets.only(
                          left: 10,
                          right: 10,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            e.commentCount > 0
                                ? Container(
                                    margin: EdgeInsets.only(top: 10.0),
                                    child: InkWell(
                                      child: IgnorePointer(
                                        child: RichText(
                                          textAlign: TextAlign.start,
                                          overflow: TextOverflow.ellipsis,
                                          maxLines: 2,
                                          text: TextSpan(
                                            text: "See all ".tr,
                                            style: Get.textTheme.bodyText1
                                                .copyWith(
                                              fontSize: 12,
                                              fontWeight: FontWeight.w500,
                                              color: Ui.parseColor("#949494"),
                                            ),
                                            children: <TextSpan>[
                                              TextSpan(
                                                text: " (${e.commentCount}) ",
                                                style: Get.textTheme.bodyText1
                                                    .copyWith(
                                                  fontSize: 12,
                                                  fontWeight: FontWeight.w500,
                                                  color:
                                                      Ui.parseColor("#949494"),
                                                ),
                                              ),
                                              TextSpan(
                                                text: "comments".tr,
                                                style: Get.textTheme.bodyText1
                                                    .copyWith(
                                                  fontSize: 12,
                                                  fontWeight: FontWeight.w500,
                                                  color:
                                                      Ui.parseColor("#949494"),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ),
                                      onTap: () async {
                                        Get.lazyPut(
                                            () => PostsCommentsController());
                                        await postCommentsController
                                            .getAllComments(e.id)
                                            .then((value) {
                                          postCommentsController
                                              .setPostId(e.id);
                                          postCommentsController
                                              .setPostOwner(e.providerId);
                                          accountController.inFeed.value = false;

                                          Get.to(
                                            PostComments(),
                                          ).then(
                                            (value) => accountController
                                                .inFeed.value = true,
                                          );
                                        });
                                      },
                                    ),
                                  )
                                : SizedBox(),
                          ],
                        ),
                      )
                    : SizedBox(),
                SizedBox(
                  height: 8,
                ),
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(
                      Radius.circular(30),
                    ),
                  ),
                  height: Get.height * 0.06,
                  width: Get.width,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      InkWell(
                        onTap: () async {
                          accountController.commentsController.clear();
                          Get.log("trying to add comments on post ");
                          Get.log('Current User=${_currentUser.name}');
                          showModalBottomSheet(
                            backgroundColor: Get.theme.primaryColor,
                            context: context,
                            constraints: BoxConstraints(
                              maxHeight: Get.height * 0.5,
                            ),
                            builder: (context) {
                              return new Scaffold(
                                body: Container(
                                  height: Get.height * 0.5,
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Expanded(
                                        flex: 5,
                                        child: TextFormField(
                                          textAlign: TextAlign.start,
                                          autofocus: true,
                                          maxLines: 2,
                                          maxLength: 160,
                                          onEditingComplete: () {},
                                          onFieldSubmitted: (value) async {
                                            // print("onFieldSubmitted");
                                            ProviderCommentModel comments =
                                                ProviderCommentModel(
                                                    providerId: e
                                                        .providerId, // controller.postOwner,
                                                    requesterId: _currentUser
                                                        .id, //.appUser.value.id,
                                                    comments: accountController
                                                        .commentsController
                                                        .text,
                                                    postId: e.id // .postId
                                                    );

                                            await ApiProvider(httpClient: Dio())
                                                .providerAddComments(comments)
                                                .then((value) {
                                              accountController.providerGridPosts
                                                  .where((element) =>
                                                      element.id ==
                                                      comments.postId)
                                                  .first
                                                  .commentCount++;
                                              accountController.providerGridPosts.refresh();
                                            });
                                            Comments c = Comments(
                                                comments: accountController
                                                    .commentsController.text,
                                                postId: e.id,
                                                providerId: e.providerId,
                                                requesterName:
                                                    _currentUser.name,
                                                providerName: e.providerName);
                                            e.comments.add(c);
                                            accountController.commentsController
                                                .clear();
                                          },
                                          controller:
                                              accountController.commentsController,
                                          textInputAction: TextInputAction.done,
                                          cursorColor: Colors.black,
                                          keyboardType: TextInputType.multiline,
                                          style:
                                              TextStyle(color: Colors.black87),
                                          decoration: new InputDecoration(
                                            border: InputBorder.none,
                                            focusedBorder: InputBorder.none,
                                            enabledBorder: InputBorder.none,
                                            errorBorder: InputBorder.none,
                                            disabledBorder: InputBorder.none,
                                            contentPadding: EdgeInsets.only(
                                                left: 15,
                                                bottom: 4,
                                                top: 4,
                                                right: 15),
                                            hintText: "Add comment".tr,
                                          ),
                                        ),
                                      ),
                                      Expanded(
                                        child: TextButton(
                                          onPressed: () async {
                                            ProviderCommentModel comments =
                                                ProviderCommentModel(
                                                    providerId: e
                                                        .providerId, // controller.postOwner,
                                                    requesterId: _currentUser
                                                        .id, //.appUser.value.id,
                                                    comments: accountController
                                                        .commentsController
                                                        .text,
                                                    postId: e.id // .postId
                                                    );

                                            await ApiProvider(httpClient: Dio())
                                                .providerAddComments(comments)
                                                .then((value) {
                                              accountController.providerGridPosts
                                                  .where((element) =>
                                                      element.id ==
                                                      comments.postId)
                                                  .first
                                                  .commentCount++;
                                              accountController.providerGridPosts.refresh();
                                            });
                                            Comments c = Comments(
                                                comments: accountController
                                                    .commentsController.text,
                                                postId: e.id,
                                                providerId: e.providerId,
                                                requesterName:
                                                    _currentUser.name,
                                                providerName: e.providerName);
                                            e.comments.add(c);
                                            accountController.commentsController
                                                .clear();
                                            Get.back();
                                          },
                                          child: Text(
                                            "Post".tr,
                                            style: Get.textTheme.bodyText2
                                                .copyWith(
                                              color: Ui.parseColor("#4c68d7"),
                                              fontSize: 12,
                                              fontWeight: FontWeight.bold,
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              );
                            },
                          );
                        },
                        child: Row(
                          children: [
                            CircleAvatar(
                              radius: 16,
                              backgroundColor: Colors.grey[400],
                              child: CircleAvatar(
                                backgroundColor: Ui.parseColor("#BEBEBE"),
                                radius: 16,
                                backgroundImage: NetworkImage(
                                  '${_currentUser.profilePictureUrl}',
                                ),
                              ),
                            ),
                            SizedBox(
                              width: 8,
                            ),
                            Text(
                              "Add Comment...".tr,
                              style: Get.textTheme.bodyText1.copyWith(
                                color: Get.theme.hintColor,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          )
        : SizedBox();
  }
}

class PostVideoWidget extends StatefulWidget {
  const PostVideoWidget({Key key, this.videoUrl, this.autoPlay})
      : super(key: key);

  final bool autoPlay;
  final String videoUrl;

  @override
  _PostVideoWidgetState createState() => _PostVideoWidgetState();
}

class _PostVideoWidgetState extends State<PostVideoWidget> {
  VideoPlayerController _controller;

  @override
  void dispose() {
    super.dispose();
    _controller?.dispose();
  }

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(widget.videoUrl)
      ..initialize().then((_) {
        _controller.setLooping(widget.autoPlay);
        Future.delayed(
          Duration(
            milliseconds: 500,
          ),
          () {
            if (widget.autoPlay) {
              _controller.play();
              _controller.setVolume(0.0);
            } else {
              _controller.pause();
              _controller.setVolume(0.0);
            }

            setState(() {});
          },
        );
      });
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (_controller.value.volume == 0.0) {
          _controller.setVolume(1.0);
        } else {
          _controller.setVolume(0.0);
        }
        setState(() {});
      },
      child: _controller.value.isInitialized
          ? Center(
              child: Stack(
                children: [
                  AspectRatio(
                    aspectRatio: 1,
                    child: VideoPlayer(_controller),
                  ),
                  widget.autoPlay
                      ? Container(
                          margin: EdgeInsets.all(5.0),
                          child: _controller.value.volume == 0.0
                              ? Icon(
                                  Icons.volume_off,
                                  color: Get.theme.primaryColor,
                                  size: 25,
                                )
                              : Icon(
                                  Icons.volume_up,
                                  color: Get.theme.primaryColor,
                                  size: 25,
                                ),
                        )
                      : Icon(
                          Icons.play_arrow,
                          color: Get.theme.primaryColor,
                          size: 25,
                        )
                ],
              ),
            )
          : Container(),
    );
  }
}

// Forbidden
class VideoWidget extends StatefulWidget {
  const VideoWidget({
    @required this.url,
  });

  final String url;

  @override
  _VideoWidgetState createState() => _VideoWidgetState();
}

class _VideoWidgetState extends State<VideoWidget> {
  double aRatio = 0.5;
  ChewieController chewieController;

  VideoPlayerController _controller;
  Future<void> _initializeVideoPlayerFuture;

  @override
  void dispose() {
    _controller?.dispose();
    chewieController?.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.network(widget.url);
    _initializeVideoPlayerFuture = _controller.initialize().then((_) {
      // Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
      setState(
        () {
          aRatio = _controller.value.aspectRatio <
                  ((MediaQuery.of(context).size.width /
                          MediaQuery.of(context).size.height) +
                      0.4)
              ? _controller.value.aspectRatio / 0.7
              : _controller.value.aspectRatio;
          chewieController = ChewieController(
            cupertinoProgressColors: ChewieProgressColors(
              playedColor: Colors.transparent,
              bufferedColor: Colors.transparent,
              handleColor: Colors.transparent,
            ),
            materialProgressColors: ChewieProgressColors(
              playedColor: Colors.transparent,
              bufferedColor: Colors.transparent,
              handleColor: Colors.transparent,
            ),
            videoPlayerController: _controller,
            autoPlay: false,
            looping: false,
            autoInitialize: true,
            allowMuting: true,
            deviceOrientationsOnEnterFullScreen: [
              DeviceOrientation.portraitUp,
              DeviceOrientation.landscapeLeft,
              DeviceOrientation.landscapeRight,
            ],
            deviceOrientationsAfterFullScreen: [DeviceOrientation.portraitUp],
            aspectRatio: aRatio,
          );
          _controller.pause();
          _controller.setLooping(false);
        },
      
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return VisibilityDetector(
      key: ObjectKey(_controller),
      onVisibilityChanged: (visibility) {
        if (visibility.visibleFraction == 0 &&
            this.mounted &&
            !chewieController.isFullScreen) {
          _controller.pause();
        } else if (chewieController.isFullScreen) {
          _controller.play();
        } else {
          _controller.pause();
        }
      },
      child: FutureBuilder(
        future: _initializeVideoPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return _controller.value.isInitialized
                ? AspectRatio(
                    aspectRatio: aRatio,
                    child: Stack(
                      children: [
                        VolumeWatcher(
                          onVolumeChangeListener: (double volume) {
                            chewieController.setVolume(volume);
                          },
                          child: Chewie(
                            controller: chewieController,
                          ),
                        ),
                        Positioned.fill(
                          child: GestureDetector(
                            child: Container(
                              color: Colors.transparent,
                            ).marginSymmetric(vertical: 40),
                            onTap: () {
                              if (_controller.value.isPlaying) {
                                _controller.pause();
                              } else {
                                _controller.play();
                              }
                            },
                          ),
                        ),
                      ],
                    ),
                  )
                : Container();
          } else {
            return Container(
              width: double.infinity,
              height: 400,
              color: Colors.black,
              child: Center(
                child: CircularProgressIndicator(),
              ),
              // child: FancyShimmerImage(
              //     imageUrl: 'https://i.imgur.com/j57mwe4.jpg'),
            );
          }
        },
      ),
    );
  }
}