optareid_flutter 0.1.4
optareid_flutter: ^0.1.4 copied to clipboard
Official Optare ID SDK for Flutter - Authentication, licensing, and user management
example/lib/main.dart
// GENERATED CODE - DO NOT MODIFY BY HAND
// Run with: flutter run example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:optareid_flutter/optareid_flutter.dart';
/// Example app demonstrating Optare ID Flutter SDK usage.
///
/// Before running:
/// 1. Replace 'your-client-id' with your actual Optare client ID
/// 2. Configure redirect URI in Optare Dashboard
/// 3. Setup iOS/Android deep link handling (see README)
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Initialize Optare Auth SDK
await OptareAuth.init(
clientId: 'your-client-id',
redirectUri: 'myapp://callback',
);
runApp(const OptareExampleApp());
}
/// Main application widget.
class OptareExampleApp extends StatelessWidget {
const OptareExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Optare ID Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
useMaterial3: true,
),
home: OptareAuth.isAuthenticated
? const HomeScreen()
: const LoginScreen(),
);
}
}
/// Login screen with Optare sign-in button.
class LoginScreen extends StatelessWidget {
const LoginScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Optare ID Example')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.lock_outline, size: 64, color: Colors.blue),
const SizedBox(height: 24),
const Text(
'Welcome to Optare ID',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
const Text('Sign in to continue'),
const SizedBox(height: 32),
ElevatedButton.icon(
onPressed: () async {
try {
await OptareAuth.login();
} on OptareAuthException catch (e) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Login failed: ${e.message}')),
);
}
}
},
icon: const Icon(Icons.login),
label: const Text('Sign in with Optare'),
),
],
),
),
);
}
}
/// Home screen showing user info after login.
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
OptareUser? _user;
bool _loading = true;
@override
void initState() {
super.initState();
_loadUser();
}
Future<void> _loadUser() async {
final user = await OptareAuth.getUser();
if (mounted) {
setState(() {
_user = user;
_loading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
actions: [
IconButton(
icon: const Icon(Icons.logout),
onPressed: () async {
await OptareAuth.logout();
if (context.mounted) {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (_) => const LoginScreen()),
);
}
},
),
],
),
body: _loading
? const Center(child: CircularProgressIndicator())
: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (_user?.picture != null)
CircleAvatar(
backgroundImage: NetworkImage(_user!.picture!),
radius: 50,
)
else
const CircleAvatar(
radius: 50,
child: Icon(Icons.person, size: 50),
),
const SizedBox(height: 16),
Text(
'Welcome, ${_user?.name ?? 'User'}!',
style: Theme.of(context).textTheme.headlineSmall,
),
Text(_user?.email ?? ''),
const SizedBox(height: 24),
// Show licenses
if (_user?.licenses.isNotEmpty ?? false) ...[
const Text('Licenses:',
style: TextStyle(fontWeight: FontWeight.bold)),
...(_user!.licenses.map((l) => Text('• $l'))),
],
// Show entitlements
if (_user?.entitlements.isNotEmpty ?? false) ...[
const SizedBox(height: 16),
const Text('Entitlements:',
style: TextStyle(fontWeight: FontWeight.bold)),
...(_user!.entitlements.map((e) => Text('• $e'))),
],
],
),
),
);
}
}