169 lines
5.6 KiB
Dart
169 lines
5.6 KiB
Dart
// 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),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|