dynamic_sdk 1.3.5 copy "dynamic_sdk: ^1.3.5" to clipboard
dynamic_sdk: ^1.3.5 copied to clipboard

Dynamic SDK

example/lib/main.dart

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

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Dynamic SDK Example',
      theme: ThemeData(
        colorSchemeSeed: Colors.deepPurple,
        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 DynamicSDK _sdk;

  @override
  void initState() {
    super.initState();
    _sdk = DynamicSDK.init(
      props: ClientProps(environmentId: 'YOUR_ENVIRONMENT_ID'),
    );
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Dynamic SDK Example'),
          bottom: const TabBar(
            tabs: [
              Tab(text: 'Home'),
              Tab(text: 'Bitcoin'),
              Tab(text: 'TON'),
              Tab(text: 'SUI'),
            ],
          ),
        ),
        body: TabBarView(
          children: [
            _buildHomeTab(),
            BitcoinTab(sdk: _sdk),
            TonTab(sdk: _sdk),
            SuiTab(sdk: _sdk),
          ],
        ),
      ),
    );
  }

  Widget _buildHomeTab() {
    return Column(
      children: [
        Expanded(child: _sdk.dynamicWidget),
      ],
    );
  }
}

// ---------------------------------------------------------------------------
// Bitcoin Tab
// ---------------------------------------------------------------------------

class BitcoinTab extends StatefulWidget {
  const BitcoinTab({super.key, required this.sdk});
  final DynamicSDK sdk;

  @override
  State<BitcoinTab> createState() => _BitcoinTabState();
}

class _BitcoinTabState extends State<BitcoinTab> {
  final _walletIdController = TextEditingController();
  String _result = '';
  bool _loading = false;

  BitcoinModule get _btc => widget.sdk.bitcoin;

  void _run(Future<Object> Function() fn) async {
    setState(() {
      _loading = true;
      _result = '';
    });
    try {
      final res = await fn();
      setState(() => _result = res.toString());
    } catch (e) {
      setState(() => _result = 'Error: $e');
    } finally {
      setState(() => _loading = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    final walletId = _walletIdController.text;

    return ListView(
      padding: const EdgeInsets.all(16),
      children: [
        TextField(
          controller: _walletIdController,
          decoration: const InputDecoration(
            labelText: 'Wallet ID',
            border: OutlineInputBorder(),
          ),
          onChanged: (_) => setState(() {}),
        ),
        const SizedBox(height: 16),
        _ActionButton(
          label: 'Get Balance',
          onPressed: () => _run(
            () => _btc.getBalance(walletId: walletId),
          ),
        ),
        _ActionButton(
          label: 'Sign Message',
          onPressed: () => _run(
            () => _btc.signMessage(
              walletId: walletId,
              message: 'Hello Bitcoin',
            ),
          ),
        ),
        _ActionButton(
          label: 'Send Bitcoin',
          onPressed: () => _run(
            () => _btc.sendBitcoin(
              walletId: walletId,
              recipientAddress: 'bc1q...',
              amount: '1000',
              feePriority: 'medium',
            ),
          ),
        ),
        _ActionButton(
          label: 'Build PSBT',
          onPressed: () => _run(
            () => _btc.buildPsbt(
              walletId: walletId,
              recipientAddress: 'bc1q...',
              amount: '1000',
            ),
          ),
        ),
        _ActionButton(
          label: 'Sign PSBT',
          onPressed: () => _run(
            () => _btc.signPsbt(
              walletId: walletId,
              request: {'unsignedPsbtBase64': 'base64...'},
            ),
          ),
        ),
        const SizedBox(height: 16),
        _ResultCard(result: _result, loading: _loading),
      ],
    );
  }

  @override
  void dispose() {
    _walletIdController.dispose();
    super.dispose();
  }
}

// ---------------------------------------------------------------------------
// TON Tab
// ---------------------------------------------------------------------------

class TonTab extends StatefulWidget {
  const TonTab({super.key, required this.sdk});
  final DynamicSDK sdk;

  @override
  State<TonTab> createState() => _TonTabState();
}

class _TonTabState extends State<TonTab> {
  final _walletIdController = TextEditingController();
  String _result = '';
  bool _loading = false;

  TonModule get _ton => widget.sdk.ton;

  void _run(Future<Object> Function() fn) async {
    setState(() {
      _loading = true;
      _result = '';
    });
    try {
      final res = await fn();
      setState(() => _result = res.toString());
    } catch (e) {
      setState(() => _result = 'Error: $e');
    } finally {
      setState(() => _loading = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    final walletId = _walletIdController.text;

    return ListView(
      padding: const EdgeInsets.all(16),
      children: [
        TextField(
          controller: _walletIdController,
          decoration: const InputDecoration(
            labelText: 'Wallet ID',
            border: OutlineInputBorder(),
          ),
          onChanged: (_) => setState(() {}),
        ),
        const SizedBox(height: 16),
        _ActionButton(
          label: 'Get Balance',
          onPressed: () => _run(
            () => _ton.getBalance(walletId: walletId),
          ),
        ),
        _ActionButton(
          label: 'Sign Message',
          onPressed: () => _run(
            () => _ton.signMessage(
              walletId: walletId,
              message: 'Hello TON',
            ),
          ),
        ),
        _ActionButton(
          label: 'Send TON',
          onPressed: () => _run(
            () => _ton.sendTon(
              walletId: walletId,
              to: 'EQ...',
              amount: '100000000',
            ),
          ),
        ),
        _ActionButton(
          label: 'Send Jetton',
          onPressed: () => _run(
            () => _ton.sendJetton(
              walletId: walletId,
              recipientAddress: 'EQ...',
              jettonAmount: '1000000',
              jettonMasterAddress: 'EQ...',
            ),
          ),
        ),
        _ActionButton(
          label: 'Get Network Details',
          onPressed: () => _run(
            () => _ton.getNetworkDetails(walletId: walletId),
          ),
        ),
        const SizedBox(height: 16),
        _ResultCard(result: _result, loading: _loading),
      ],
    );
  }

  @override
  void dispose() {
    _walletIdController.dispose();
    super.dispose();
  }
}

// ---------------------------------------------------------------------------
// SUI Tab
// ---------------------------------------------------------------------------

class SuiTab extends StatefulWidget {
  const SuiTab({super.key, required this.sdk});
  final DynamicSDK sdk;

  @override
  State<SuiTab> createState() => _SuiTabState();
}

class _SuiTabState extends State<SuiTab> {
  final _walletIdController = TextEditingController();
  String _result = '';
  bool _loading = false;

  SuiModule get _sui => widget.sdk.sui;

  void _run(Future<Object> Function() fn) async {
    setState(() {
      _loading = true;
      _result = '';
    });
    try {
      final res = await fn();
      setState(() => _result = res.toString());
    } catch (e) {
      setState(() => _result = 'Error: $e');
    } finally {
      setState(() => _loading = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    final walletId = _walletIdController.text;

    return ListView(
      padding: const EdgeInsets.all(16),
      children: [
        TextField(
          controller: _walletIdController,
          decoration: const InputDecoration(
            labelText: 'Wallet ID',
            border: OutlineInputBorder(),
          ),
          onChanged: (_) => setState(() {}),
        ),
        const SizedBox(height: 16),
        _ActionButton(
          label: 'Get Network Name',
          onPressed: () => _run(
            () => _sui.getNetworkName(walletId: walletId),
          ),
        ),
        _ActionButton(
          label: 'Sign Message',
          onPressed: () => _run(
            () => _sui.signMessage(
              walletId: walletId,
              message: 'Hello SUI',
            ),
          ),
        ),
        _ActionButton(
          label: 'Sign Transaction',
          onPressed: () => _run(
            () => _sui.signTransaction(
              walletId: walletId,
              transaction: 'base64EncodedTx...',
            ),
          ),
        ),
        _ActionButton(
          label: 'Sign & Send Transfer',
          onPressed: () => _run(
            () => _sui.signAndSendTransferTransaction(
              walletId: walletId,
              to: '0x...',
              value: '0.001',
            ),
          ),
        ),
        _ActionButton(
          label: 'Send Transaction',
          onPressed: () => _run(
            () => _sui.sendTransaction(
              walletId: walletId,
              transaction: 'base64EncodedTx...',
            ),
          ),
        ),
        const SizedBox(height: 16),
        _ResultCard(result: _result, loading: _loading),
      ],
    );
  }

  @override
  void dispose() {
    _walletIdController.dispose();
    super.dispose();
  }
}

// ---------------------------------------------------------------------------
// Shared widgets
// ---------------------------------------------------------------------------

class _ActionButton extends StatelessWidget {
  const _ActionButton({required this.label, required this.onPressed});
  final String label;
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: FilledButton.tonal(
        onPressed: onPressed,
        child: Text(label),
      ),
    );
  }
}

class _ResultCard extends StatelessWidget {
  const _ResultCard({required this.result, required this.loading});
  final String result;
  final bool loading;

  @override
  Widget build(BuildContext context) {
    if (loading) {
      return const Center(child: CircularProgressIndicator());
    }
    if (result.isEmpty) return const SizedBox.shrink();
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: SelectableText(
          result,
          style: Theme.of(context).textTheme.bodyMedium,
        ),
      ),
    );
  }
}