debug_environment_view.dart 3.71 KB
import 'package:doublefeel_flutter/app/routes/app_pages.dart';
import 'package:doublefeel_flutter/core/theme/app_theme.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../core/config/app_environment.dart';
import '../../../../core/config/app_environment_config.dart';
import '../../../../core/constants/app_const.dart';
import '../../../../core/network/dio_client.dart';
import '../../../../core/services/web_socket_service.dart';
import '../../../../core/util/app_toast.dart';

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

  @override
  Widget build(BuildContext context) {
    final environmentConfig = Get.find<AppEnvironmentConfig>();
    final dioClient = Get.find<DioClient>();

    return Scaffold(
      backgroundColor: const Color(0xFFF7F6FA),
      appBar: AppBar(
        title: const Text('Env'),
        centerTitle: true,
        backgroundColor: Colors.white,
        foregroundColor: context.colors.textPrimary,
        elevation: 0,
      ),
      body: SafeArea(
        child: Obx(() {
          final current = environmentConfig.environment.value;
          final region = environmentConfig.region.value;

          return ListView(
            padding: const EdgeInsets.all(16),
            children: [
              Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(16),
                ),
                child: Column(
                  children: [
                    for (final environment in AppEnvironment.values)
                      RadioListTile<AppEnvironment>(
                        value: environment,
                        groupValue: current,
                        activeColor: context.colors.primary,
                        title: Text(
                          environment.debugName,
                          style: TextStyle(
                            color: context.colors.textPrimary,
                            fontSize: 16,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        subtitle: Text(
                          environmentConfig.resolveServerUrlFor(
                            environment,
                            region == AppRegion.china
                                ? AppConst.serverBaseUrl
                                : AppConst.serverBaseUrlGlobal,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                        onChanged: (value) async {
                          if (value == null || value == current) return;

                          await environmentConfig.setEnvironment(value);
                          dioClient.refreshBaseUrl();
                          Get.find<WebSocketService>().reconnect();
                          AppToast.show(
                            '环境已切换: ${environmentConfig.serverBaseUrl}',
                          );
                        },
                      ),
                  ],
                ),
              ),
              SizedBox(
                height: 20,
              ),
              ElevatedButton(
                onPressed: () {
                  Get.toNamed(Routes.DEVELOPER_OPTIONS);
                },
                child: const Text('DEVELOPER OPTIONS'),
              ),
            ],
          );
        }),
      ),
    );
  }
}

extension on AppEnvironment {
  String get debugName {
    return switch (this) {
      AppEnvironment.dev => 'Dev',
      AppEnvironment.xlab => 'XLab',
      AppEnvironment.release => 'Release',
    };
  }
}