Skip to content

Commit 57f8da8

Browse files
authored
Apply provider & restructure (duytq94#55)
1 parent 1928c76 commit 57f8da8

25 files changed

+828
-499
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@
1717
## How to run
1818
* Clone this repo
1919
* Run `flutter packages get`
20-
* Run `flutter run` (remember open simulator or connect physical device, iOS auto run additional command `pod install`)
20+
* Run `flutter run` (remember open simulator or connect physical device, iOS auto run additional command `pod install`)
21+
22+
## Big update
23+
* Jun, 4th, 2021 — Migrating to Flutter 2 and using Dart sound null safety, so maybe some template code in this article is outdated, please check source code for the latest.
24+
* Oct, 2th, 2021 - Apply provider & restructure

images/app_icon.png

8.34 KB
Loading

lib/constants/app_constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ class AppConstants {
55
static const chatTitle = "Chat";
66
static const settingsTitle = "Settings";
77
static const fullPhotoTitle = "Full Photo";
8-
}
8+
}

lib/constants/constants.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export 'color_constants.dart';
2+
export 'firestore_constants.dart';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class FirestoreConstants {
2+
static const pathUserCollection = "users";
3+
static const pathMessageCollection = "messages";
4+
static const nickname = "nickname";
5+
static const aboutMe = "aboutMe";
6+
static const photoUrl = "photoUrl";
7+
static const id = "id";
8+
static const chattingWith = "chattingWith";
9+
static const idFrom = "idFrom";
10+
static const idTo = "idTo";
11+
static const timestamp = "timestamp";
12+
static const content = "content";
13+
static const type = "type";
14+
}

lib/main.dart

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,72 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:firebase_auth/firebase_auth.dart';
13
import 'package:firebase_core/firebase_core.dart';
4+
import 'package:firebase_storage/firebase_storage.dart';
25
import 'package:flutter/material.dart';
36
import 'package:flutter_chat_demo/constants/app_constants.dart';
7+
import 'package:flutter_chat_demo/providers/auth_provider.dart';
8+
import 'package:google_sign_in/google_sign_in.dart';
9+
import 'package:provider/provider.dart';
10+
import 'package:shared_preferences/shared_preferences.dart';
411

512
import 'constants/color_constants.dart';
613
import 'pages/pages.dart';
14+
import 'providers/providers.dart';
715

816
void main() async {
917
WidgetsFlutterBinding.ensureInitialized();
1018
await Firebase.initializeApp();
11-
runApp(MyApp());
19+
SharedPreferences prefs = await SharedPreferences.getInstance();
20+
runApp(MyApp(prefs: prefs));
1221
}
1322

1423
class MyApp extends StatelessWidget {
24+
final SharedPreferences prefs;
25+
final FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
26+
final FirebaseStorage firebaseStorage = FirebaseStorage.instance;
27+
28+
MyApp({required this.prefs});
29+
1530
@override
1631
Widget build(BuildContext context) {
17-
return MaterialApp(
18-
title: AppConstants.appTitle,
19-
theme: ThemeData(
20-
primaryColor: ColorConstants.themeColor,
32+
return MultiProvider(
33+
providers: [
34+
ChangeNotifierProvider<AuthProvider>(
35+
create: (_) => AuthProvider(
36+
firebaseAuth: FirebaseAuth.instance,
37+
googleSignIn: GoogleSignIn(),
38+
prefs: this.prefs,
39+
firebaseFirestore: this.firebaseFirestore,
40+
),
41+
),
42+
Provider<SettingProvider>(
43+
create: (_) => SettingProvider(
44+
prefs: this.prefs,
45+
firebaseFirestore: this.firebaseFirestore,
46+
firebaseStorage: this.firebaseStorage,
47+
),
48+
),
49+
Provider<HomeProvider>(
50+
create: (_) => HomeProvider(
51+
firebaseFirestore: this.firebaseFirestore,
52+
),
53+
),
54+
Provider<ChatProvider>(
55+
create: (_) => ChatProvider(
56+
prefs: this.prefs,
57+
firebaseFirestore: this.firebaseFirestore,
58+
firebaseStorage: this.firebaseStorage,
59+
),
60+
),
61+
],
62+
child: MaterialApp(
63+
title: AppConstants.appTitle,
64+
theme: ThemeData(
65+
primaryColor: ColorConstants.themeColor,
66+
),
67+
home: SplashPage(),
68+
debugShowCheckedModeBanner: false,
2169
),
22-
home: LoginPage(),
23-
debugShowCheckedModeBanner: false,
2470
);
2571
}
2672
}

lib/models/message_chat.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:flutter_chat_demo/constants/constants.dart';
3+
4+
class MessageChat {
5+
String idFrom;
6+
String idTo;
7+
String timestamp;
8+
String content;
9+
int type;
10+
11+
MessageChat({
12+
required this.idFrom,
13+
required this.idTo,
14+
required this.timestamp,
15+
required this.content,
16+
required this.type,
17+
});
18+
19+
Map<String, dynamic> toJson() {
20+
return {
21+
FirestoreConstants.idFrom: this.idFrom,
22+
FirestoreConstants.idTo: this.idTo,
23+
FirestoreConstants.timestamp: this.timestamp,
24+
FirestoreConstants.content: this.content,
25+
FirestoreConstants.type: this.type,
26+
};
27+
}
28+
29+
factory MessageChat.fromDocument(DocumentSnapshot doc) {
30+
String idFrom = doc.get(FirestoreConstants.idFrom);
31+
String idTo = doc.get(FirestoreConstants.idTo);
32+
String timestamp = doc.get(FirestoreConstants.timestamp);
33+
String content = doc.get(FirestoreConstants.content);
34+
int type = doc.get(FirestoreConstants.type);
35+
return MessageChat(idFrom: idFrom, idTo: idTo, timestamp: timestamp, content: content, type: type);
36+
}
37+
}

lib/models/models.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export 'message_chat.dart';
12
export 'user_chat.dart';

lib/models/user_chat.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:flutter_chat_demo/constants/constants.dart';
23

34
class UserChat {
45
String id;
@@ -8,18 +9,26 @@ class UserChat {
89

910
UserChat({required this.id, required this.photoUrl, required this.nickname, required this.aboutMe});
1011

12+
Map<String, String> toJson() {
13+
return {
14+
FirestoreConstants.nickname: nickname,
15+
FirestoreConstants.aboutMe: aboutMe,
16+
FirestoreConstants.photoUrl: photoUrl,
17+
};
18+
}
19+
1120
factory UserChat.fromDocument(DocumentSnapshot doc) {
1221
String aboutMe = "";
1322
String photoUrl = "";
1423
String nickname = "";
1524
try {
16-
aboutMe = doc.get('aboutMe');
25+
aboutMe = doc.get(FirestoreConstants.aboutMe);
1726
} catch (e) {}
1827
try {
19-
photoUrl = doc.get('photoUrl');
28+
photoUrl = doc.get(FirestoreConstants.photoUrl);
2029
} catch (e) {}
2130
try {
22-
nickname = doc.get('nickname');
31+
nickname = doc.get(FirestoreConstants.nickname);
2332
} catch (e) {}
2433
return UserChat(
2534
id: doc.id,

0 commit comments

Comments
 (0)