Fakultas Ilmu Komputer UI

sliver_app_bar_product.dart 5.75 KB
Newer Older
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
1
2
3
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
71
72
73
74
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
105
106
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:home_industry/Component/cart_icon.dart';
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(
              decoration: ShapeDecoration(
                color: Colors.grey.withOpacity(0.7),
                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(
            color: Colors.grey.withOpacity(0.7),
            borderRadius: const BorderRadius.all(Radius.circular(8)),
          ),
          child: Text(
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
107
            '${widget.label}',
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
108
109
110
111
112
113
114
115
116
            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
117
            '${widget.label}',
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
            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,
              icon: const Icon(Icons.search),
            ),
            secondChild: IconButton(
                icon: const Icon(Icons.search), onPressed: onSearchPressed),
            crossFadeState:
                isFirst ? CrossFadeState.showFirst : CrossFadeState.showSecond,
          ),
        ),
        const SizedBox(
          width: 2,
        ),
        Padding(
          padding: const EdgeInsets.only(top: 4),
          child: AnimatedCrossFade(
            duration: duration,
            firstChild: const _ActionFlexibleExpanded(
              widgetOveride: CartIcon(),
              icon: null,
              onClick: null,
            ),
            secondChild: const CartIcon(),
            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;
  final Widget widgetOveride;
  const _ActionFlexibleExpanded({
    Key key,
    @required this.onClick,
    @required this.icon,
    this.widgetOveride,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Ink(
        decoration: ShapeDecoration(
          color: Colors.grey.withOpacity(0.7),
          shape: const CircleBorder(),
        ),
        child: widgetOveride ?? IconButton(icon: icon, onPressed: onClick),
      ),
    );
  }
}