shotik 0.2.0
shotik: ^0.2.0 copied to clipboard
Shotik SDK for Flutter. In-app support chat answered from Shotik and product analytics, with no third-party dependencies.
example/lib/main.dart
// Shotik Sample: onboarding → paywall → settings with a Help row, a masked field and sign-in. The key and server come
// from --dart-define SHOTIK_APP_KEY / SHOTIK_SERVER_URL. Widgets only, like the SDK: no Material or Cupertino dependency.
import 'package:flutter/widgets.dart';
import 'package:shotik/shotik.dart';
const appKey = String.fromEnvironment('SHOTIK_APP_KEY');
const serverUrl = String.fromEnvironment('SHOTIK_SERVER_URL', defaultValue: 'http://127.0.0.1:4590');
/// configure() time on the UI isolate, measured around the call (logged as "[shotik] configure N us").
int configureMicros = 0;
void main() {
final sw = Stopwatch()..start();
Shotik.configure(appKey: appKey, serverUrl: serverUrl, debug: const bool.fromEnvironment('SHOTIK_DEBUG'));
sw.stop();
configureMicros = sw.elapsedMicroseconds;
debugPrint('[shotik] configure $configureMicros us');
ShotikSupport.identify();
runApp(const ShotikRoot(child: SampleApp()));
}
const _ink = Color(0xFF0B0B0C);
const _sub = Color(0xFF6B6B73);
const _raised = Color(0xFFF2F2F4);
const _accent = Color(0xFF007AFF);
class SampleApp extends StatelessWidget {
const SampleApp({super.key});
@override
Widget build(BuildContext context) {
return WidgetsApp(
title: 'Shotik Sample',
color: _accent,
debugShowCheckedModeBanner: false,
textStyle: const TextStyle(fontSize: 16, color: _ink),
pageRouteBuilder: <T>(RouteSettings settings, WidgetBuilder builder) => PageRouteBuilder<T>(settings: settings, pageBuilder: (context, _, __) => builder(context)),
navigatorObservers: [ShotikAnalytics.navigatorObserver],
builder: ShotikSupportWidget.builder(title: 'Shotik Sample', launcher: true, launcherBottom: 132, quickReplies: const ['Billing', 'Report a bug']),
initialRoute: '/onboarding',
routes: {
'/onboarding': (_) => const Onboarding(),
'/paywall': (_) => const Paywall(),
'/settings': (_) => const Settings(),
},
);
}
}
class Pill extends StatelessWidget {
const Pill({super.key, required this.text, required this.onTap, this.filled = true});
final String text;
final VoidCallback onTap;
final bool filled;
@override
Widget build(BuildContext context) {
return Semantics(
button: true,
child: GestureDetector(
onTap: onTap,
child: Container(
constraints: const BoxConstraints(minHeight: 46),
alignment: Alignment.center,
decoration: BoxDecoration(color: filled ? _accent : _raised, borderRadius: BorderRadius.circular(23)),
child: Text(text, style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600, color: filled ? const Color(0xFFFFFFFF) : _ink)),
),
),
);
}
}
class SampleScreen extends StatelessWidget {
const SampleScreen({super.key, required this.title, required this.children});
final String title;
final List<Widget> children;
@override
Widget build(BuildContext context) {
return ColoredBox(
color: const Color(0xFFFFFFFF),
child: SafeArea(
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 24, 20, 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(title, style: const TextStyle(fontSize: 28, fontWeight: FontWeight.w700, color: _ink)),
const SizedBox(height: 16),
...children,
],
),
),
),
);
}
}
class Onboarding extends StatelessWidget {
const Onboarding({super.key});
@override
Widget build(BuildContext context) {
return SampleScreen(title: 'Walk more, every day', children: [
const Text('Three short steps and you are set.', style: TextStyle(fontSize: 16, color: _sub)),
const Spacer(),
Pill(key: const ValueKey('continue'), text: 'Continue', onTap: () => Navigator.of(context).pushNamed('/paywall')),
]);
}
}
class Paywall extends StatelessWidget {
const Paywall({super.key});
@override
Widget build(BuildContext context) {
return ShotikScreen(
name: 'Paywall',
child: SampleScreen(title: 'Go Pro', children: [
const PlanCard(),
const Spacer(),
Pill(
key: const ValueKey('annual'),
text: 'Start trial',
onTap: () {
ShotikAnalytics.purchase(product: 'pro.annual', status: 'start', price: 39.99, currency: 'USD', store: 'app_store', trial: true);
ShotikAnalytics.purchase(product: 'pro.annual', status: 'done', price: 39.99, currency: 'USD', store: 'app_store', trial: true);
Navigator.of(context).pushReplacementNamed('/settings');
},
),
const SizedBox(height: 8),
Pill(text: 'Not now', filled: false, onTap: () => Navigator.of(context).pushReplacementNamed('/settings')),
]),
);
}
}
class PlanCard extends StatelessWidget {
const PlanCard({super.key});
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(color: _raised, borderRadius: BorderRadius.circular(18)),
child: const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Annual', style: TextStyle(fontSize: 17, fontWeight: FontWeight.w600, color: _ink)),
SizedBox(height: 4),
Text('\$39.99 a year · 7 days free', style: TextStyle(fontSize: 15, color: _sub)),
],
),
);
}
}
class PromoField extends StatefulWidget {
const PromoField({super.key});
@override
State<PromoField> createState() => _PromoFieldState();
}
class _PromoFieldState extends State<PromoField> {
final _text = TextEditingController();
final _focus = FocusNode();
@override
void dispose() {
_text.dispose();
_focus.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
const style = TextStyle(fontSize: 17, color: _ink);
return GestureDetector(
onTap: _focus.requestFocus,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
decoration: BoxDecoration(color: _raised, borderRadius: BorderRadius.circular(12)),
child: Stack(children: [
ValueListenableBuilder<TextEditingValue>(
valueListenable: _text,
builder: (context, v, _) => v.text.isEmpty ? Text('XXXX-XXXX', style: style.copyWith(color: _sub)) : const SizedBox.shrink(),
),
EditableText(controller: _text, focusNode: _focus, style: style, cursorColor: _accent, backgroundCursorColor: _sub),
]),
),
);
}
}
class Settings extends StatefulWidget {
const Settings({super.key});
@override
State<Settings> createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
bool _signedIn = false;
Widget _row(String text, VoidCallback onTap, {Key? key}) => Semantics(
button: true,
child: GestureDetector(
key: key,
behavior: HitTestBehavior.opaque,
onTap: onTap,
child: Container(
constraints: const BoxConstraints(minHeight: 48),
alignment: AlignmentDirectional.centerStart,
child: Text(text, style: const TextStyle(fontSize: 17, color: _ink)),
),
),
);
@override
Widget build(BuildContext context) {
return SampleScreen(title: 'Settings', children: [
_row('Help', () => ShotikSupport.open(), key: const ValueKey('help')),
_row(_signedIn ? 'Sign out' : 'Sign in as demo user', () {
if (_signedIn) {
ShotikSupport.reset();
ShotikAnalytics.reset();
} else {
ShotikSupport.identify(userId: 'demo-user', name: 'Demo', email: 'demo@example.com');
ShotikAnalytics.identify('demo-user', {'plan': 'pro'});
}
setState(() => _signedIn = !_signedIn);
}, key: const ValueKey('sign-in')),
_row('Send a test event', () => ShotikAnalytics.track('test_event', {'source': 'settings'})),
const SizedBox(height: 16),
const Text('Promo code (masked)', style: TextStyle(fontSize: 13, color: _sub)),
const SizedBox(height: 6),
const ShotikMask(child: PromoField()),
const Spacer(),
Text('configure: $configureMicros us', style: const TextStyle(fontSize: 12, color: _sub)),
]);
}
}