cloudflare_turnstile 2.0.3 copy "cloudflare_turnstile: ^2.0.3" to clipboard
cloudflare_turnstile: ^2.0.3 copied to clipboard

A free CAPTCHAs alternative to enhance security and protect against bots.

example/lib/main.dart

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

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

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final TurnstileController _controller = TurnstileController();
  final TurnstileOptions _options = TurnstileOptions(
    size: TurnstileSize.normal,
    theme: TurnstileTheme.light,
    refreshExpired: TurnstileRefreshExpired.manual,
    language: 'en',
    retryAutomatically: false,
  );

  String? _token;

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Builder(
        builder: (context) => Scaffold(
          body: Center(
            child: SingleChildScrollView(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  ConstrainedBox(
                    constraints: const BoxConstraints(maxWidth: 700),
                    child: _token != null
                        ? SelectableText(_token!)
                        : const CircularProgressIndicator(),
                  ),
                  const SizedBox(height: 48.0),
                  CloudFlareTurnstile(
                    siteKey: '3x00000000000000000000FF',
                    mode: TurnstileMode.managed,
                    options: _options,
                    controller: _controller,
                    onTokenRecived: (token) {
                      setState(() {
                        _token = token;
                      });
                    },
                    onTokenExpired: () {},
                    errorBuilder: (context, error) {
                      return Text(error.message);
                    },
                  ),
                  const SizedBox(height: 48.0),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      ElevatedButton(
                        onPressed: () async {
                          setState(() {
                            _token = null;
                          });

                          await _controller.refreshToken();
                        },
                        child: const Text('Refresh Token'),
                      ),
                      const SizedBox(width: 16.0),
                      ElevatedButton(
                        onPressed: () {
                          _controller.isExpired().then((isExpired) {
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(
                                content: Text(
                                    'Token is ${isExpired ? "Expired" : "Valid"}'),
                              ),
                            );
                          });
                        },
                        child: const Text('Validate Token'),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
14
likes
160
pub points
85%
popularity

Publisher

unverified uploader

A free CAPTCHAs alternative to enhance security and protect against bots.

Repository (GitHub)
View/report issues

Topics

#hcaptcha #recaptcha #cloudflare-turnstile

Documentation

API reference

License

MIT (license)

Dependencies

flutter, webview_flutter, webview_flutter_android, webview_flutter_wkwebview

More

Packages that depend on cloudflare_turnstile