interact_view.dart 16.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
import 'package:doublefeel_flutter/app/modules/interact/controllers/interact_controller.dart';
import 'package:doublefeel_flutter/data/models/interaction/interaction_models.dart';
import 'package:doublefeel_flutter/data/models/user/user_models.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class InteractView extends GetView<InteractController> {
  const InteractView({super.key});

  static const _purple = Color(0xff896cdc);
  static const _muted = Color(0xff908b91);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: [
          Image.asset('assets/images/interact/page_bg.png', fit: BoxFit.cover),
          SafeArea(
            child: Obx(
              () => Column(
                children: [
                  _appBar(),
                  Expanded(
                    child: RefreshIndicator(
                      onRefresh: controller.loadRecords,
                      child: ListView(
                        physics: const AlwaysScrollableScrollPhysics(),
                        padding: const EdgeInsets.only(bottom: 28),
                        children: [
                          _marquee(),
                          _characterArea(),
                          const SizedBox(height: 34),
                          _actionButtons(context),
                          const SizedBox(height: 28),
                          _records(),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Obx(() {
            final asset = controller.actionMaskAsset();
            if (asset == null) return const SizedBox.shrink();
            return IgnorePointer(
              child: Align(
                alignment: const Alignment(0, -.5),
                child:
                    Image.asset(asset, width: Get.width, fit: BoxFit.fitWidth),
              ),
            );
          }),
        ],
      ),
    );
  }

  Widget _appBar() {
    return SizedBox(
      height: 48,
      child: Row(
        children: [
          IconButton(
            icon: const Icon(Icons.arrow_back_ios_new, color: Colors.black),
            onPressed: Get.back,
          ),
          const Spacer(),
          IconButton(
            tooltip: '刷新互动记录',
            icon: const Icon(Icons.refresh, color: Colors.black87),
            onPressed: controller.loadRecords,
          ),
        ],
      ),
    );
  }

  Widget _marquee() {
    final records = controller.records.take(3).toList(growable: false);
    if (records.isEmpty) return const SizedBox(height: 72);
    return SizedBox(
      height: 72,
      child: ListView.separated(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
        scrollDirection: Axis.horizontal,
        itemCount: records.length,
        separatorBuilder: (_, __) => const SizedBox(width: 10),
        itemBuilder: (_, index) {
          final record = records[index];
          final isMe = record.userId == controller.me?.id;
          return Container(
            constraints: const BoxConstraints(maxWidth: 210),
            padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 7),
            decoration: BoxDecoration(
              image: const DecorationImage(
                image: AssetImage('assets/images/interact/page_bubble_bg.png'),
                fit: BoxFit.fill,
              ),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                _avatar(isMe ? controller.me : controller.partner, size: 28),
                const SizedBox(width: 7),
                Flexible(
                  child: Text(
                    controller.recordText(record),
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: const TextStyle(color: Colors.white, fontSize: 13),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget _characterArea() {
    final actionAsset = controller.partnerActionAsset();
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 24),
      child: Row(
        children: [
          Expanded(child: _person(me: true)),
          const SizedBox(width: 12),
          Expanded(
            child: controller.isPaired
                ? _person(me: false, actionAsset: actionAsset)
                : _unpairedPerson(),
          ),
        ],
      ),
    );
  }

  Widget _person({required bool me, String? actionAsset}) {
    final user = me ? controller.me : controller.partner;
    return Column(
      children: [
        SizedBox(
          height: 132,
          child: actionAsset == null
              ? _avatar(user,
                  size: 108,
                  borderColor: me ? _purple : const Color(0xfffff45b))
              : Image.asset(actionAsset, fit: BoxFit.contain),
        ),
        const SizedBox(height: 6),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            if (me)
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
                decoration:
                    const BoxDecoration(color: _purple, shape: BoxShape.circle),
                child: const Text('我',
                    style: TextStyle(color: Colors.white, fontSize: 11)),
              ),
            if (me) const SizedBox(width: 5),
            Flexible(
              child: Text(
                user?.nickname ?? '-',
                overflow: TextOverflow.ellipsis,
                style:
                    const TextStyle(fontWeight: FontWeight.w600, fontSize: 16),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _unpairedPerson() {
    return Column(
      children: [
        Image.asset('assets/images/interact/unpaired_avatar.png', width: 58),
        const SizedBox(height: 8),
        const Text('暂未绑定Feel搭子',
            style: TextStyle(color: Color(0xffff2c20), fontSize: 12)),
        const SizedBox(height: 10),
        TextButton.icon(
          onPressed: () => Get.snackbar('互动', '请先前往搭子页面完成绑定'),
          icon: const Text('去绑定'),
          label: const Icon(Icons.chevron_right, size: 18),
          style: TextButton.styleFrom(
            foregroundColor: Colors.white,
            backgroundColor: _purple,
            shape: const StadiumBorder(),
          ),
        ),
      ],
    );
  }

  Widget _actionButtons(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.end,
      children: [
        _actionButton(context, InteractAction.miss, width: 88, height: 52),
        const SizedBox(width: 12),
        _actionButton(context, InteractAction.stick, width: 112, height: 72),
        const SizedBox(width: 12),
        _actionButton(context, InteractAction.punch, width: 88, height: 52),
      ],
    );
  }

  Widget _actionButton(
    BuildContext context,
    InteractAction action, {
    required double width,
    required double height,
  }) {
    final enabled =
        controller.isPaired && (!action.needsVip || controller.isVip);
    final sending = controller.sendingAction.value == action;
    return GestureDetector(
      onLongPress: () => _showActionSheet(context),
      child: SizedBox(
        width: width,
        height: height + 18,
        child: ElevatedButton(
          onPressed: sending ? null : () => controller.sendAction(action),
          style: ElevatedButton.styleFrom(
            padding: EdgeInsets.zero,
            backgroundColor: _purple.withValues(alpha: enabled ? 1 : .4),
            disabledBackgroundColor: _purple.withValues(alpha: .4),
            elevation: 5,
            shadowColor: _purple.withValues(alpha: .5),
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
          ),
          child: sending
              ? const SizedBox(
                  width: 22,
                  height: 22,
                  child: CircularProgressIndicator(
                      color: Colors.white, strokeWidth: 2))
              : Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image.asset(
                      enabled
                          ? action.iconAsset
                          : 'assets/images/interact/icon_locked.png',
                      width: action == InteractAction.stick ? 48 : 40,
                      height: action == InteractAction.stick ? 48 : 40,
                    ),
                    Image.asset(
                      enabled ? action.titleAsset : action.lockedTitleAsset,
                      height: 20,
                      fit: BoxFit.contain,
                    ),
                  ],
                ),
        ),
      ),
    );
  }

  Widget _records() {
    final records = controller.records.take(10).toList(growable: false);
    return Container(
      margin: const EdgeInsets.only(top: 2),
      padding: const EdgeInsets.fromLTRB(24, 20, 24, 42),
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          colors: [Color(0xffeae1ff), Color(0x00fcf4ff)],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
        borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
      ),
      child: Column(
        children: [
          Row(
            children: [
              const Text('互动记录',
                  style: TextStyle(
                      fontSize: 17,
                      fontWeight: FontWeight.w700,
                      color: Color(0xff2c2020))),
              const Spacer(),
              Text('仅保留10条', style: TextStyle(fontSize: 12, color: _muted)),
            ],
          ),
          const SizedBox(height: 15),
          if (controller.isLoading.value && records.isEmpty)
            const Padding(
                padding: EdgeInsets.all(40), child: CircularProgressIndicator())
          else if (records.isEmpty)
            const Padding(
              padding: EdgeInsets.symmetric(vertical: 76),
              child: Text('暂时还没有记录哦~',
                  style: TextStyle(color: _muted, fontSize: 14)),
            )
          else
            ...records.map(_recordItem),
        ],
      ),
    );
  }

  Widget _recordItem(InteractionRecord record) {
    return Container(
      width: double.infinity,
      margin: const EdgeInsets.only(bottom: 7),
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 9),
      decoration: BoxDecoration(
          color: Colors.white.withValues(alpha: .48),
          borderRadius: BorderRadius.circular(7)),
      child: Row(
        children: [
          const Icon(Icons.circle_outlined, size: 10, color: _purple),
          const SizedBox(width: 8),
          Expanded(
              child: Text(controller.recordText(record),
                  style: const TextStyle(fontSize: 14))),
          Text(controller.recordTime(record),
              style: const TextStyle(fontSize: 12, color: _muted)),
        ],
      ),
    );
  }

  Widget _avatar(UserInfoResponse? user,
      {required double size, Color? borderColor}) {
    final avatar = user?.avatar;
    return Container(
      width: size,
      height: size,
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(
          shape: BoxShape.circle,
          border: Border.all(color: borderColor ?? _purple, width: 2),
          color: Colors.white),
      child: avatar == null || avatar.isEmpty
          ? Icon(Icons.person, size: size * .55, color: _muted)
          : Image.network(avatar,
              fit: BoxFit.cover,
              errorBuilder: (_, __, ___) =>
                  Icon(Icons.person, size: size * .55, color: _muted)),
    );
  }

  void _showActionSheet(BuildContext context) {
    Get.bottomSheet(
      InteractActionBottomSheet(controller: controller),
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
    );
  }
}

class InteractActionBottomSheet extends StatelessWidget {
  const InteractActionBottomSheet({required this.controller, super.key});

  final InteractController controller;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      top: false,
      child: Container(
        height: 331,
        decoration: const BoxDecoration(
          image: DecorationImage(
              image: AssetImage('assets/images/interact/bottom_sheet_bg.png'),
              fit: BoxFit.fill),
        ),
        child: Stack(
          children: [
            Align(
              alignment: const Alignment(.2, -.98),
              child: Image.asset(
                  'assets/images/interact/bottom_sheet_label.png',
                  width: 126),
            ),
            Column(
              children: [
                const SizedBox(height: 18),
                _sheetAvatar(),
                const SizedBox(height: 13),
                const Text('给搭子送去一份互动',
                    style: TextStyle(color: Color(0xff908b91), fontSize: 14)),
                const SizedBox(height: 3),
                const Text('想念要说出来',
                    style: TextStyle(
                        color: Color(0xff896cdc),
                        fontSize: 24,
                        fontWeight: FontWeight.w700)),
                const SizedBox(height: 24),
                Obx(
                  () => Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: InteractAction.values
                        .map((action) => Padding(
                              padding:
                                  const EdgeInsets.symmetric(horizontal: 6),
                              child: _sheetButton(action),
                            ))
                        .toList(growable: false),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _sheetAvatar() {
    final avatar = controller.partner?.avatar;
    return Container(
      width: 56,
      height: 56,
      clipBehavior: Clip.antiAlias,
      decoration: BoxDecoration(
          shape: BoxShape.circle,
          border: Border.all(color: const Color(0xff896cdc), width: 2)),
      child: avatar == null || avatar.isEmpty
          ? const Icon(Icons.person, color: Color(0xff908b91))
          : Image.network(avatar, fit: BoxFit.cover),
    );
  }

  Widget _sheetButton(InteractAction action) {
    final enabled =
        controller.isPaired && (!action.needsVip || controller.isVip);
    final sending = controller.sendingAction.value == action;
    return SizedBox(
      width: action == InteractAction.stick ? 112 : 88,
      height: action == InteractAction.stick ? 74 : 56,
      child: ElevatedButton(
        onPressed: sending
            ? null
            : () async {
                await controller.sendAction(
                  action,
                  actionVariables: const InteractionDataAction(objectTarget: 2),
                );
                if (controller.sendingAction.value == null) Get.back();
              },
        style: ElevatedButton.styleFrom(
          padding: EdgeInsets.zero,
          backgroundColor:
              const Color(0xff896cdc).withValues(alpha: enabled ? 1 : .4),
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(28)),
        ),
        child: sending
            ? const CircularProgressIndicator(
                color: Colors.white, strokeWidth: 2)
            : Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image.asset(
                      enabled
                          ? action.iconAsset
                          : 'assets/images/interact/icon_locked.png',
                      height: 38),
                  Image.asset(
                      enabled ? action.titleAsset : action.lockedTitleAsset,
                      height: 18),
                ],
              ),
      ),
    );
  }
}