Skip to content

Instantly share code, notes, and snippets.

@XavierChanth
Created March 5, 2022 23:58
Show Gist options
  • Save XavierChanth/e850f6a87ca3ce37039b9e106ed4c2f9 to your computer and use it in GitHub Desktop.
Save XavierChanth/e850f6a87ca3ce37039b9e106ed4c2f9 to your computer and use it in GitHub Desktop.
Instant Onboarding Demo
import 'package:at_client_mobile/at_client_mobile.dart';
import 'package:flutter/material.dart';
// * Once the onboarding process is completed you will be taken to this screen
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// * Getting the AtClientManager instance to use below
AtClientManager atClientManager = AtClientManager.getInstance();
return Scaffold(
appBar: AppBar(
title: const Text('What\'s my current @sign?'),
),
body: Center(
child: Column(children: [
const Text('Successfully onboarded and navigated to FirstAppScreen'),
// * Use the AtClientManager instance to get the AtClient
// * Then use the AtClient to get the current @sign
Text('Current @sign: ${atClientManager.atClient.getCurrentAtSign()}')
]),
),
);
}
}
import 'dart:async';
import 'package:at_app_flutter/at_app_flutter.dart' show AtEnv;
import 'package:at_client_mobile/at_client_mobile.dart';
import 'package:at_onboarding_flutter/at_onboarding_flutter.dart' show Onboarding;
import 'package:at_utils/at_logger.dart' show AtSignLogger;
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart' show getApplicationSupportDirectory;
import 'home_screen.dart';
final AtSignLogger _logger = AtSignLogger(AtEnv.appNamespace);
Future<void> main() async {
// * AtEnv is an abtraction of the flutter_dotenv package used to
// * load the environment variables set by at_app
try {
await AtEnv.load();
WidgetsFlutterBinding.ensureInitialized();
} catch (e) {
_logger.finer('Environment failed to load from .env: ', e);
}
runApp(MaterialApp(home: const MyApp()));
}
Future<AtClientPreference> loadAtClientPreference() async {
var dir = await getApplicationSupportDirectory();
return AtClientPreference()
..rootDomain = AtEnv.rootDomain
..namespace = AtEnv.appNamespace
..hiveStoragePath = dir.path
..commitLogPath = dir.path
..isLocalStoreRequired = true;
// TODO
// * By default, this configuration is suitable for most applications
// * In advanced cases you may need to modify [AtClientPreference]
// * Read more here: https://pub.dev/documentation/at_client/latest/at_client/AtClientPreference-class.html
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// * load the AtClientPreference in the background
Future<AtClientPreference> futurePreference = loadAtClientPreference();
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addPostFrameCallback(
(_) => _onboard(context),
);
}
void _onboard(BuildContext context) async {
if (mounted) {
Onboarding(
context: context,
atClientPreference: await futurePreference,
domain: AtEnv.rootDomain,
rootEnvironment: AtEnv.rootEnvironment,
appAPIKey: AtEnv.appApiKey,
onboard: (value, atsign) {
_logger.finer('Successfully onboarded $atsign');
},
onError: (error) {
_logger.severe('Onboarding throws $error error');
},
nextScreen: const HomeScreen(),
);
}
}
@override
Widget build(BuildContext context) {
// * The onboarding screen (first screen)
return Scaffold(
appBar: AppBar(
title: const Text('MyApp'),
),
body: Center(
child: ElevatedButton(
child: Text("Onboard"),
onPressed: () => _onboard(context),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment