friend_trend_view.dart 4.37 KB
import 'package:cached_network_image/cached_network_image.dart';
import 'package:doublefeel_flutter/data/models/friend/friend_models.dart';
import 'package:doublefeel_flutter/l10n/l10n_extensions.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../health_trend/views/health_trend_content.dart';
import '../controllers/friend_trend_controller.dart';

class FriendTrendView extends GetView<FriendTrendController> {
  const FriendTrendView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F2FF),
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        toolbarHeight: 44,
        elevation: 0,
        scrolledUnderElevation: 0,
        backgroundColor: Colors.transparent,
        surfaceTintColor: Colors.transparent,
        systemOverlayStyle: SystemUiOverlayStyle.dark.copyWith(
          statusBarColor: Colors.transparent,
        ),
        leadingWidth: 44,
        leading: IconButton(
          onPressed: Get.back,
          padding: const EdgeInsets.only(left: 12),
          icon: const Icon(Icons.arrow_back_ios_new_rounded),
          color: const Color(0xFF0F0F11),
          iconSize: 20,
          tooltip: context.l10n.friendsBack,
        ),
        titleSpacing: 0,
        title: _FriendTrendTitle(
          title: context.l10n.friendsTrendTitle(
            _friendDisplayName(controller.friendItem),
          ),
          avatarUrl: controller.friendItem?.avatar ?? "",
        ),
      ),
      body: Stack(
        children: [
          Container(
            height: 300,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [Color(0xFFC5B0FF), Color(0xFFF5F2FF)],
              ),
            ),
          ),
          Column(
            children: [
              SizedBox(height: MediaQuery.paddingOf(context).top + 44),
              Expanded(
                child: Obx(
                  () => HealthTrendContent(
                    query: controller.query,
                    queryForType: controller.queryForType,
                    selectedTypeIndex: controller.selectedTypeIndex.value,
                    onTypeChanged: controller.changeType,
                    onPeriodChanged: controller.changePeriod,
                    onDateChanged: controller.changeDate,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  String _friendDisplayName(FriendItem? friend) {
    final remark = friend?.remarkName?.trim();
    if (remark != null && remark.isNotEmpty) return remark;

    final nickname = friend?.friendNickname?.trim();
    if (nickname != null && nickname.isNotEmpty) return nickname;

    return l10n.friendsUnknownFriend;
  }
}

class _FriendTrendTitle extends StatelessWidget {
  const _FriendTrendTitle({
    required this.title,
    required this.avatarUrl,
  });

  final String title;
  final String? avatarUrl;

  @override
  Widget build(BuildContext context) {
    final imageUrl = avatarUrl?.trim();
    return Row(
      children: [
        Container(
          width: 28,
          height: 28,
          decoration: ShapeDecoration(
            image: imageUrl?.isNotEmpty == true
                ? DecorationImage(
                    image: CachedNetworkImageProvider(imageUrl!),
                    fit: BoxFit.cover,
                  )
                : null,
            shape: RoundedRectangleBorder(
              side: const BorderSide(width: 0.78, color: Colors.white),
              borderRadius: BorderRadius.circular(35.78),
            ),
          ),
          child: imageUrl?.isNotEmpty == true ? null : const _AvatarFallback(),
        ),
        const SizedBox(width: 8),
        Expanded(
          child: Text(
            title,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: const TextStyle(
              color: Color(0xFF0F0F11),
              fontSize: 16,
              fontWeight: FontWeight.w500,
            ),
          ),
        ),
      ],
    );
  }
}

class _AvatarFallback extends StatelessWidget {
  const _AvatarFallback();

  @override
  Widget build(BuildContext context) {
    return const Icon(Icons.person_rounded, size: 18, color: Colors.white);
  }
}