// Copyright (C) 2023 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/user_cubit.dart'; import 'package:crud_bloc_example/user_entity.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:wyatt_crud_bloc/wyatt_crud_bloc.dart'; class BasicCubitView extends StatelessWidget { const BasicCubitView({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Basic Cubit"), ), body: BlocProvider( create: (context) => UserCubit(context.read>())..read(), child: Builder(builder: (context) { return Column( children: [ BlocBuilder( buildWhen: (previous, current) { if (current is CrudLoading && current is! CrudReading) { return false; } return true; }, builder: (context, state) { return Expanded( child: CrudBuilder.typed>( state: state, builder: ((context, state) { return ListView.builder( itemCount: state.data.length, itemBuilder: (context, index) { final user = state.data.elementAt(index); return ListTile( title: Text(user?.name ?? 'Error'), subtitle: Text(user?.email ?? 'Error'), onTap: () { context .read() .delete(id: (user?.id)!); }, onLongPress: () { context.read().update( single: UpdateParameters( id: user?.id ?? '', raw: { 'email': '${user?.id}@updated.io', }), ); }, ); }, ); }), initialBuilder: (context, state) => const Center(child: CircularProgressIndicator()), loadingBuilder: (context, state) => const Center(child: CircularProgressIndicator()), errorBuilder: (context, state) => Text("Error: $state"), ), ); }, ), const SizedBox(height: 20), ElevatedButton( onPressed: () { final r = Random().nextInt(1000); context.read().create( User( id: '$r', name: 'Wyatt $r', email: '$r@wyattapp.io', phone: '06$r', ), ); }, child: BlocBuilder( buildWhen: (previous, current) { if (current is CrudLoading && current is! CrudCreating) { return false; } return true; }, builder: (context, state) { return state is CrudCreating ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator( color: Colors.white, ), ) : const Text("Create"); }, ), ), ElevatedButton( onPressed: () { context.read().read(); }, child: BlocBuilder( buildWhen: (previous, current) { if (current is CrudLoading && current is! CrudReading) { return false; } return true; }, builder: (context, state) { return state is CrudReading ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator( color: Colors.white, ), ) : const Text("Read"); }, ), ), const SizedBox(height: 20), ], ); }), ), ); } }