Skip to content

Instantly share code, notes, and snippets.

@timfong888
Created July 22, 2022 02:49
Show Gist options
  • Save timfong888/28ed4ed8eb04c4525e52dd24acdd5c95 to your computer and use it in GitHub Desktop.
Save timfong888/28ed4ed8eb04c4525e52dd24acdd5c95 to your computer and use it in GitHub Desktop.
main() listener with goRouter
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'auth/firebase_user_provider.dart';
import 'auth/auth_util.dart';
import 'flutter_flow/flutter_flow_theme.dart';
import 'flutter_flow/flutter_flow_util.dart';
import 'flutter_flow/internationalization.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'flutter_flow/nav/nav.dart';
import 'index.dart';
import 'package:getsocial_flutter_sdk/getsocial_flutter_sdk.dart';
import 'landing_page/landing_page_widget.dart';
import 'app_state.dart';
import 'package:get/get.dart';
import 'package:go_router/go_router.dart';
import 'locator.service.dart';
ReferralData globalReferralData;
List<BuildContext> buildContextList = [];
GlobalKey<ScaffoldMessengerState> scaffoldKey = GlobalKey();
StreamController<String> sController = StreamController.broadcast();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FFAppState(); // Initialize FFAppState
GetSocial.addOnInitializedListener(() => {
// GetSocial SDK is ready to use
});
setupLocator();
runApp(MyApp());
locator<LandingPageData>().referralID = "defaultReferralID";
}
void registerListeners() {
Invites.setOnReferralDataReceivedListener((received) {
globalReferralData = received;
print(globalReferralData);
print(globalReferralData.linkParams);
print("listener - socialdata");
String passedReferralID =
globalReferralData.linkParams['referralID'].toString();
String passedCreatorID =
globalReferralData.linkParams['creatorID'].toString();
String passedCampaignID =
globalReferralData.linkParams['\$campaign_id'].toString();
print(passedReferralID);
print(passedCreatorID);
print(passedCampaignID);
sController.sink.add("");
locator<LandingPageData>().referralID = passedReferralID;
locator<LandingPageData>().creatorID = passedCreatorID;
locator<LandingPageData>().campaignID = passedCampaignID;
});
}
class MyApp extends StatefulWidget {
// This widget is the root of your application.
@override
State<MyApp> createState() => _MyAppState();
static _MyAppState of(BuildContext context) =>
context.findAncestorStateOfType<_MyAppState>();
}
class _MyAppState extends State<MyApp> {
Locale _locale;
ThemeMode _themeMode = ThemeMode.system;
Stream<AssemblyFirebaseUser> userStream;
AppStateNotifier _appStateNotifier;
GoRouter _router;
final authUserSub = authenticatedUserStream.listen((_) {});
@override
void initState() {
registerListeners();
super.initState();
_appStateNotifier = AppStateNotifier();
_router = createRouter(_appStateNotifier);
userStream = assemblyFirebaseUserStream()
..listen((user) => _appStateNotifier.update(user));
Future.delayed(
Duration(seconds: 1),
() => _appStateNotifier.stopShowingSplashImage(),
);
}
@override
void dispose() {
authUserSub.cancel();
super.dispose();
}
void setLocale(Locale value) => setState(() => _locale = value);
void setThemeMode(ThemeMode mode) => setState(() {
_themeMode = mode;
});
@override
Widget build(BuildContext context) {
SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
sController.stream.listen((event) {
context.go('landingPage');
});
});
return MaterialApp.router(
// scaffoldMessengerKey: locator<NavigationService>().navigatorKey,
title: 'Assembly',
localizationsDelegates: [
FFLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
locale: _locale,
supportedLocales: const [
Locale('en', ''),
],
theme: ThemeData(brightness: Brightness.light),
themeMode: _themeMode,
routeInformationParser: _router.routeInformationParser,
routerDelegate: _router.routerDelegate,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment