Fakultas Ilmu Komputer UI

sliver_app_bar_product.dart 5.84 KB
Newer Older
1
2
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
3
import 'package:home_industry/Component/cart_icon.dart';
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import 'package:home_industry/Pages/Product/bloc_search_product/bloc.dart';
import 'package:home_industry/Pages/Product/component/product_search_delegate.dart';
import 'package:home_industry/Pages/Product/repositories/product_repository.dart';

class SliverAppBarProduct extends StatefulWidget {
  final String label;
  final Widget child;
  final ScrollController scrollController;

  const SliverAppBarProduct(
      {Key key,
      @required this.label,
      @required this.scrollController,
      @required this.child})
      : assert(label != null),
        assert(scrollController != null),
        super(key: key);

  @override
  _SliverAppBarProductState createState() => _SliverAppBarProductState();
}

class _SliverAppBarProductState extends State<SliverAppBarProduct> {
  static const Duration duration = Duration(milliseconds: 300);
  bool isFirst = true;

  void _onscroll() {
    final _scrollThreshold = MediaQuery.of(context).size.height / 3.5 - 50;
    final currentScroll = widget.scrollController.position.pixels;
    if (currentScroll > _scrollThreshold) {
      setState(() {
        isFirst = false;
      });
    } else {
      setState(() {
        isFirst = true;
      });
    }
  }

  Future<void> onSearchPressed() async {
    final searchProductBloc =
        SearchProductBloc(RepositoryProvider.of<ProductRepository>(context));
    await showSearch(
        context: context, delegate: ProductSearchDelegate(searchProductBloc));
    await searchProductBloc.close();
  }

  @override
  void initState() {
    widget.scrollController.addListener(_onscroll);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SliverAppBar(
      automaticallyImplyLeading: false,
      leading: Container(
        height: 5,
        width: 5,
        padding: const EdgeInsets.only(left: 10, top: 4),
        child: AnimatedCrossFade(
          duration: duration,
          firstChild: Material(
            color: Colors.transparent,
            child: Ink(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
71
72
              height: 40,
              width: 40,
73
              decoration: ShapeDecoration(
74
                color: Colors.grey.withOpacity(0.7),
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
                shape: const CircleBorder(),
              ),
              child: IconButton(
                icon: Icon(Icons.arrow_back),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ),
          ),
          secondChild: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
          crossFadeState:
              isFirst ? CrossFadeState.showFirst : CrossFadeState.showSecond,
        ),
      ),
      floating: false,
      pinned: true,
      snap: false,
      backgroundColor: Colors.white,
      title: AnimatedCrossFade(
        duration: duration,
        firstChild: Container(
          width: MediaQuery.of(context).size.width / 2.5,
          alignment: Alignment.center,
          decoration: BoxDecoration(
105
            color: Colors.grey.withOpacity(0.7),
106
107
108
            borderRadius: const BorderRadius.all(Radius.circular(8)),
          ),
          child: Text(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
109
            '${widget.label}',
110
111
112
113
114
115
116
117
118
            maxLines: 2,
            textAlign: TextAlign.center,
            style: TextStyle(color: Colors.black),
          ),
        ),
        secondChild: Container(
          width: MediaQuery.of(context).size.width / 3,
          alignment: Alignment.center,
          child: Text(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
119
            '${widget.label}',
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
            maxLines: 2,
            textAlign: TextAlign.center,
            style: TextStyle(color: Colors.black),
          ),
        ),
        crossFadeState:
            isFirst ? CrossFadeState.showFirst : CrossFadeState.showSecond,
      ),
      centerTitle: true,
      actions: <Widget>[
        Padding(
          padding: const EdgeInsets.only(top: 4),
          child: AnimatedCrossFade(
            duration: duration,
            firstChild: _ActionFlexibleExpanded(
              onClick: onSearchPressed,
136
              icon: const Icon(Icons.search),
137
138
            ),
            secondChild: IconButton(
139
                icon: const Icon(Icons.search), onPressed: onSearchPressed),
140
141
142
143
144
145
146
147
148
149
150
            crossFadeState:
                isFirst ? CrossFadeState.showFirst : CrossFadeState.showSecond,
          ),
        ),
        const SizedBox(
          width: 2,
        ),
        Padding(
          padding: const EdgeInsets.only(top: 4),
          child: AnimatedCrossFade(
            duration: duration,
151
152
153
154
            firstChild: const _ActionFlexibleExpanded(
              widgetOveride: CartIcon(),
              icon: null,
              onClick: null,
155
            ),
156
            secondChild: const CartIcon(),
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
            crossFadeState:
                isFirst ? CrossFadeState.showFirst : CrossFadeState.showSecond,
          ),
        ),
        const SizedBox(
          width: 2,
        ),
      ],
      expandedHeight: MediaQuery.of(context).size.height / 3.5,
      flexibleSpace: FlexibleSpaceBar(
        background: widget.child,
      ),
    );
  }
}

class _ActionFlexibleExpanded extends StatelessWidget {
  final Icon icon;
  final Function onClick;
176
  final Widget widgetOveride;
177
178
179
180
  const _ActionFlexibleExpanded({
    Key key,
    @required this.onClick,
    @required this.icon,
181
    this.widgetOveride,
182
183
184
185
186
187
188
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Ink(
Tsamara Esperanti Erwin's avatar
Tsamara Esperanti Erwin committed
189
190
        height: 40,
        width: 40,
191
        decoration: ShapeDecoration(
192
          color: Colors.grey.withOpacity(0.7),
193
194
          shape: const CircleBorder(),
        ),
195
        child: widgetOveride ?? IconButton(icon: icon, onPressed: onClick),
196
197
198
199
      ),
    );
  }
}