import 'package:flutter/material.dart';
import 'package:flagship/flagship.dart';
import 'package:abtasty_qa_assistant/abtasty_qa_assistant.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'ABTasty QA Demo',
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
ABTastyQAAssistant? _qaAssistant;
String _flagValue = "Loading...";
@override
void initState() {
super.initState();
_initFlagship();
}
Future<void> _initFlagship() async {
// 1. Initialize Flagship
await Flagship.start(
"YOUR_ENVIRONMENT_ID",
"YOUR_API_KEY",
);
// 2. Create a visitor
Flagship.newVisitor(
visitorId: "user_123",
hasConsented: true,
).withContext({
"isQA": true,
}).build();
// 3. Fetch flags
await Flagship.getCurrentVisitor()?.fetchFlags();
// 4. Setup live updates
Flagship.getCurrentVisitor()?.onFlagUpdate = (changedKeys) {
print('π Flags updated: $changedKeys');
_updateFlagValue();
};
_updateFlagValue();
}
void _updateFlagValue() {
setState(() {
_flagValue = Flagship.getCurrentVisitor()
?.getFlag("btnTitle")
.value("Default Button") ?? "No value";
});
}
void _toggleQA() {
if (_qaAssistant == null) {
_qaAssistant = ABTastyQAAssistant(
"YOUR_ENVIRONMENT_ID",
"YOUR_API_KEY",
onClose: () {
print('QA Assistant closed');
},
);
_qaAssistant?.showOverlayButton(context);
} else {
_qaAssistant?.hideOverlayButton();
_qaAssistant?.dispose();
_qaAssistant = null;
}
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ABTasty QA Demo'),
leading: IconButton(
icon: Icon(
_qaAssistant?.isOverlayVisible ?? false
? Icons.visibility_off
: Icons.bug_report,
),
onPressed: _toggleQA,
tooltip: 'Toggle QA Assistant',
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Flag Value:',
style: TextStyle(fontSize: 18),
),
const SizedBox(height: 8),
Text(
_flagValue,
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
const SizedBox(height: 32),
ElevatedButton(
onPressed: _toggleQA,
child: Text(
_qaAssistant?.isOverlayVisible ?? false
? 'Hide QA Assistant'
: 'Show QA Assistant',
),
),
],
),
),
);
}
@override
void dispose() {
Flagship.getCurrentVisitor()?.onFlagUpdate = null;
_qaAssistant?.dispose();
super.dispose();
}
}