|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:provider/provider.dart'; |
| 3 | +import 'package:sentinelx/shared_state/rate_state.dart'; |
| 4 | + |
| 5 | +class AmountWidget extends StatefulWidget { |
| 6 | + final num result; |
| 7 | + TextStyle style = new TextStyle(); |
| 8 | + TextAlign align = TextAlign.start; |
| 9 | + double height = 40; |
| 10 | + |
| 11 | + @override |
| 12 | + _AmountWidgetState createState() => _AmountWidgetState(); |
| 13 | + |
| 14 | + AmountWidget(this.result, {this.style, this.height, this.align}); |
| 15 | +} |
| 16 | + |
| 17 | +class _AmountWidgetState extends State<AmountWidget> with SingleTickerProviderStateMixin { |
| 18 | + PageController _pageController; |
| 19 | + |
| 20 | + @override |
| 21 | + void initState() { |
| 22 | + super.initState(); |
| 23 | + _pageController = new PageController(initialPage: RateState().index, keepPage: true, viewportFraction: 0.89); |
| 24 | + init(); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + @override |
| 29 | + Widget build(BuildContext context) { |
| 30 | + return GestureDetector( |
| 31 | + onLongPress: () { |
| 32 | + if (RateState().index == 2) { |
| 33 | + RateState().setViewIndex(0); |
| 34 | + } else { |
| 35 | + RateState().setViewIndex(RateState().index + 1); |
| 36 | + } |
| 37 | + }, |
| 38 | + child: AbsorbPointer( |
| 39 | + absorbing: true, |
| 40 | + child: Container( |
| 41 | + height: widget.height, |
| 42 | + child: PageView.builder( |
| 43 | + controller: _pageController, |
| 44 | + scrollDirection: Axis.vertical, |
| 45 | + physics: BouncingScrollPhysics(), |
| 46 | + itemCount: 3, |
| 47 | + itemBuilder: (context, position) { |
| 48 | + if (position == 0) { |
| 49 | + return Column( |
| 50 | + mainAxisAlignment: MainAxisAlignment.center, |
| 51 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 52 | + children: <Widget>[ |
| 53 | + Text( |
| 54 | + "${RateState().formatToBTCRate(widget.result)}", |
| 55 | + style: widget.style, |
| 56 | + textAlign: widget.align, |
| 57 | + ), |
| 58 | + ], |
| 59 | + ); |
| 60 | + } |
| 61 | + if (position == 1) { |
| 62 | + return Consumer<RateState>(builder: (context, model, c) { |
| 63 | + return Column( |
| 64 | + mainAxisAlignment: MainAxisAlignment.center, |
| 65 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 66 | + children: <Widget>[ |
| 67 | + Text( |
| 68 | + "${model.formatRate(widget.result)}", |
| 69 | + style: widget.style, |
| 70 | + textAlign: widget.align, |
| 71 | + ), |
| 72 | + ], |
| 73 | + ); |
| 74 | + }); |
| 75 | + } else { |
| 76 | + return Column( |
| 77 | + mainAxisAlignment: MainAxisAlignment.center, |
| 78 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 79 | + children: <Widget>[ |
| 80 | + Text( |
| 81 | + "${RateState().formatSatRate(widget.result)}", |
| 82 | + style: widget.style, |
| 83 | + textAlign: widget.align, |
| 84 | + ), |
| 85 | + ], |
| 86 | + ); |
| 87 | + } |
| 88 | + }, |
| 89 | + ), |
| 90 | + ), |
| 91 | + ), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + void init() async { |
| 96 | + |
| 97 | + RateState().addListener(() { |
| 98 | + if (_pageController.hasClients && RateState().index < 3 && RateState().index >= 0) { |
| 99 | + _pageController.animateToPage(RateState().index, duration: Duration(milliseconds: 300), curve: Curves.easeInOut); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | +} |
0 commit comments