tei_shield_flutter 0.1.10
tei_shield_flutter: ^0.1.10 copied to clipboard
Configurable TEI Shield native Android and iOS assessments for Flutter.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:tei_shield_flutter/tei_shield_flutter.dart';
void main() => runApp(const MaterialApp(home: ShieldExample()));
class ShieldExample extends StatefulWidget {
const ShieldExample({super.key});
@override
State<ShieldExample> createState() => _ShieldExampleState();
}
class _ShieldExampleState extends State<ShieldExample> {
TeiShieldClient? _client;
StreamSubscription<TeiShieldAssessment>? _subscription;
TeiShieldAssessment? _value;
String _error = '';
@override
void initState() {
super.initState();
unawaited(_start());
}
Future<void> _start() async {
try {
final client = await TeiShieldClient.initialize(
options: AssessmentOptions(
checks: [ShieldCheck.emulator, ShieldCheck.sdkTextIntegrity]),
runPolicy: const ShieldRunPolicy(
assessOnInitialize: true, reassessOnForeground: true),
);
if (!mounted) {
await client.close();
return;
}
_subscription = client.assessments.listen(
(value) {
if (mounted) setState(() => _value = value);
},
onError: (Object error) {
if (mounted) setState(() => _error = '$error');
},
);
setState(() {
_client = client;
_value = client.lastAssessment;
});
} catch (error) {
if (mounted) setState(() => _error = '$error');
}
}
Future<void> _run(bool emulatorOnly) async {
try {
if (emulatorOnly) {
await _client?.check(ShieldCheck.emulator);
} else {
await _client?.assess();
}
} catch (error) {
if (mounted) setState(() => _error = '$error');
}
}
@override
void dispose() {
unawaited(_subscription?.cancel());
unawaited(_client?.close());
super.dispose();
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text('TEI Shield')),
body: ListView(padding: const EdgeInsets.all(24), children: [
ElevatedButton(
onPressed: _client == null ? null : () => _run(true),
child: const Text('Check emulator only')),
ElevatedButton(
onPressed: _client == null ? null : () => _run(false),
child: const Text('Assess configured checks')),
for (final signal in _value?.signals ?? <TeiShieldSignal>[])
ListTile(
title: Text(signal.code),
subtitle: Text('${signal.status.name} (${signal.confidence})')),
Text(_error),
]),
);
}