@@ -60,27 +60,24 @@ class _CoinDetailsInfoState extends State<CoinDetailsInfo>
60
60
void initState () {
61
61
super .initState ();
62
62
const selectedDurationInitial = Duration (hours: 1 );
63
- final growthBloc = context.read <PortfolioGrowthBloc >();
64
-
65
- growthBloc.add (
66
- PortfolioGrowthLoadRequested (
67
- coins: [widget.coin],
68
- fiatCoinId: 'USDT' ,
69
- selectedPeriod: selectedDurationInitial,
70
- walletId: _walletId! ,
71
- ),
72
- );
73
-
74
- final ProfitLossBloc profitLossBloc = context.read <ProfitLossBloc >();
75
63
76
- profitLossBloc.add (
77
- ProfitLossPortfolioChartLoadRequested (
78
- coins: [widget.coin],
79
- selectedPeriod: const Duration (hours: 1 ),
80
- fiatCoinId: 'USDT' ,
81
- walletId: _walletId! ,
82
- ),
83
- );
64
+ context.read <PortfolioGrowthBloc >().add (
65
+ PortfolioGrowthLoadRequested (
66
+ coins: [widget.coin],
67
+ fiatCoinId: 'USDT' ,
68
+ selectedPeriod: selectedDurationInitial,
69
+ walletId: _walletId! ,
70
+ ),
71
+ );
72
+
73
+ context.read <ProfitLossBloc >().add (
74
+ ProfitLossPortfolioChartLoadRequested (
75
+ coins: [widget.coin],
76
+ selectedPeriod: const Duration (hours: 1 ),
77
+ fiatCoinId: 'USDT' ,
78
+ walletId: _walletId! ,
79
+ ),
80
+ );
84
81
}
85
82
86
83
@override
@@ -363,11 +360,70 @@ class _CoinDetailsInfoHeader extends StatelessWidget {
363
360
}
364
361
}
365
362
366
- class _CoinDetailsMarketMetricsTabBar extends StatelessWidget {
363
+ class _CoinDetailsMarketMetricsTabBar extends StatefulWidget {
367
364
const _CoinDetailsMarketMetricsTabBar ({required this .coin});
368
365
369
366
final Coin coin;
370
367
368
+ @override
369
+ _CoinDetailsMarketMetricsTabBarState createState () =>
370
+ _CoinDetailsMarketMetricsTabBarState ();
371
+ }
372
+
373
+ class _CoinDetailsMarketMetricsTabBarState
374
+ extends State <_CoinDetailsMarketMetricsTabBar >
375
+ with TickerProviderStateMixin {
376
+ TabController ? _tabController;
377
+ int _currentIndex = 0 ;
378
+
379
+ void _initializeTabController (int numTabs) {
380
+ _tabController = TabController (
381
+ length: numTabs,
382
+ vsync: this ,
383
+ initialIndex: _currentIndex < numTabs ? _currentIndex : 0 ,
384
+ );
385
+
386
+ _tabController! .addListener (() {
387
+ if (_tabController! .indexIsChanging) {
388
+ setState (() {
389
+ _currentIndex = _tabController! .index;
390
+ });
391
+ }
392
+ });
393
+ }
394
+
395
+ @override
396
+ void didChangeDependencies () {
397
+ super .didChangeDependencies ();
398
+
399
+ final portfolioGrowthState = context.watch <PortfolioGrowthBloc >().state;
400
+ final profitLossState = context.watch <ProfitLossBloc >().state;
401
+ final isPortfolioGrowthSupported =
402
+ portfolioGrowthState is ! PortfolioGrowthChartUnsupported ;
403
+ final isProfitLossSupported =
404
+ profitLossState is ! PortfolioProfitLossChartUnsupported ;
405
+ final areChartsSupported =
406
+ isPortfolioGrowthSupported || isProfitLossSupported;
407
+ final numChartsSupported =
408
+ (isPortfolioGrowthSupported ? 1 : 0 ) + (isProfitLossSupported ? 1 : 0 );
409
+
410
+ if (areChartsSupported) {
411
+ if (_tabController == null ||
412
+ _tabController! .length != numChartsSupported) {
413
+ _initializeTabController (numChartsSupported);
414
+ }
415
+ } else {
416
+ _tabController? .dispose ();
417
+ _tabController = null ;
418
+ }
419
+ }
420
+
421
+ @override
422
+ void dispose () {
423
+ _tabController? .dispose ();
424
+ super .dispose ();
425
+ }
426
+
371
427
@override
372
428
Widget build (BuildContext context) {
373
429
final portfolioGrowthState = context.watch <PortfolioGrowthBloc >().state;
@@ -378,55 +434,46 @@ class _CoinDetailsMarketMetricsTabBar extends StatelessWidget {
378
434
profitLossState is ! PortfolioProfitLossChartUnsupported ;
379
435
final areChartsSupported =
380
436
isPortfolioGrowthSupported || isProfitLossSupported;
381
- final numChartsSupported = 0 +
382
- (isPortfolioGrowthSupported ? 1 : 0 ) +
383
- (isProfitLossSupported ? 1 : 0 );
437
+ final numChartsSupported =
438
+ (isPortfolioGrowthSupported ? 1 : 0 ) + (isProfitLossSupported ? 1 : 0 );
384
439
385
440
if (! areChartsSupported) {
386
441
return const SizedBox .shrink ();
387
442
}
388
443
389
- final TabController tabController = TabController (
390
- length: numChartsSupported,
391
- vsync: Navigator .of (context),
392
- );
444
+ if (_tabController == null ) {
445
+ _initializeTabController (numChartsSupported);
446
+ }
393
447
394
448
return Column (
395
449
children: [
396
450
Card (
397
451
child: TabBar (
398
- controller: tabController ,
452
+ controller: _tabController ,
399
453
tabs: [
400
- // spread operator used to ensure that tabs and views are
401
- // in sync
402
- ...([
403
- if (isPortfolioGrowthSupported)
404
- Tab (text: LocaleKeys .growth.tr ()),
405
- if (isProfitLossSupported)
406
- Tab (text: LocaleKeys .profitAndLoss.tr ()),
407
- ]),
454
+ if (isPortfolioGrowthSupported) Tab (text: LocaleKeys .growth.tr ()),
455
+ if (isProfitLossSupported)
456
+ Tab (text: LocaleKeys .profitAndLoss.tr ()),
408
457
],
409
458
),
410
459
),
411
460
SizedBox (
412
461
height: 340 ,
413
462
child: TabBarView (
414
- controller: tabController ,
463
+ controller: _tabController ,
415
464
children: [
416
- ...([
417
- if (isPortfolioGrowthSupported)
418
- SizedBox (
419
- width: double .infinity,
420
- height: 340 ,
421
- child: PortfolioGrowthChart (initialCoins: [coin]),
422
- ),
423
- if (isProfitLossSupported)
424
- SizedBox (
425
- width: double .infinity,
426
- height: 340 ,
427
- child: PortfolioProfitLossChart (initialCoins: [coin]),
428
- ),
429
- ]),
465
+ if (isPortfolioGrowthSupported)
466
+ SizedBox (
467
+ width: double .infinity,
468
+ height: 340 ,
469
+ child: PortfolioGrowthChart (initialCoins: [widget.coin]),
470
+ ),
471
+ if (isProfitLossSupported)
472
+ SizedBox (
473
+ width: double .infinity,
474
+ height: 340 ,
475
+ child: PortfolioProfitLossChart (initialCoins: [widget.coin]),
476
+ ),
430
477
],
431
478
),
432
479
),
0 commit comments