// 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 . 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( 'users_crud', UserFirestore.parser(), ); final _userCubit = CrudCubit(_userRepository); return RepositoryProvider>( create: (context) => _userRepository, child: BlocProvider>( 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( 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>().get( (user?.id)!, ); }, onLongPress: () { context.read>().delete( (user?.id)!, ); }, ); }, ); }, ), const SizedBox(height: 20), BlocBuilder, CrudState>( 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>().create( UserFirestore( name: 'Wyatt $r', email: '$r@wyattapp.io', phone: '06$r'), ); }, child: const Text("Create"), ), ElevatedButton( onPressed: () { context.read>().deleteAll(); }, child: const Text("DeleteAll"), ), ElevatedButton( onPressed: () { context.read>().getAll(); }, child: const Text("GetAll"), ), ElevatedButton( onPressed: () { context .read>() .query([LimitQueryFirestore(1)]); }, child: const Text("Query"), ), ElevatedButton( onPressed: () { context.read>().streamOf(); }, child: const Text("StreamOf"), ), ElevatedButton( onPressed: () { context .read>() .updateAll({'updated': DateTime.now()}); }, child: const Text("UpdateAll"), ), ElevatedButton( onPressed: () { context.read>().reset(); }, child: const Text("Reset"), ), const SizedBox(height: 20), ], ), ), ); } }