library-app.dart
library-app.dart
dart
import 'package:supabase_flutter/supabase_flutter.dart';
class Book {
final String id;
final String title;
final String author;
final DateTime publishedDate;
bool isAvailable;
Book({
required this.id,
required this.title,
required this.author,
required this.publishedDate,
required this.isAvailable,
});
// book_service.dart
class BookService {
final SupabaseClient supabase;
BookService(this.supabase);
return Book.fromJson(response);
}
// Menghapus buku
Future<void> deleteBook(String id) async {
await supabase
.from('books')
.delete()
.eq('id', id);
}
// books_screen.dart
class BooksScreen extends StatefulWidget {
@override
_BooksScreenState createState() => _BooksScreenState();
}
@override
void initState() {
super.initState();
_booksStream = _bookService.getBooksStream();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Library Management'),
),
body: StreamBuilder<List<Book>>(
stream: _booksStream,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
}
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
return ListView.builder(
itemCount: books.length,
itemBuilder: (context, index) {
final book = books[index];
return ListTile(
title: Text(book.title),
subtitle: Text(book.author),
trailing: Switch(
value: book.isAvailable,
onChanged: (bool value) {
_bookService.updateBookAvailability(book.id, value);
},
),
onLongPress: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Delete Book'),
content: Text('Are you sure you want to delete this book?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('Cancel'),
),
TextButton(
onPressed: () {
_bookService.deleteBook(book.id);
Navigator.pop(context);
},
child: Text('Delete'),
),
],
),
);
},
);
},
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// Implement add book dialog/screen
},
child: Icon(Icons.add),
),
);
}
}