paywallo_flutter 2.9.0 copy "paywallo_flutter: ^2.9.0" to clipboard
paywallo_flutter: ^2.9.0 copied to clipboard

Paywallo SDK for Flutter — paywalls, campaigns, subscriptions, identity, events.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:paywallo_flutter/paywallo.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final paywallo = await PaywalloPlatform.create();
  runApp(PaywalloSampleApp(paywallo: paywallo));
}

class PaywalloSampleApp extends StatelessWidget {
  const PaywalloSampleApp({super.key, required this.paywallo});
  final Paywallo paywallo;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Paywallo Sample',
      theme: ThemeData(colorSchemeSeed: Colors.deepPurple),
      home: SampleHome(paywallo: paywallo),
    );
  }
}

class SampleHome extends StatefulWidget {
  const SampleHome({super.key, required this.paywallo});
  final Paywallo paywallo;

  @override
  State<SampleHome> createState() => _SampleHomeState();
}

class _SampleHomeState extends State<SampleHome> {
  String _status = 'Initializing…';
  String _distinctId = '(pending)';

  @override
  void initState() {
    super.initState();
    _bootstrap();
  }

  Future<void> _bootstrap() async {
    try {
      await widget.paywallo.initialize(
        // Sem `apiUrl` o SDK usa a base de producao correta.
        const PaywalloInitConfig(appKey: 'pk_sample_demo', debug: true),
      );
      if (mounted) setState(() => _status = 'Ready');
    } catch (e) {
      if (mounted) setState(() => _status = 'Init failed: $e');
    }
    if (mounted) {
      setState(() => _distinctId = widget.paywallo.getDistinctId());
    }
  }

  Future<void> _identify() async {
    try {
      await widget.paywallo.identify(
        const IdentifyOptions(email: 'ada@example.com'),
      );
      if (mounted) setState(() => _status = 'Identified ada@example.com');
    } catch (e) {
      if (mounted) setState(() => _status = 'Identify failed: $e');
    }
  }

  Future<void> _track() async {
    await widget.paywallo.track('sample_button_tapped');
    if (mounted) setState(() => _status = 'Tracked sample_button_tapped');
  }

  Future<void> _startSession() async {
    final sid = await widget.paywallo.startSession();
    if (mounted) setState(() => _status = 'Started session $sid');
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    return Scaffold(
      appBar: AppBar(title: const Text('Paywallo Sample')),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Text('Status', style: theme.textTheme.titleMedium),
            Text(_status),
            const SizedBox(height: 16),
            Text('Distinct ID', style: theme.textTheme.titleMedium),
            Text(_distinctId),
            const SizedBox(height: 16),
            Text('Session', style: theme.textTheme.titleMedium),
            Text(widget.paywallo.getSessionId() ?? '(none)'),
            const SizedBox(height: 32),
            FilledButton(
              onPressed: _identify,
              child: const Text('Identify ada@example.com'),
            ),
            const SizedBox(height: 8),
            FilledButton.tonal(
              onPressed: _track,
              child: const Text('Track event'),
            ),
            const SizedBox(height: 8),
            FilledButton.tonal(
              onPressed: _startSession,
              child: const Text('Start session'),
            ),
          ],
        ),
      ),
    );
  }
}