debug_environment_view.dart 3.12 KB
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';

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

  static const _brandColor = Color(0xFF845EEE);
  static const _titleColor = Color(0xFF0F0F11);

  @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('Debug'),
        centerTitle: true,
        backgroundColor: Colors.white,
        foregroundColor: _titleColor,
        elevation: 0,
      ),
      body: SafeArea(
        child: Obx(() {
          final current = environmentConfig.environment.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: _brandColor,
                        title: Text(
                          environment.debugName,
                          style: const TextStyle(
                            color: _titleColor,
                            fontSize: 16,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        subtitle: Text(
                          environmentConfig.resolveServerUrlFor(
                            environment,
                            AppConst.serverBaseUrl,
                          ),
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ),
                        onChanged: (value) async {
                          if (value == null || value == current) return;

                          await environmentConfig.setEnvironment(value);
                          dioClient.refreshBaseUrl();
                          Get.snackbar(
                            '环境已切换',
                            environmentConfig.serverBaseUrl,
                            snackPosition: SnackPosition.BOTTOM,
                          );
                        },
                      ),
                  ],
                ),
              ),
            ],
          );
        }),
      ),
    );
  }
}

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