Skip to main content

mesh_sdk_flutter 1.1.16 copy "mesh_sdk_flutter: ^1.1.16" to clipboard
mesh_sdk_flutter: ^1.1.16 copied to clipboard

Flutter library for integrating with Mesh Connect.

example/lib/main.dart

// ignore_for_file: avoid_print
import 'package:flutter/material.dart';
import 'package:mesh_sdk_flutter/mesh_sdk_flutter.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      supportedLocales: MeshLocalizations.supportedLocales,
      localizationsDelegates: MeshLocalizations.localizationsDelegates,
      theme: ThemeData.from(
        colorScheme: const ColorScheme.dark(primary: Color(0xFFFAFF6E)),
      ),
      home: const HomePage(),
    );
  }
}

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

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

class _HomePageState extends State<HomePage> {
  final _textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    final linkToken = _textController.text.trim();

    return Scaffold(
      appBar: AppBar(title: const Text('Mesh Example App')),
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              TextField(
                controller: _textController,
                decoration: const InputDecoration(
                  labelText: 'Link Token',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.all(Radius.circular(8)),
                  ),
                ),
                onChanged: (value) => setState(() {}),
              ),
              const SizedBox(height: 32),
              FilledButton(
                onPressed: linkToken.isEmpty
                    ? null
                    : () => _showMeshLinkPage(linkToken),
                child: const Text('Link'),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> _showMeshLinkPage(String linkToken) async {
    // Clear TextField
    _textController.clear();
    setState(() {});

    // Show MeshSdk
    final result = await MeshSdk.show(
      context,
      configuration: MeshConfiguration(
        language: 'system',
        displayFiatCurrency: 'USD',
        theme: ThemeMode.system,
        linkToken: linkToken,
        onEvent: (event) {
          print('Mesh event: $event');
        },
        onError: (errorType) {
          print('Mesh exit: $errorType');
        },
        onSuccess: (payload) {
          print('Mesh success: ${payload.page}');
        },
        onIntegrationConnected: (integration) {
          print('Mesh integration connected: $integration');
        },
        onTransferFinished: (transfer) {
          print('Mesh transfer finished: $transfer');
        },
      ),
    );

    // Handle the result
    switch (result) {
      case MeshSuccess():
        print('Mesh link finished successfully');
      case MeshError():
        print('Mesh link error: ${result.type}');
    }

    // Alternatively, use `when` method
    result.when(
      success: (success) {
        final payload = success.payload;
        print('Mesh link success: ${payload.page}');
      },
      error: (error) {
        final errorType = error.type;
        print('Mesh link error: $errorType');
      },
    );
  }
}