docs(crud): add example app
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:crud_bloc_example/models.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_crud_bloc/wyatt_crud_bloc.dart';
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({Key? key}) : super(key: key);
|
||||
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final _userRepository = CrudRepositoryFirestore<UserFirestore>(
|
||||
'users_crud',
|
||||
UserFirestore.parser(),
|
||||
);
|
||||
|
||||
final _userCubit = CrudCubit<UserFirestore>(_userRepository);
|
||||
|
||||
return RepositoryProvider<CrudRepositoryFirestore<UserFirestore>>(
|
||||
create: (context) => _userRepository,
|
||||
child: BlocProvider<CrudCubit<UserFirestore>>(
|
||||
create: (context) => _userCubit,
|
||||
child: MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: const MyHomePage(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatelessWidget {
|
||||
const MyHomePage({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Flutter Demo Home Page'),
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
const SizedBox(height: 20),
|
||||
const Text("Data:"),
|
||||
CrudStreamBuilder<UserFirestore>(
|
||||
onError: (context, state) => const SizedBox.shrink(),
|
||||
onLoading: (context, state) => const Text("Loading..."),
|
||||
onStream: (context, data) {
|
||||
return ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: data.length,
|
||||
itemBuilder: (context, index) {
|
||||
final user = data.elementAt(index);
|
||||
return ListTile(
|
||||
title: Text(user?.name ?? 'Error'),
|
||||
subtitle: Text(user?.id ?? 'Error'),
|
||||
onTap: () {
|
||||
context.read<CrudCubit<UserFirestore>>().get(
|
||||
(user?.id)!,
|
||||
);
|
||||
},
|
||||
onLongPress: () {
|
||||
context.read<CrudCubit<UserFirestore>>().delete(
|
||||
(user?.id)!,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
BlocBuilder<CrudCubit<UserFirestore>, CrudState<UserFirestore>>(
|
||||
builder: (context, state) {
|
||||
return Center(
|
||||
child: Text(
|
||||
state.toString(),
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(
|
||||
fontSize: 20,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
final r = Random().nextInt(1000);
|
||||
context.read<CrudCubit<UserFirestore>>().create(
|
||||
UserFirestore(
|
||||
name: 'Wyatt $r',
|
||||
email: '$r@wyattapp.io',
|
||||
phone: '06$r'),
|
||||
);
|
||||
},
|
||||
child: const Text("Create"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<CrudCubit<UserFirestore>>().deleteAll();
|
||||
},
|
||||
child: const Text("DeleteAll"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<CrudCubit<UserFirestore>>().getAll();
|
||||
},
|
||||
child: const Text("GetAll"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context
|
||||
.read<CrudCubit<UserFirestore>>()
|
||||
.query([LimitQueryFirestore(1)]);
|
||||
},
|
||||
child: const Text("Query"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<CrudCubit<UserFirestore>>().streamOf();
|
||||
},
|
||||
child: const Text("StreamOf"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context
|
||||
.read<CrudCubit<UserFirestore>>()
|
||||
.updateAll({'updated': DateTime.now()});
|
||||
},
|
||||
child: const Text("UpdateAll"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<CrudCubit<UserFirestore>>().reset();
|
||||
},
|
||||
child: const Text("Reset"),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:crud_bloc_example/app.dart';
|
||||
import 'package:crud_bloc_example/firebase_options.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
|
||||
runApp(const MyApp());
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:wyatt_crud_bloc/wyatt_crud_bloc.dart';
|
||||
|
||||
class UserFirestore extends Model<DocumentSnapshot, UserFirestore> {
|
||||
@override
|
||||
String? id;
|
||||
|
||||
String? name;
|
||||
String? email;
|
||||
String? phone;
|
||||
|
||||
UserFirestore({
|
||||
required this.name,
|
||||
required this.email,
|
||||
required this.phone,
|
||||
this.id,
|
||||
});
|
||||
UserFirestore._();
|
||||
|
||||
factory UserFirestore.parser() {
|
||||
return UserFirestore._();
|
||||
}
|
||||
|
||||
@override
|
||||
UserFirestore? from(DocumentSnapshot? object) {
|
||||
if (object == null) return null;
|
||||
if (object.exists) {
|
||||
return UserFirestore(
|
||||
id: object.id,
|
||||
name: (object.data() as Map<String, dynamic>?)!['name'] as String,
|
||||
email: (object.data() as Map<String, dynamic>?)!['email'] as String,
|
||||
phone: (object.data() as Map<String, dynamic>?)!['phone'] as String,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Map<String, Object> toMap() {
|
||||
return {
|
||||
'name': name ?? '',
|
||||
'email': email ?? '',
|
||||
'phone': phone ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
'UserFirestore(id: $id, name: $name, email: $email, phone: $phone)';
|
||||
}
|
||||
Reference in New Issue
Block a user