airvoy 1.0.4 copy "airvoy: ^1.0.4" to clipboard
airvoy: ^1.0.4 copied to clipboard

Flutter SDK for Airvoy eSIM connectivity API.

example/lib/main.dart

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

void main() {
  runApp(const ExampleApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Airvoy Example',
      theme: ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue), useMaterial3: true),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late final Airvoy _airvoy;
  Esim? _esim;
  bool _loading = false;
  String? _error;
  String? _status;

  @override
  void initState() {
    super.initState();
    _airvoy = Airvoy(
      apiKey: 'sk_live_your_api_key',
      groupId: 'your_group_id',
    );
  }

  Future<void> _createEsim() async {
    setState(() {
      _loading = true;
      _error = null;
      _status = 'Creating eSIM...';
    });

    try {
      final esim = await _airvoy.createEsim();
      setState(() {
        _esim = esim;
        _status = 'eSIM created';
        _loading = false;
      });
    } on AirvoyException catch (e) {
      setState(() {
        _error = e.message;
        _status = null;
        _loading = false;
      });
    }
  }

  Future<void> _install() async {
    if (_esim == null) return;
    try {
      await _airvoy.installOnDevice(_esim!.activationCode);
      setState(() => _status = 'Installation opened');
    } catch (e) {
      setState(() => _error = e.toString());
    }
  }

  Future<void> _refresh() async {
    if (_esim == null) return;
    setState(() => _loading = true);

    try {
      final esim = await _airvoy.getEsim(_esim!.id);
      setState(() {
        _esim = esim;
        _loading = false;
      });
    } catch (e) {
      setState(() {
        _error = e.toString();
        _loading = false;
      });
    }
  }

  Future<void> _enableFullInternet(int dataMb) async {
    if (_esim == null) return;
    setState(() {
      _loading = true;
      _status = 'Enabling full internet...';
    });

    try {
      final esim = await _airvoy.enableFullInternet(_esim!.id, dataLimitMb: dataMb);
      setState(() {
        _esim = esim;
        _status = 'Full internet enabled';
        _loading = false;
      });
    } catch (e) {
      setState(() {
        _error = e.toString();
        _loading = false;
      });
    }
  }

  Future<void> _disableFullInternet() async {
    if (_esim == null) return;
    setState(() {
      _loading = true;
      _status = 'Disabling full internet...';
    });

    try {
      final esim = await _airvoy.disableFullInternet(_esim!.id);
      setState(() {
        _esim = esim;
        _status = 'Restricted mode restored';
        _loading = false;
      });
    } catch (e) {
      setState(() {
        _error = e.toString();
        _loading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Airvoy Example'),
        actions: [
          if (_esim != null)
            IconButton(icon: const Icon(Icons.refresh), onPressed: _loading ? null : _refresh),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            if (_status != null) _buildStatus(),
            if (_error != null) _buildError(),
            if (_esim == null) _buildWelcome(),
            if (_esim != null) ...[
              EsimCard(
                airvoy: _airvoy,
                esimId: _esim!.id,
                onInstall: _install,
                onUpgrade: _esim!.fullInternet ? null : () => _enableFullInternet(1024),
              ),
              const SizedBox(height: 16),
              if (!_esim!.fullInternet)
                UpsellBanner(
                  airvoy: _airvoy,
                  esimId: _esim!.id,
                  onUpgrade: _enableFullInternet,
                ),
              if (_esim!.fullInternet)
                OutlinedButton.icon(
                  onPressed: _loading ? null : _disableFullInternet,
                  icon: const Icon(Icons.lock),
                  label: const Text('Switch to Restricted Mode'),
                ),
            ],
          ],
        ),
      ),
    );
  }

  Widget _buildStatus() {
    return Container(
      padding: const EdgeInsets.all(12),
      margin: const EdgeInsets.only(bottom: 16),
      decoration: BoxDecoration(color: Colors.blue[50], borderRadius: BorderRadius.circular(8)),
      child: Row(
        children: [
          if (_loading)
            const Padding(
              padding: EdgeInsets.only(right: 12),
              child: SizedBox(width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2)),
            ),
          Text(_status!),
        ],
      ),
    );
  }

  Widget _buildError() {
    return Container(
      padding: const EdgeInsets.all(12),
      margin: const EdgeInsets.only(bottom: 16),
      decoration: BoxDecoration(color: Colors.red[50], borderRadius: BorderRadius.circular(8)),
      child: Row(
        children: [
          Icon(Icons.error, color: Colors.red[700], size: 20),
          const SizedBox(width: 8),
          Expanded(child: Text(_error!, style: TextStyle(color: Colors.red[700]))),
        ],
      ),
    );
  }

  Widget _buildWelcome() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        const Text('Airvoy SDK Example', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
        const SizedBox(height: 8),
        const Text('Create an eSIM to get started.'),
        const SizedBox(height: 24),
        ElevatedButton.icon(
          onPressed: _loading ? null : _createEsim,
          icon: const Icon(Icons.sim_card),
          label: const Text('Create eSIM'),
          style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 16)),
        ),
      ],
    );
  }
}
0
likes
125
points
183
downloads

Publisher

unverified uploader

Weekly Downloads

Flutter SDK for Airvoy eSIM connectivity API.

Homepage

Documentation

Documentation
API reference

License

MIT (license)

Dependencies

flutter, http, qr_flutter, url_launcher

More

Packages that depend on airvoy