forked from ambreshgupta12/InstaDart-Flutter-Instagram-Clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
88 lines (79 loc) · 2.61 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:instagram/models/models.dart';
import 'package:instagram/screens/screens.dart';
import 'package:instagram/utilities/themes.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences.getInstance().then((prefs) {
var darkModeOn = prefs.getBool('darkMode') ?? false;
//Set Navigation bar color
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
systemNavigationBarColor: darkModeOn ? Colors.black : Colors.white,
systemNavigationBarIconBrightness:
darkModeOn ? Brightness.light : Brightness.dark));
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<UserData>(create: (context) => UserData()),
ChangeNotifierProvider<ThemeNotifier>(
create: (context) =>
ThemeNotifier(darkModeOn ? darkTheme : lightTheme))
],
child: MyApp(),
),
);
});
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _isTimerDone = false;
@override
void initState() {
Timer(Duration(seconds: 3), () => setState(() => _isTimerDone = true));
super.initState();
}
Widget _getScreenId() {
return StreamBuilder<FirebaseUser>(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting ||
!_isTimerDone) {
return SplashScreen();
}
if (snapshot.hasData && _isTimerDone) {
Provider.of<UserData>(context, listen: false).currentUserId =
snapshot.data.uid;
return HomeScreen(
currentUserId: snapshot.data.uid,
);
} else {
return LoginScreen();
}
},
);
}
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
final themeNotifier = Provider.of<ThemeNotifier>(context);
return MaterialApp(
title: 'InstaDart',
debugShowCheckedModeBanner: false,
theme: themeNotifier.getTheme(),
home: _getScreenId(),
routes: {
LoginScreen.id: (context) => LoginScreen(),
SignupScreen.id: (context) => SignupScreen(),
FeedScreen.id: (context) => FeedScreen(),
},
);
}
}