Skip to content

Commit

Permalink
Feat: Implement Json encoded ShoppingList copying to clipboard in Exp…
Browse files Browse the repository at this point in the history
…ortShoppingList
  • Loading branch information
MuKhAlm committed Feb 24, 2023
1 parent 6486552 commit 953c24c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/models/shopping_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ class ShoppingItem {
this.quantity = 1,
this.id = 0,
});

Map toJson() => {
'name': name,
'checked': checked,
'quantity': quantity,
};
}
5 changes: 5 additions & 0 deletions lib/models/shopping_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ class ShoppingList {
required this.name,
this.id = 0,
});

Map toJson() => {
'name': name,
'shoppingItems': shoppingItems.map((si) => si.toJson()).toList(),
};
}
23 changes: 16 additions & 7 deletions lib/widgets/export_shopping_list.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shopping_list_but_free/models/shopping_list.dart';

/// Displays a card that lets the user copy the Json encoding of
/// [shoppingList]
class ExportShoppingList extends StatefulWidget {
class ExportShoppingList extends StatelessWidget {
final ShoppingList shoppingList;
const ExportShoppingList({
required this.shoppingList,
Key? key,
}) : super(key: key);

@override
State<ExportShoppingList> createState() => _ExportShoppingListState();
}

class _ExportShoppingListState extends State<ExportShoppingList> {
@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -54,7 +51,19 @@ class _ExportShoppingListState extends State<ExportShoppingList> {
Expanded(
child: Center(
child: ElevatedButton.icon(
onPressed: () {},
onPressed: () async {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'Copied shopping list code to clipboard'),
),
);

await Clipboard.setData(
ClipboardData(
text: json.encode(shoppingList.toJson())),
);
},
icon: const Icon(Icons.copy),
label: const Text('Shopping list code'),
),
Expand Down

0 comments on commit 953c24c

Please sign in to comment.