forked from RivaanRanawat/flutter-google-docs-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome_screen.dart
106 lines (97 loc) · 3.29 KB
/
home_screen.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import 'package:docs_clone_flutter/colors.dart';
import 'package:docs_clone_flutter/common/widgets/loader.dart';
import 'package:docs_clone_flutter/models/document_model.dart';
import 'package:docs_clone_flutter/models/error_model.dart';
import 'package:docs_clone_flutter/repository/auth_repository.dart';
import 'package:docs_clone_flutter/repository/document_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:routemaster/routemaster.dart';
class HomeScreen extends ConsumerWidget {
const HomeScreen({Key? key}) : super(key: key);
void signOut(WidgetRef ref) {
ref.read(authRepositoryProvider).signOut();
ref.read(userProvider.notifier).update((state) => null);
}
void createDocument(BuildContext context, WidgetRef ref) async {
String token = ref.read(userProvider)!.token;
final navigator = Routemaster.of(context);
final snackbar = ScaffoldMessenger.of(context);
final errorModel = await ref.read(documentRepositoryProvider).createDocument(token);
if (errorModel.data != null) {
navigator.push('/document/${errorModel.data.id}');
} else {
snackbar.showSnackBar(
SnackBar(
content: Text(errorModel.error!),
),
);
}
}
void navigateToDocument(BuildContext context, String documentId) {
Routemaster.of(context).push('/document/$documentId');
}
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
appBar: AppBar(
backgroundColor: kWhiteColor,
elevation: 0,
actions: [
IconButton(
onPressed: () => createDocument(context, ref),
icon: const Icon(
Icons.add,
color: kBlackColor,
),
),
IconButton(
onPressed: () => signOut(ref),
icon: const Icon(
Icons.logout,
color: kRedColor,
),
),
],
),
body: FutureBuilder(
future: ref.watch(documentRepositoryProvider).getDocuments(
ref.watch(userProvider)!.token,
),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Loader();
}
return Center(
child: Container(
width: 600,
margin: const EdgeInsets.only(top: 10),
child: ListView.builder(
itemCount: snapshot.data!.data.length,
itemBuilder: (context, index) {
DocumentModel document = snapshot.data!.data[index];
return InkWell(
onTap: () => navigateToDocument(context, document.id),
child: SizedBox(
height: 50,
child: Card(
child: Center(
child: Text(
document.title,
style: const TextStyle(
fontSize: 17,
),
),
),
),
),
);
},
),
),
);
},
),
);
}
}