Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,320 --> 00:00:04,520
The application is really taking shape,
2
00:00:04,630 --> 00:00:12,310
besides some styling or layout things like that keyboard thing here or maybe on different device sizes
3
00:00:12,670 --> 00:00:17,350
where we might want to change the look and feel of our app a little bit,
4
00:00:17,440 --> 00:00:21,270
besides that, the app is really looking and working good and decent
5
00:00:21,880 --> 00:00:29,110
and the missing feature I want to add here is a feature that allows us to delete transactions. For that,
6
00:00:29,110 --> 00:00:34,450
LEt's go to our ListTile here in the transaction_list.dart file and there on the ListTile,
7
00:00:34,450 --> 00:00:41,830
I did mention that on the ListTile, you can add a trailing widget as well. So if you add trailing here,
8
00:00:41,840 --> 00:00:47,470
you can add a widget there which you want to show at the end of the ListTile and the trailing widget
9
00:00:47,530 --> 00:00:53,770
I want to show here is an icon button with an icon of
10
00:00:56,880 --> 00:01:00,030
delete, which is a trashcan trash icon.
11
00:01:00,330 --> 00:01:01,700
So I'm creating that
12
00:01:01,710 --> 00:01:07,680
icon button because when we press this button, the element on which we triggered it should be removed.
13
00:01:09,360 --> 00:01:15,990
I'll give this a color, that icon and the color here will be received or taken from my theme and there,
14
00:01:15,990 --> 00:01:18,030
I'll take the error color.
15
00:01:18,030 --> 00:01:24,750
Now we haven't set the error color and of course you could do that in your theme, there in the main.dart
16
00:01:24,760 --> 00:01:30,210
file, besides setting a primary swatch and accent color, you can also set an error color but the
17
00:01:30,210 --> 00:01:35,580
default here is red already, so you don't need to set that but you could if you wanted to and especially
18
00:01:35,580 --> 00:01:37,970
if you want a different color than red of course
19
00:01:38,460 --> 00:01:44,820
and then onPressed, I of course want to trigger a method that removes that element. Now for the moment,
20
00:01:44,850 --> 00:01:48,600
let's do nothing so that we can see whether it's displayed and that's looking good,
21
00:01:48,600 --> 00:01:49,270
we have that
22
00:01:49,290 --> 00:01:51,560
icon here, we can press it,
23
00:01:51,690 --> 00:01:57,640
so let's now also make sure that when we press it, we do delete this transaction. Since we managed the
24
00:01:57,640 --> 00:02:05,380
transactions in main.dart, here in our MyHomePage state class where we also have the add new transaction
25
00:02:05,380 --> 00:02:05,950
method,
26
00:02:06,010 --> 00:02:09,670
this is also where we should have the delete transaction method.
27
00:02:09,670 --> 00:02:16,060
So let's add delete transaction here, doesn't need to return anything because the place where we call
28
00:02:16,060 --> 00:02:18,970
it doesn't really need a return value
29
00:02:18,970 --> 00:02:25,210
and now the question of course is, how can we delete a transaction? How do we identify a transaction?
30
00:02:25,210 --> 00:02:30,000
Now of course you could identify a transaction by its name, by its title
31
00:02:30,640 --> 00:02:34,090
but the problem is the title here is not necessarily unique,
32
00:02:34,090 --> 00:02:37,590
so is the amount and the date, all of that could be duplicated.
33
00:02:37,630 --> 00:02:41,470
We could have the same transaction twice on the same day with the exact same amount,
34
00:02:41,500 --> 00:02:43,750
that would be valid.
35
00:02:43,810 --> 00:02:52,690
So rather than doing it like this and trying to delete the transaction by name, we can use another trait
36
00:02:52,720 --> 00:02:58,090
of our transaction. If we have a look at the model, we see that we have an ID there and the ID should
37
00:02:58,090 --> 00:02:58,630
be unique
38
00:02:58,630 --> 00:03:05,870
and indeed, it is, at least almost, of course in our app here in the main.dart file when we create a new
39
00:03:06,500 --> 00:03:11,020
transaction, we do generate the ID based on the current timestamp
40
00:03:11,030 --> 00:03:15,480
and in theory, we could have the same transaction added twice at the exact same time
41
00:03:15,620 --> 00:03:18,800
but in reality in this app that won't really happen.
42
00:03:18,800 --> 00:03:24,500
So it's a good enough ID for now and therefore we can use this ID as an identifier.
43
00:03:24,950 --> 00:03:31,640
So when this method here gets called, delete transaction, I expect to get that ID as an argument by
44
00:03:31,640 --> 00:03:33,210
whoever is calling that,
45
00:03:33,230 --> 00:03:38,240
so whoever calls this method needs to provide the ID of the transaction that should be deleted
46
00:03:38,450 --> 00:03:43,100
and then in here, we can delete this transaction by that ID.
47
00:03:43,100 --> 00:03:49,370
For that, we will call set state because we certainly will need to rebuild our UI thereafter and inside
48
00:03:49,370 --> 00:03:56,180
of that function you pass to set state, we can reach out to our user transactions, to our list of transactions
49
00:03:56,470 --> 00:03:57,800
and there as you learned,
50
00:03:57,830 --> 00:04:02,690
since this is a list, we have multiple methods that allow you to manipulate that list and generate a
51
00:04:02,690 --> 00:04:04,310
new list
52
00:04:04,310 --> 00:04:11,630
and I want to use remove where. This manipulates the existing list, just like add manipulates the existing
53
00:04:11,630 --> 00:04:16,700
list and it removes an item where a certain condition is met.
54
00:04:16,700 --> 00:04:24,170
Therefore we parse in a function and that function gets the element, each element of the list because
55
00:04:24,170 --> 00:04:27,140
this runs this function on every element in the list
56
00:04:27,230 --> 00:04:31,420
just like where did, so we get our transaction in here
57
00:04:31,460 --> 00:04:35,000
and we have to return true if it is the element we want to remove
58
00:04:35,120 --> 00:04:40,400
and that of course is the case if the ID of the element I'm looking at is the same ID as I'm getting
59
00:04:40,400 --> 00:04:41,180
here.
60
00:04:41,180 --> 00:04:46,640
So if tx.id is equal to ID, then I know this is the element I want to get rid of
61
00:04:47,270 --> 00:04:52,940
and since we only have one expression here which value we return, you could also use the shortcut of
62
00:04:52,940 --> 00:04:59,090
using that arrow without the curly braces and without the return statement and use this inline notation,
63
00:04:59,270 --> 00:05:05,090
also without the semicolon and have this shorter syntax here, that's optional but you can use it here
64
00:05:05,270 --> 00:05:10,310
because we have only one expression and we want to return the value and use that value of that expression.
65
00:05:11,180 --> 00:05:14,480
So this allows us to remove an item with that ID,
66
00:05:14,480 --> 00:05:20,050
now we need to call delete transaction and that happens from inside the transaction list.
67
00:05:20,300 --> 00:05:23,540
So let's pass delete transaction to the transaction list
68
00:05:23,540 --> 00:05:27,130
now here as a second argument maybe
69
00:05:27,260 --> 00:05:31,370
and of course we now need to tweak the constructor of transaction list
70
00:05:31,370 --> 00:05:32,910
now to accept that.
71
00:05:32,930 --> 00:05:39,940
So here I now expect to get a function, delete tx, whatever you want to name it and I bind the second
72
00:05:39,970 --> 00:05:47,180
argument I'm getting to this delete tx property here in the transaction list widget. And now delete
73
00:05:47,180 --> 00:05:50,010
tx is a pointer at that delete function,
74
00:05:50,050 --> 00:05:53,900
so in our transaction list we can now use it here
75
00:05:54,010 --> 00:05:57,720
on that icon button to trigger that deletion.
76
00:05:57,880 --> 00:06:07,180
Now important, binding it like this won't work because delete tx actually points at our delete transaction
77
00:06:07,240 --> 00:06:09,660
method here and that requires an argument,
78
00:06:09,730 --> 00:06:16,540
the ID of the transaction that should be deleted. The onPressed method by default doesn't pass any arguments,
79
00:06:16,540 --> 00:06:22,650
so we would be missing that argument which we need. The solution is to wrap this into an anonymous function
80
00:06:22,950 --> 00:06:28,460
so that we pass a reference to this function, to this anonymous function, to onPressed
81
00:06:28,500 --> 00:06:31,350
and then when this anonymous function is executed in there,
82
00:06:31,470 --> 00:06:36,990
we have our own function which we call and there we can now pass our own argument and the argument I
83
00:06:36,990 --> 00:06:43,800
want to pass here is my transaction ID, so I can use transactions and then for the given index.id
84
00:06:43,860 --> 00:06:48,040
and forward that. With that, let's give it a try.
85
00:06:48,640 --> 00:06:49,490
Let's save it all and
86
00:06:49,970 --> 00:06:55,470
let's try deleting the new shoes here and that works and you see the chart also updates.
87
00:06:55,510 --> 00:07:00,520
So now, we have a set up where we can not only add transactions but where we can also delete them.
9668
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.