added logic to save collections and display them
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import '../model/collection.dart';
|
||||||
|
import '../service/storage.dart';
|
||||||
import '../widgets/create_bookmark_collection_dialog.dart';
|
import '../widgets/create_bookmark_collection_dialog.dart';
|
||||||
|
|
||||||
class CollectionsPage extends StatefulWidget {
|
class CollectionsPage extends StatefulWidget {
|
||||||
@@ -11,23 +13,36 @@ class CollectionsPage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _CollectionsPageState extends State<CollectionsPage> {
|
class _CollectionsPageState extends State<CollectionsPage> {
|
||||||
|
final collections = Storage.loadCollections();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(),
|
appBar: AppBar(),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: () => showDialog(
|
onPressed: onAddButtonPressed,
|
||||||
context: context,
|
|
||||||
builder: (context) => CreateBookmarkCollectionDialog(),
|
|
||||||
),
|
|
||||||
child: Icon(Icons.add),
|
child: Icon(Icons.add),
|
||||||
),
|
),
|
||||||
body: ListView(),
|
body: ListView.builder(
|
||||||
|
itemBuilder: itemBuilder,
|
||||||
|
itemCount: collections.length,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onAddButtonPressed() => showDialog(
|
void onAddButtonPressed() => showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (context) => CreateBookmarkCollectionDialog(),
|
builder: (context) =>
|
||||||
|
CreateBookmarkCollectionDialog(onSavePressed: onCollectionSaved),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void onCollectionSaved(String name) {
|
||||||
|
collections.add(Collection(name: name));
|
||||||
|
setState(() {});
|
||||||
|
Storage.saveCollections(collections);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget itemBuilder(BuildContext context, int index) {
|
||||||
|
return ListTile(title: Text(collections.elementAt(index).name));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,19 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
class CreateBookmarkCollectionDialog extends StatelessWidget {
|
class CreateBookmarkCollectionDialog extends StatelessWidget {
|
||||||
const CreateBookmarkCollectionDialog({super.key});
|
const CreateBookmarkCollectionDialog({
|
||||||
|
super.key,
|
||||||
|
required this.onSavePressed,
|
||||||
|
});
|
||||||
|
final void Function(String name) onSavePressed;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final nameController = TextEditingController();
|
||||||
return AlertDialog(
|
return AlertDialog(
|
||||||
title: Text('Create Collection'),
|
title: Text('Create Collection'),
|
||||||
content: TextField(
|
content: TextField(
|
||||||
|
controller: nameController,
|
||||||
autofocus: true,
|
autofocus: true,
|
||||||
maxLines: 1,
|
maxLines: 1,
|
||||||
maxLength: 50,
|
maxLength: 50,
|
||||||
@@ -22,7 +28,13 @@ class CreateBookmarkCollectionDialog extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
FloatingActionButton(onPressed: () {}, child: Icon(Icons.save)),
|
FloatingActionButton(
|
||||||
|
onPressed: () {
|
||||||
|
onSavePressed(nameController.text);
|
||||||
|
Navigator.of(context).pop();
|
||||||
|
},
|
||||||
|
child: Icon(Icons.save),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user