Skip to content

Commit d2c9908

Browse files
committed
fix(market-metrics): coin details tab bar view resetting to growth chart
convert to stateful widget to store tab bar view state
1 parent ecc5a5b commit d2c9908

File tree

1 file changed

+99
-52
lines changed

1 file changed

+99
-52
lines changed

lib/views/wallet/coin_details/coin_details_info/coin_details_info.dart

+99-52
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,24 @@ class _CoinDetailsInfoState extends State<CoinDetailsInfo>
6060
void initState() {
6161
super.initState();
6262
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>();
7563

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+
);
8481
}
8582

8683
@override
@@ -363,11 +360,70 @@ class _CoinDetailsInfoHeader extends StatelessWidget {
363360
}
364361
}
365362

366-
class _CoinDetailsMarketMetricsTabBar extends StatelessWidget {
363+
class _CoinDetailsMarketMetricsTabBar extends StatefulWidget {
367364
const _CoinDetailsMarketMetricsTabBar({required this.coin});
368365

369366
final Coin coin;
370367

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+
371427
@override
372428
Widget build(BuildContext context) {
373429
final portfolioGrowthState = context.watch<PortfolioGrowthBloc>().state;
@@ -378,55 +434,46 @@ class _CoinDetailsMarketMetricsTabBar extends StatelessWidget {
378434
profitLossState is! PortfolioProfitLossChartUnsupported;
379435
final areChartsSupported =
380436
isPortfolioGrowthSupported || isProfitLossSupported;
381-
final numChartsSupported = 0 +
382-
(isPortfolioGrowthSupported ? 1 : 0) +
383-
(isProfitLossSupported ? 1 : 0);
437+
final numChartsSupported =
438+
(isPortfolioGrowthSupported ? 1 : 0) + (isProfitLossSupported ? 1 : 0);
384439

385440
if (!areChartsSupported) {
386441
return const SizedBox.shrink();
387442
}
388443

389-
final TabController tabController = TabController(
390-
length: numChartsSupported,
391-
vsync: Navigator.of(context),
392-
);
444+
if (_tabController == null) {
445+
_initializeTabController(numChartsSupported);
446+
}
393447

394448
return Column(
395449
children: [
396450
Card(
397451
child: TabBar(
398-
controller: tabController,
452+
controller: _tabController,
399453
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()),
408457
],
409458
),
410459
),
411460
SizedBox(
412461
height: 340,
413462
child: TabBarView(
414-
controller: tabController,
463+
controller: _tabController,
415464
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+
),
430477
],
431478
),
432479
),

0 commit comments

Comments
 (0)