onboardsync 0.1.0
onboardsync: ^0.1.0 copied to clipboard
Flutter SDK for OnboardSync - Remote configuration platform for mobile app onboarding flows with A/B testing capabilities
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:onboardsync/onboardsync.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'OnboardSync Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _status = 'Not started';
bool _isLoading = false;
bool _testingEnabled = false;
Future<void> _showOnboarding() async {
setState(() {
_isLoading = true;
_status = 'Loading...';
});
try {
// Show the onboarding flow
await OnboardSync.showOnboarding(
context,
projectId: '[project_key]', // Replace with your actual project ID
secretKey: '[secret_key]', // Replace with your secret key
testingEnabled: _testingEnabled,
onComplete: ([result]) {
debugPrint('[Example] Onboarding completed!');
// Process form responses if available
if (result != null && result.hasResponses) {
debugPrint('[Example] Received ${result.responseCount} form responses:');
for (final response in result.responses) {
debugPrint('[Example] Q: "${response.questionText}"');
debugPrint('[Example] A: ${response.answer}');
debugPrint('[Example] Type: ${response.questionType}');
}
// Example: Get a specific answer by question text
final nameResponse = result.getResponseByQuestion('What is your name?');
if (nameResponse != null) {
debugPrint('[Example] User name: ${nameResponse.answerAsString}');
}
} else {
debugPrint('[Example] No form responses received');
}
setState(() {
_status = result != null && result.hasResponses
? 'Completed with ${result.responseCount} responses'
: 'Onboarding completed (no responses)';
});
},
);
// If we get here without the onComplete callback,
// it means onboarding was already completed
if (_status != 'Onboarding completed successfully' &&
!_status.contains('Completed')) {
setState(() {
_status = 'Onboarding already completed or skipped';
});
}
} catch (e) {
setState(() {
_status = 'Error: $e';
});
} finally {
setState(() {
_isLoading = false;
});
}
}
Future<void> _resetOnboarding() async {
try {
// Clear the completion status from SharedPreferences
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('onboarding_completed', false);
setState(() {
_status = 'Onboarding reset successfully';
});
} catch (e) {
setState(() {
_status = 'Error resetting: $e';
});
}
}
Future<void> _checkStatus() async {
try {
// Check completion status from SharedPreferences
final prefs = await SharedPreferences.getInstance();
final hasCompleted = prefs.getBool('onboarding_completed') ?? false;
setState(() {
_status = hasCompleted
? 'Onboarding has been completed'
: 'Onboarding not yet completed';
});
} catch (e) {
setState(() {
_status = 'Error checking status: $e';
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text('OnboardSync Example'),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'OnboardSync Flutter SDK Example',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(8),
),
child: Text(
'Status: $_status',
style: const TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
),
const SizedBox(height: 40),
if (_isLoading)
const CircularProgressIndicator()
else
Column(
children: [
ElevatedButton.icon(
onPressed: _showOnboarding,
icon: const Icon(Icons.play_arrow),
label: const Text('Show Onboarding'),
style: ElevatedButton.styleFrom(
minimumSize: const Size(200, 48),
),
),
const SizedBox(height: 16),
OutlinedButton.icon(
onPressed: _checkStatus,
icon: const Icon(Icons.info_outline),
label: const Text('Check Status'),
style: OutlinedButton.styleFrom(
minimumSize: const Size(200, 48),
),
),
const SizedBox(height: 16),
TextButton.icon(
onPressed: _resetOnboarding,
icon: const Icon(Icons.refresh),
label: const Text('Reset Onboarding'),
style: TextButton.styleFrom(
minimumSize: const Size(200, 48),
),
),
],
),
const SizedBox(height: 20),
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Testing Mode',
style: TextStyle(fontSize: 16),
),
Switch(
value: _testingEnabled,
onChanged: (value) {
setState(() {
_testingEnabled = value;
});
},
),
],
),
),
),
const SizedBox(height: 20),
const Card(
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Setup Instructions:',
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: 8),
Text('1. Replace [project_key] with your actual project ID'),
Text('2. Replace [secret_key] with your secret key'),
Text('3. Configure your app permissions in iOS/Android'),
Text('4. Run the app and tap "Show Onboarding"'),
SizedBox(height: 8),
Text(
'Testing Mode: Shows onboarding every time regardless of completion status',
style: TextStyle(fontSize: 12, fontStyle: FontStyle.italic),
),
],
),
),
),
],
),
),
),
);
}
}