base_url_switcher 2.1.0 copy "base_url_switcher: ^2.1.0" to clipboard
base_url_switcher: ^2.1.0 copied to clipboard

A Flutter package for switching between base URLs with a beautiful UI switcher

Base URL Switcher #

The simplest way to switch Base URLs in Flutter ๐Ÿš€

pub package License: MIT

โšก Ultra Simple - Just 3 Steps! #

1๏ธโƒฃ Add the Package #

dependencies:
  base_url_switcher: ^2.1.0

2๏ธโƒฃ Add URLs in main.dart #

import 'package:base_url_switcher/base_url_switcher.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // Just add URLs - everything else is automatic!
  await EnvService.initialize(
    developmentUrl: 'https://dev-api.mycompany.com',
    productionUrl: 'https://api.mycompany.com',
    defaultEnvironment: EnvironmentType.development,
  );
  
  runApp(MyApp());
}

3๏ธโƒฃ Wrap Your Widget #

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('My App')),
        body: SimpleBaseUrlWrapper(
          // Just wrap the body - everything else is automatic!
          child: YourWidget(),
        ),
      ),
    );
  }
}

๐ŸŽฏ How It Works? #

  1. Tap 7 times anywhere in the app
  2. Enter password (default: "admin")
  3. Switch environment and use the new Base URL

๐Ÿ’ป Using Base URL in Code #

// Anywhere in your app
final url = BaseUrlManager.instance.currentBaseUrl;
final response = await http.get(Uri.parse('$url/api/users'));

๐ŸŽจ Customize Access #

SimpleBaseUrlWrapper(
  password: "myapp123", // Custom password
  tapCount: 5, // 5 taps instead of 7
  child: YourWidget(),
)

๐Ÿ“ฑ Show Environment Info #

AppBar(
  title: Text('My App'),
  actions: [
    EnvironmentIndicator(), // Current environment indicator
  ],
)

// Or anywhere
EnvironmentInfo(
  showBaseUrl: true,
  showDescription: true,
)

๐ŸŒŸ Features #

  • โœ… Setup in 3 steps - No more!
  • โœ… Hidden access - Multiple taps to access
  • โœ… Password protection - Customizable
  • โœ… Ready-to-use screen - No coding needed
  • โœ… Auto save - Remembers your choice
  • โœ… Environment indicator - Shows current environment
  • โœ… Comprehensive tests - 21/21 tests passed

๐Ÿ”ง Advanced Setup #

Add Staging Environment #

await EnvService.initialize(
  developmentUrl: 'https://dev-api.com',
  productionUrl: 'https://api.com',
  stagingUrl: 'https://staging-api.com', // Optional
  defaultEnvironment: EnvironmentType.development,
);

Access Settings Manually #

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => EnvSwitcherScreen(),
  ),
);

Use Widget Instead of Wrapper #

AppBar(
  actions: [
    EnvSwitcher(
      onEnvironmentChanged: (env) {
        print('Switched to ${env.name}');
      },
    ),
  ],
)

๐Ÿ“‹ API Reference #

EnvService.initialize() #

static Future<void> initialize({
  String? developmentUrl,    // Development environment URL
  String? productionUrl,     // Production environment URL
  String? stagingUrl,        // Staging environment URL (optional)
  EnvironmentType? defaultEnvironment, // Default environment
});

BaseUrlManager #

// Get current Base URL
String get currentBaseUrl;

// Get current environment name
String get currentEnvironmentName;

// Get current environment object
Environment get currentEnvironment;

EnvironmentType Enum #

enum EnvironmentType {
  development,
  staging,
  production,
  local,
}

// Usage
EnvironmentType.development.displayName  // "Development"
EnvironmentType.production.description   // "Production environment"

SimpleBaseUrlWrapper #

class SimpleBaseUrlWrapper extends StatelessWidget {
  final Widget child;        // Widget to wrap
  final String? password;    // Password (default: "admin")
  final int? tapCount;       // Number of taps (default: 7)
}

๐Ÿš€ Complete Example #

import 'package:flutter/material.dart';
import 'package:base_url_switcher/base_url_switcher.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  await EnvService.initialize(
    developmentUrl: 'https://dev-api.mycompany.com',
    productionUrl: 'https://api.mycompany.com',
    defaultEnvironment: EnvironmentType.development,
  );
  
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My App'),
          actions: [EnvironmentIndicator()],
        ),
        body: SimpleBaseUrlWrapper(
          password: "myapp123",
          tapCount: 5,
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('Current Environment: ${BaseUrlManager.instance.currentEnvironmentName}'),
                Text('Base URL: ${BaseUrlManager.instance.currentBaseUrl}'),
                ElevatedButton(
                  onPressed: () async {
                    final response = await http.get(
                      Uri.parse('${BaseUrlManager.instance.currentBaseUrl}/api/users'),
                    );
                    print('Response: ${response.body}');
                  },
                  child: Text('Test API Call'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

๐Ÿ“ฆ Installation #

flutter pub add base_url_switcher

๐Ÿงช Testing #

flutter test

๐Ÿ“„ License #

MIT License - see LICENSE file for details.

โญ Support #

If you find this package helpful, please give it a โญ on pub.dev!


Made with โค๏ธ for the Flutter community

7
likes
0
points
23
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package for switching between base URLs with a beautiful UI switcher

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on base_url_switcher