day_sdk 1.0.1
day_sdk: ^1.0.1 copied to clipboard
Flutter plugin for the Day.io SDK - sign the employee in from your backend and show the Day.io attendance screen in your app.
example/lib/main.dart
import 'dart:async';
import 'package:day_sdk/day_sdk.dart';
import 'package:flutter/material.dart';
final _messengerKey = GlobalKey<ScaffoldMessengerState>();
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// Once, when the app starts - no network calls.
await DaySdk.initialize(
environment: DayEnvironment.stage,
onSessionInvalid: () => _messengerKey.currentState?.showSnackBar(
const SnackBar(content: Text('Day.io rejected the session - paste a new one-time token and start')),
),
);
runApp(const DaySdkDemoApp());
}
class DaySdkDemoApp extends StatelessWidget {
const DaySdkDemoApp({super.key});
@override
Widget build(BuildContext context) => MaterialApp(
title: 'Day.io SDK demo',
scaffoldMessengerKey: _messengerKey,
theme: ThemeData(colorSchemeSeed: Colors.indigo),
darkTheme: ThemeData(colorSchemeSeed: Colors.indigo, brightness: Brightness.dark),
home: const SignInPage(),
);
}
/// Stands in for your app's own sign-in: paste a one-time token your backend requested from the
/// Day.io External API (STAGE), then start - or resume with the session already on the device.
class SignInPage extends StatefulWidget {
const SignInPage({super.key});
@override
State<SignInPage> createState() => _SignInPageState();
}
class _SignInPageState extends State<SignInPage> {
final _token = TextEditingController();
final _log = <String>[];
late final StreamSubscription<bool> _online;
bool? _isOnline;
@override
void initState() {
super.initState();
_online = DaySdk.isOnline.listen((online) => setState(() => _isOnline = online));
}
@override
void dispose() {
_online.cancel();
_token.dispose();
super.dispose();
}
void _add(String line) => setState(() => _log.insert(0, line));
Future<void> _start() async {
final token = _token.text.trim();
// A one-time token works once - never keep it around.
_token.clear();
_add('start -> ${_describe(await DaySdk.start(token))}');
}
Future<void> _resume() async => _add('resume -> ${_describe(await DaySdk.resume())}');
Future<void> _signOut() async {
await DaySdk.signOut();
_add('signOut -> done');
}
Future<void> _lastAuthenticatedAt() async =>
_add('lastAuthenticatedAt -> ${await DaySdk.lastAuthenticatedAt ?? 'never'}');
String _describe(DaySdkState state) => switch (state) {
DaySdkReady(:final profile) => 'Ready(${profile.name}, ${profile.companyName})',
DaySdkFailed(:final message) => 'Failed($message)',
DaySdkProfileNotFound(:final profileUuid) => 'ProfileNotFound($profileUuid)',
_ => state.runtimeType.toString().replaceFirst('DaySdk', ''),
};
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Day.io SDK demo'),
actions: [
if (_isOnline != null)
Padding(
padding: const EdgeInsets.only(right: 16),
child: Icon(_isOnline! ? Icons.cloud_done : Icons.cloud_off),
),
],
),
body: StreamBuilder<DaySdkState>(
stream: DaySdk.state,
builder: (context, snapshot) {
final state = snapshot.data;
return ListView(
padding: const EdgeInsets.all(16),
children: [
Text('State: ${state == null ? '…' : _describe(state)}',
style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 16),
TextField(
controller: _token,
decoration: const InputDecoration(
labelText: 'One-time token (STAGE)',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 8),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
FilledButton(onPressed: _start, child: const Text('start')),
FilledButton.tonal(onPressed: _resume, child: const Text('resume')),
OutlinedButton(onPressed: _signOut, child: const Text('signOut')),
OutlinedButton(onPressed: _lastAuthenticatedAt, child: const Text('lastAuthenticatedAt')),
],
),
const SizedBox(height: 16),
FilledButton.icon(
icon: const Icon(Icons.schedule),
label: const Text('Open attendance'),
onPressed: state is DaySdkReady
? () => Navigator.of(context).push(MaterialPageRoute(builder: (_) => const AttendancePage()))
: null,
),
const Divider(height: 32),
for (final line in _log) Text(line, style: const TextStyle(fontFamily: 'monospace')),
],
);
},
),
);
}
/// The SDK's screen under the app's own AppBar, in the app's colors - switchable while it is up.
class AttendancePage extends StatefulWidget {
const AttendancePage({super.key});
@override
State<AttendancePage> createState() => _AttendancePageState();
}
class _AttendancePageState extends State<AttendancePage> {
bool _appColors = true;
@override
Widget build(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(
title: const Text('Attendance'),
actions: [
IconButton(
tooltip: _appColors ? "Day.io's colors" : "This app's colors",
icon: Icon(_appColors ? Icons.palette : Icons.palette_outlined),
onPressed: () => setState(() => _appColors = !_appColors),
),
],
),
body: DaySdkAttendance(
configuration: _appColors
? DaySdkConfiguration(
colors: DaySdkColors(
primary: scheme.primary,
onPrimary: scheme.onPrimary,
primaryContainer: scheme.primaryContainer,
background: scheme.surface,
surface: scheme.surfaceContainerLow,
onSurface: scheme.onSurface,
onSurfaceVariant: scheme.onSurfaceVariant,
),
)
: null,
),
);
}
}