Fakultas Ilmu Komputer UI

detail_product.dart 12.4 KB
Newer Older
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
1
2
3
4
5
6
7
8
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:home_industry/Component/blue_button.dart';
import 'package:home_industry/Component/border_blue_button.dart';
import 'package:home_industry/Component/cart_icon.dart';
import 'package:home_industry/Component/price_text_formatter.dart';
import 'package:home_industry/Component/product_image.dart';
import 'package:home_industry/Component/router.dart';
9
10
import 'package:home_industry/Pages/Cart/bloc/cart_bloc.dart';
import 'package:home_industry/Pages/Cart/bloc/cart_event.dart';
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import 'package:home_industry/Pages/Cart/cart.dart';
import 'package:home_industry/Pages/Product/model/product/product.dart';
import 'package:provider/provider.dart';

class DetailProduct extends StatelessWidget {
  final Product product;

  const DetailProduct({Key key, @required this.product})
      : assert(product != null),
        super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
25
26
27
28
29
      appBar: AppBar(
        elevation: 0.5,
        actions: <Widget>[
          const CartIcon(),
        ],
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
      ),
      body: _BodyDetailProduct(product: product),
    );
  }
}

class _BodyDetailProduct extends StatelessWidget {
  const _BodyDetailProduct({
    Key key,
    @required this.product,
  }) : super(key: key);

  final Product product;
  static const int int64MaxValue = 9223372036854775807;

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<CounterItem>(
      create: (context) => CounterItem(
          total: 1,
          minimum: 1,
          maksimum: product.preOrder ? int64MaxValue : product.stock),
      child: Builder(
        builder: (BuildContext context) {
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
          return LayoutBuilder(
              builder: (BuildContext context, BoxConstraints viewport) {
            return SingleChildScrollView(
              child: ConstrainedBox(
                constraints: BoxConstraints(minHeight: viewport.maxHeight),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        SizedBox(
                          height: MediaQuery.of(context).size.height / 4,
                          child: Hero(
                              tag: product.id,
                              child: CachedImage(url: product.image)),
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 16),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Container(
                                    margin: const EdgeInsets.symmetric(
                                        vertical: 10),
                                    child: Text(
                                      product.name,
                                      style: const TextStyle(
                                          fontSize: 22,
                                          fontWeight: FontWeight.w800),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
88
89
                                    ),
                                  ),
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Expanded(
                                        flex: 6,
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: <Widget>[
                                            PriceTextFormatter(
                                              price: product.price,
                                              style: TextStyle(
                                                  color: Theme.of(context)
                                                      .primaryColor,
                                                  fontSize: 20),
                                            ),
                                            Text(
                                              product.preOrder
                                                  ? 'Preorder'
                                                  : 'Stok: ${product.stock}',
                                              style:
                                                  const TextStyle(fontSize: 17),
                                            )
                                          ],
                                        ),
                                      ),
                                      Expanded(
                                        flex: 5,
                                        child: Row(
                                          mainAxisAlignment:
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
121
                                              MainAxisAlignment.spaceAround,
122
123
                                          children: <Widget>[
                                            Ink(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
124
125
                                              width: 30,
                                                height: 30,
126
127
128
129
130
131
                                                decoration:
                                                    const ShapeDecoration(
                                                  color: Colors.red,
                                                  shape: CircleBorder(),
                                                ),
                                                child: IconButton(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
132
                                                    icon: const Icon(Icons.remove),
133
134
135
136
137
138
                                                    onPressed: () {
                                                      context
                                                          .read<CounterItem>()
                                                          .decrement();
                                                    },
                                                    color: Colors.white,
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
139
                                                    iconSize: 15)),
140
141
142
143
144
145
146
147
                                            Consumer<CounterItem>(
                                              builder: (_, counter, __) => Text(
                                                '${counter.total}',
                                                style: const TextStyle(
                                                    fontSize: 22),
                                              ),
                                            ),
                                            Ink(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
148
149
                                                width: 30,
                                                height: 30,
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
150
                                                decoration: const ShapeDecoration(
151
                                                  color: Colors.green,
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
152
                                                  shape: CircleBorder(),
153
154
                                                ),
                                                child: IconButton(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
155
                                                  icon: const Icon(Icons.add),
156
157
158
159
160
161
                                                  onPressed: () {
                                                    context
                                                        .read<CounterItem>()
                                                        .increment();
                                                  },
                                                  color: Colors.white,
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
162
                                                  iconSize: 15,
163
164
165
166
167
168
169
170
                                                )),
                                          ],
                                        ),
                                      )
                                    ],
                                  )
                                ],
                              ),
171
                              const Divider(
172
173
174
175
176
177
178
179
180
181
182
183
                                height: 16,
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  const Padding(
                                    padding: EdgeInsets.only(bottom: 5),
                                    child: Text(
                                      'Deskripsi',
                                      style: TextStyle(
                                          fontSize: 20,
                                          fontWeight: FontWeight.w800),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
184
                                    ),
185
186
187
188
189
190
191
192
193
                                  ),
                                  Text(
                                    '${product.description}',
                                    style: const TextStyle(fontSize: 16),
                                  ),
                                ],
                              )
                            ],
                          ),
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
194
                        ),
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 16, vertical: 12),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: <Widget>[
                          SizedBox(
                              width: double.infinity,
                              height: 45,
                              child: Consumer<CounterItem>(
                                builder: (_, counter, __) => BorderBlueButton(
                                    onPressed: product.stock == 0
                                        ? null
                                        : () {
                                            Scaffold.of(context).showSnackBar(
                                                SnackBar(
                                                    backgroundColor:
                                                        Theme.of(context)
                                                            .primaryColor,
216
217
218
                                                    content: const Text(
                                                        'Produk ditambahkan '
                                                        'ke keranjang')));
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
                                            BlocProvider.of<CartBloc>(context)
                                                .add(AddCartItem(
                                                    counter.total, product.id));
                                          },
                                    label: product.stock == 0
                                        ? 'HABIS'
                                        : 'TAMBAH KE KERANJANG'),
                              )),
                          if (product.stock != 0)
                            const Divider(
                              height: 12,
                            ),
                          if (product.stock != 0)
                            SizedBox(
                                width: double.infinity,
                                height: 45,
                                child: Consumer<CounterItem>(
                                  builder: (_, counter, __) => BlueButton(
                                      onPressed: () {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
238
239
240
                                        BlocProvider.of<CartBloc>(context).add(
                                            AddCartItem(
                                                counter.total, product.id));
241
242
                                        Navigator.of(context)
                                            .pushNamed(Router.cartPage);
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
243
                                      },
244
245
246
247
248
249
                                      label: 'BELI'),
                                ))
                        ],
                      ),
                    )
                  ],
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
250
                ),
251
252
253
              ),
            );
          });
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
254
255
256
257
258
        },
      ),
    );
  }
}