createFirebaseMainWithOnBoarding static method
Future<void>
createFirebaseMainWithOnBoarding(
- String projectName
)
Implementation
static Future<void> createFirebaseMainWithOnBoarding(
String projectName) async {
// change the content of main.dart
final File mainFile = File('lib/main.dart');
mainFile.writeAsStringSync('''
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:$projectName/firebase_options.dart';
import 'package:$projectName/features/auth/ui/auth_screen.dart';
import 'package:$projectName/features/onBoarding/ui/on_boarding_screen.dart';
import 'package:$projectName/shared/nav_bar.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
SharedPreferences prefs = await SharedPreferences.getInstance();
bool hasSeenOnboarding = prefs.getBool('hasSeenOnboarding') ?? false;
runApp(ProviderScope(child: MyApp(hasSeenOnboarding: hasSeenOnboarding)));
}
class MyApp extends StatelessWidget {
final bool hasSeenOnboarding;
const MyApp({required this.hasSeenOnboarding, super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: !hasSeenOnboarding ? const OnBoardingScreen(): StreamBuilder<User?>(
stream: FirebaseAuth.instance.authStateChanges(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Text(snapshot.error.toString());
}
if (snapshot.connectionState == ConnectionState.active) {
if (snapshot.data == null) {
return const AuthScreen();
} else {
return const NavigationScreen();
}
}
return const Center(
child: CircularProgressIndicator(),
);
},
),
);
}
}
''');
}