Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,530 --> 00:00:06,550
To show a sheet, there is a built-in function Flutter offers us
2
00:00:06,550 --> 00:00:09,430
that's good to show such a bottom modal sheet
3
00:00:09,430 --> 00:00:15,500
I should say and since I want to show that sheet both when we click that floating action button and
4
00:00:15,500 --> 00:00:21,620
when we click that action in the app bar, I will create a method that opens up that sheet so that we
5
00:00:21,620 --> 00:00:27,070
can share that method with both places where we want to trigger it.
6
00:00:28,700 --> 00:00:29,950
It will return nothing,
7
00:00:30,050 --> 00:00:35,260
that method and I'll name it startAddNewTransaction.
8
00:00:35,300 --> 00:00:38,360
The name might be a bit clunky but in the end that is what happens here,
9
00:00:38,360 --> 00:00:40,180
it doesn't add a new transaction,
10
00:00:40,220 --> 00:00:43,280
it starts the process of adding a new transaction,
11
00:00:43,280 --> 00:00:47,900
it starts the process by showing the input area for that new transaction data.
12
00:00:48,680 --> 00:00:55,610
So startAddNewTransaction is the method name and in there, you can call showModalBottomSheet,
13
00:00:55,710 --> 00:00:57,620
a function provided by Flutter,
14
00:00:57,620 --> 00:01:00,200
that's why this function is available here.
15
00:01:00,350 --> 00:01:06,260
So this function can be called and showModalBottomSheet
16
00:01:06,500 --> 00:01:08,790
now needs at least two arguments -
17
00:01:08,810 --> 00:01:15,440
the first is a value for the context because internally, this function will use this strange context
18
00:01:15,470 --> 00:01:20,240
object with which we haven't worked to show that modal sheet,
19
00:01:20,240 --> 00:01:22,850
so we need to pass a value for context here.
20
00:01:22,850 --> 00:01:26,760
The good thing is we get a value for context in here,
21
00:01:27,050 --> 00:01:31,400
we get it passed into the build function automatically by Flutter but of course that's the wrong place,
22
00:01:31,400 --> 00:01:32,680
we need it up here.
23
00:01:32,690 --> 00:01:34,040
Solution is simple,
24
00:01:34,040 --> 00:01:40,190
we accept it as an argument here and I'll name it ctx to avoid confusion with the names. So I forward
25
00:01:40,250 --> 00:01:45,680
my context which I expect to get as an argument to showModalBottomSheet for the context argument
26
00:01:46,280 --> 00:01:52,160
and then we also need a builder. Builder now is a function that needs to return the widget that should
27
00:01:52,160 --> 00:01:54,880
be inside of that modal bottom sheet.
28
00:01:55,280 --> 00:02:02,150
The builder function itself also gives us a context and I'll name this bCtx to really make this clear
29
00:02:02,150 --> 00:02:04,870
that this is not the same value as this context.
30
00:02:04,880 --> 00:02:09,620
This is the context we pass to this function to pass it to the modal bottom sheet,
31
00:02:09,620 --> 00:02:18,200
the modal bottom sheet then starts building that sheet that slides in and to that builder, it again gives
32
00:02:18,200 --> 00:02:25,070
us its own context, so its own package of meta information about that widget which it builds. We never
33
00:02:25,070 --> 00:02:26,510
use that context,
34
00:02:26,510 --> 00:02:31,470
well at least now we start using it here indirectly when we pass it to showModalBottomSheet,
35
00:02:31,640 --> 00:02:37,070
we get it here nonetheless in the builder in case that inside of the builder where we now built the
36
00:02:37,070 --> 00:02:43,280
widget that should be part of the modal sheet, in case we needed access to the context object there and
37
00:02:43,280 --> 00:02:48,800
you will later in this module learn why you for example need the context, so we get it here in case we
38
00:02:48,800 --> 00:02:49,950
need it even though we don't.
39
00:02:49,970 --> 00:02:54,770
Since we don't use it, of course you could use that underscore thing to indicate I know that, I get it but
40
00:02:54,770 --> 00:02:58,480
I don't care. Inside of the builder function here,
41
00:02:58,490 --> 00:03:06,110
you now return something and that something now is the widget you want to show inside of that modal
42
00:03:06,110 --> 00:03:06,700
sheet
43
00:03:06,790 --> 00:03:12,620
and in my case, that of course should be that new transaction.
44
00:03:12,620 --> 00:03:20,840
So here I want to show new transaction and for that, I now need to import my own new transaction widget with
45
00:03:20,840 --> 00:03:26,350
that relative import path and new transaction now has one disadvantage,
46
00:03:26,630 --> 00:03:34,580
it of course needs the add transaction function reference and we outsource that into user transactions
47
00:03:34,610 --> 00:03:38,150
because up until now, that was the perfect solution,
48
00:03:38,150 --> 00:03:40,950
we had new transaction and transaction list in there,
49
00:03:40,970 --> 00:03:45,080
we had our transaction lists or a list of objects in there,
50
00:03:45,110 --> 00:03:51,280
we had the add new transaction method in there and all that was hidden away from our main.dart file
51
00:03:51,620 --> 00:03:58,910
and now all of a sudden, because we have to trigger startAddNewTransaction from in here, in the main.dart
52
00:03:58,910 --> 00:04:03,980
file because here is where we have the app bar and the floating action button because we have
53
00:04:03,980 --> 00:04:06,000
to trigger that function from in here,
54
00:04:06,020 --> 00:04:13,430
we now have to bring new transaction back into that main.dart file and since that needs the add transaction
55
00:04:13,430 --> 00:04:22,040
method, we now actually have to bring all our state logic into this file. So there is no way around it,
56
00:04:22,130 --> 00:04:26,060
let's transform this stateless widget to a stateful widget.
57
00:04:26,060 --> 00:04:29,450
At least we can do that automatically with the help of the refactoring tool,
58
00:04:29,480 --> 00:04:35,050
if you click on it and then use the refactoring shortcut, you can choose convert to stateful widget
59
00:04:35,060 --> 00:04:43,310
here, you automatically get your state object and now in there, I will basically grab my list of transactions
60
00:04:43,610 --> 00:04:51,770
and add new transaction from inside the user transactions widget, add it in the main.dart file here below
61
00:04:51,770 --> 00:04:53,990
title controller and amount controller
62
00:04:56,760 --> 00:05:00,270
which I by the way don't need there anymore
63
00:05:06,430 --> 00:05:13,240
and now with the transactions and the add transaction method brought over, we need to import our transaction
64
00:05:13,240 --> 00:05:16,700
model to unlock that transaction class, so
65
00:05:16,750 --> 00:05:23,560
let's add this import here and of course now that means that our user transactions widget unfortunately
66
00:05:23,560 --> 00:05:27,970
is redundant and that is also something you'll have in Flutter development, which is why I'm showing
67
00:05:27,970 --> 00:05:28,680
it here.
68
00:05:28,780 --> 00:05:33,790
You will sometimes build a solution which you then have to break up at some point because you recognize
69
00:05:34,120 --> 00:05:37,340
okay for this feature, I need to manage my state somewhere else,
70
00:05:37,480 --> 00:05:43,510
otherwise I can't pass data around. Later in this course, we'll learn about a more elegant state management
71
00:05:43,510 --> 00:05:50,590
solution, which avoids having solutions like this where we have to lift state up to the common denominator
72
00:05:50,770 --> 00:05:56,050
but for the moment and for simple apps like this, it is fine and we also won't have a performance problem
73
00:05:56,290 --> 00:06:01,930
if we use our main file for managing the state and for rebuilding everything on every change therefore.
74
00:06:03,100 --> 00:06:08,760
We can get rid of the user transaction file however, we don't need it anymore
75
00:06:08,860 --> 00:06:12,570
and in main.dart, we hence
76
00:06:12,580 --> 00:06:18,880
now instead of returning our user transactions, return our transaction list widget, pass our user transactions
77
00:06:18,880 --> 00:06:24,050
into it, user transactions is now managed here in the main.dart already in our MyHomePage
78
00:06:24,040 --> 00:06:30,880
state class and the remaining thing we have to do therefore is that instead of importing user transactions,
79
00:06:31,090 --> 00:06:32,870
that's the widget I just deleted,
80
00:06:33,010 --> 00:06:37,930
we import the transaction_list.dart file with the transaction list widget.
81
00:06:37,930 --> 00:06:47,060
Now with that restructuring, we can pass _addNewTransaction, so a pointer at this method, to
82
00:06:47,070 --> 00:06:48,540
new transaction here
83
00:06:48,780 --> 00:06:55,920
and now we have a setup here where we just need to wire up this startAddNewTransaction method, which
84
00:06:55,920 --> 00:07:02,040
now should be _startAddNewTransaction because it's now part of that private state class
85
00:07:02,040 --> 00:07:06,080
and therefore the convention is to make this a private method as well,
86
00:07:06,090 --> 00:07:11,670
now we just have to use this method here and wire it up to our button in the action bar.
87
00:07:11,670 --> 00:07:17,430
So here instead of doing nothing when we press this, we target startAddNewTransaction.
88
00:07:17,430 --> 00:07:22,830
Now keep in mind that this requires the build context because it needs to forward that to show modal
89
00:07:22,830 --> 00:07:24,020
bottom sheet,
90
00:07:24,030 --> 00:07:29,880
so actually we need to call this as an anonymous function so that in here, we can call it manually and
91
00:07:29,890 --> 00:07:38,380
forward context, which is this context value here and now we do the exact same for our floating action
92
00:07:38,380 --> 00:07:45,400
button down here. And now with that if you save that and maybe restart the app because we changed a lot,
93
00:07:45,410 --> 00:07:48,560
typically the app needs to be restarted thereafter,
94
00:07:48,740 --> 00:07:54,740
if you do that now, if you press the plus up there, you'll see that this action sheet floats in and the same
95
00:07:54,770 --> 00:07:56,950
for the floating action bar at the bottom.
96
00:07:57,290 --> 00:07:59,250
That's a default feature you get,
97
00:07:59,270 --> 00:08:02,600
you can also click the background to close it,
98
00:08:02,640 --> 00:08:05,080
you can also click the actionsheet itself to close it,
99
00:08:05,100 --> 00:08:07,470
that should eventually change in the future.
100
00:08:07,470 --> 00:08:14,570
Until this changes, you can easily fix that by wrapping your new transaction in show modal bottom
101
00:08:14,590 --> 00:08:20,490
sheet with a gesture detector which is another widget I mentioned earlier on a slide, which takes a
102
00:08:20,490 --> 00:08:21,060
child,
103
00:08:21,060 --> 00:08:25,800
in this case our new transaction and where you can register a certain gesture,
104
00:08:25,920 --> 00:08:32,430
in this case onTap and simply do nothing when this gesture occurs. This catches this tapping on that
105
00:08:32,430 --> 00:08:35,100
bottom sheet and avoids that the sheet gets closed
106
00:08:35,130 --> 00:08:37,700
if you tap on the sheet. It still gets closed
107
00:08:37,700 --> 00:08:42,960
if you tap on the background but not if you tap on the sheet. For this to work, you also need to set a
108
00:08:42,960 --> 00:08:47,020
behavior and you need to set this to HitTestBehaviour.opaque,
109
00:08:47,280 --> 00:08:54,780
this is important to catch the event and avoid the tap here on this gesture detector
110
00:08:54,840 --> 00:09:01,260
which itself is invisible but which has the size of our new transaction widget, that a tap on that also
111
00:09:01,260 --> 00:09:07,920
then still triggers a tap on the underlying modal sheet. With this setting, you catch the tap event and
112
00:09:07,920 --> 00:09:10,080
avoid that it's handled by anyone else
113
00:09:10,200 --> 00:09:13,530
and this avoids that the sheet closes when you tap on the sheet itself.
114
00:09:13,530 --> 00:09:16,370
Again to my knowledge, this should change in the future,
115
00:09:16,470 --> 00:09:20,100
so maybe when you're viewing this, you don't need to add this detector anymore.
116
00:09:20,100 --> 00:09:24,390
Here you see now I avoid that this closes when I tap in here which is of course what I want,
117
00:09:24,390 --> 00:09:26,670
I don't want to close the sheet when I tap in here,
118
00:09:26,700 --> 00:09:27,540
I want to close it
119
00:09:27,540 --> 00:09:30,650
however when I tap on the background and that is what's happening.
13251
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.