account_apple_page_v2.dart 6.97 KB
import 'package:appframe/bloc/setting/account_apple_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

class AccountApplePageV2 extends StatelessWidget {
  const AccountApplePageV2({super.key});

  @override
  Widget build(BuildContext context) {
    final Map<String, dynamic>? extraData = GoRouterState.of(context).extra as Map<String, dynamic>?;
    var appleId = extraData?['appleId'] ?? '';

    return BlocProvider(
      create: (context) => AccountAppleCubit(AccountAppleState(appleId: appleId)),
      child: BlocConsumer<AccountAppleCubit, AccountAppleState>(
        builder: (context, state) {
          return Scaffold(
            backgroundColor: Colors.white,
            appBar: AppBar(
              title: const Text(
                'Apple账号',
                style: TextStyle(
                  color: Color(0xFF333333),
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                  height: 20 / 16,
                ),
              ),
              centerTitle: true,
              backgroundColor: Colors.white,
              elevation: 0,
              iconTheme: const IconThemeData(color: Colors.black),
            ),
            body: (state.appleId != '' && !state.allowBind)
                ? _buildBound(context, state)
                : _buildPlaceholder(context, state),
          );
        },
        listener: (context, state) {},
      ),
    );
  }

  // 已绑定状态视图
  Widget _buildBound(BuildContext context, AccountAppleState state) {
    return Column(
      children: [
        const SizedBox(height: 80),
        const Text(
          '已绑定Apple账号',
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.normal,
            letterSpacing: 0,
            color: Color(0xFF333333),
          ),
        ),
        const SizedBox(height: 15),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 15),
          child: Text(
            state.appleId,
            style: const TextStyle(
              fontSize: 21,
              fontWeight: FontWeight.bold,
              color: Color(0xFF333333),
            ),
          ),
        ),
        const Spacer(),
        Padding(
          padding: const EdgeInsets.only(left: 15, right: 15, bottom: 15),
          child: SizedBox(
            width: double.infinity,
            height: 44,
            child: ElevatedButton(
              onPressed: () {
                context.read<AccountAppleCubit>().change();
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: const Color(0xFF7691FA),
                foregroundColor: Colors.white,
                elevation: 0,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(22),
                ),
              ),
              child: const Text(
                '更换绑定Apple账号',
                style: TextStyle(
                  fontSize: 16,
                  height: 20 / 16,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(left: 15, right: 15, bottom: 40),
          child: SizedBox(
            width: double.infinity,
            height: 44,
            child: OutlinedButton(
              onPressed: () => _showUnbindDialog(context),
              style: OutlinedButton.styleFrom(
                foregroundColor: const Color(0xFF333333),
                side: const BorderSide(color: Color(0xFFE5E5E5), width: 0.5),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(26),
                ),
              ),
              child: const Text(
                '解绑',
                style: TextStyle(
                  fontSize: 16,
                  height: 20 / 16,
                  color: Color(0xFF333333),
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }

  void _showUnbindDialog(BuildContext context) {
    showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext ctx) {
        return AlertDialog(
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(5),
            ),
          ),
          title: const Text(
            '提示',
            style: TextStyle(
              fontSize: 17,
              color: Color(0xFF000000),
            ),
            textAlign: TextAlign.center,
          ),
          content: const Text.rich(
            TextSpan(
              text: '解绑后将无法继续使用Apple账号登录该班小二账号',
              style: TextStyle(color: Color(0xFF666666), fontSize: 14),
            ),
          ),
          actions: [
            Table(
              children: [
                TableRow(
                  children: [
                    TableCell(
                      child: TextButton(
                        onPressed: () {
                          Navigator.of(ctx).pop();
                        },
                        style: TextButton.styleFrom(
                          foregroundColor: const Color(0xFF333333),
                          textStyle: const TextStyle(fontSize: 17),
                          minimumSize: const Size.fromHeight(40),
                          padding: EdgeInsets.zero,
                          shape: const RoundedRectangleBorder(
                            borderRadius: BorderRadius.zero,
                          ),
                        ),
                        child: const Text('取消'),
                      ),
                    ),
                    TableCell(
                      child: TextButton(
                        onPressed: () {
                          Navigator.of(ctx).pop();
                          context.read<AccountAppleCubit>().unbindApple();
                        },
                        style: TextButton.styleFrom(
                          foregroundColor: const Color(0xFF7691FA),
                          textStyle: const TextStyle(fontSize: 17),
                          minimumSize: const Size.fromHeight(40),
                          padding: EdgeInsets.zero,
                          shape: const RoundedRectangleBorder(
                            borderRadius: BorderRadius.zero,
                          ),
                        ),
                        child: const Text('确定'),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ],
        );
      },
    );
  }

  // 未绑定/更换绑定占位视图
  Widget _buildPlaceholder(BuildContext context, AccountAppleState state) {
    return const Center(
      child: Text(
        '未绑定Apple账号',
        style: TextStyle(
          fontSize: 16,
          color: Color(0xFF999999),
        ),
      ),
    );
  }
}