app_history 0.0.3
app_history: ^0.0.3 copied to clipboard
A reusable Flutter package for managing history and likes with generic models.
import 'package:app_history/defaultkey.dart';
import 'package:flutter/material.dart';
import 'package:app_history/app_history.dart';
import 'package:uuid/uuid.dart';
// -------------------- Note Model --------------------
class NoteModel implements HasId {
final String id;
final String title;
NoteModel({required this.id, required this.title});
@override
String getId() => id;
Map<String, dynamic> toJson() => {'id': id, 'title': title};
factory NoteModel.fromJson(Map<String, dynamic> json) =>
NoteModel(id: json['id'], title: json['title']);
@override
String toString() => 'Note(id: $id, title: $title)';
}
// -------------------- Main App --------------------
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Create AppHistory for NoteModel
final appHistory = AppHistory<NoteModel>(
fromJson: (json) => NoteModel.fromJson(json),
);
// Sample Note
final note1 = NoteModel(id: const Uuid().v4(), title: "Test Note 1");
final note2 = NoteModel(id: const Uuid().v4(), title: "Test Note 2");
// Add to history
await appHistory.addHistory(note1);
await appHistory.addHistory(note2);
// Toggle favourites
await appHistory.toggleFavourite(note1);
// Get all history
final history = await appHistory.getHistory();
// Get all favourites
final favourites = await appHistory.getFavourites();
print("History:");
for (var h in history) {
print(" - ${h.title}");
}
print("Favourites:");
for (var f in favourites) {
print(" - ${f.title}");
}
runApp(const MyApp());
}
// -------------------- Flutter UI --------------------
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AppHistory Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const NotesDemoPage(),
);
}
}
class NotesDemoPage extends StatefulWidget {
const NotesDemoPage({super.key});
@override
State<NotesDemoPage> createState() => _NotesDemoPageState();
}
class _NotesDemoPageState extends State<NotesDemoPage> {
late AppHistory<NoteModel> appHistory;
List<NoteModel> history = [];
List<NoteModel> favourites = [];
@override
void initState() {
super.initState();
appHistory = AppHistory<NoteModel>(fromJson: (json) => NoteModel.fromJson(json));
_loadData();
}
Future<void> _loadData() async {
final h = await appHistory.getHistory();
final f = await appHistory.getFavourites();
setState(() {
history = h;
favourites = f;
});
}
Future<void> _addNote() async {
final newNote = NoteModel(id: const Uuid().v4(), title: "Note ${history.length + 1}");
await appHistory.addHistory(newNote);
await _loadData();
}
Future<void> _toggleFav(NoteModel note) async {
await appHistory.toggleFavourite(note);
await _loadData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('AppHistory Demo')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
ElevatedButton(onPressed: _addNote, child: const Text('Add Note')),
const SizedBox(height: 16),
const Text("History:", style: TextStyle(fontWeight: FontWeight.bold)),
Expanded(
child: ListView.builder(
itemCount: history.length,
itemBuilder: (_, index) {
final note = history[index];
final isFav = favourites.any((f) => f.id == note.id);
return ListTile(
title: Text(note.title),
trailing: IconButton(
icon: Icon(isFav ? Icons.star : Icons.star_border,
color: isFav ? Colors.amber : null),
onPressed: () => _toggleFav(note),
),
);
},
),
),
],
),
),
);
}
}