Skip to content

Commit

Permalink
fix: ewm-392 fix focus on current account (#625)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrey Malochka <[email protected]>
  • Loading branch information
AndreyMolochko and Andrey Malochka authored Nov 19, 2024
1 parent 87812f5 commit e7ca864
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ class RequestPermissionsWidget
Widget build(RequestPermissionsWidgetModel wm) => ValueListenableBuilder(
valueListenable: wm.step,
builder: (context, value, child) => switch (value) {
RequestPermissionsStep.account => _SelectAccountWidget(wm),
RequestPermissionsStep.account =>
_SelectAccountWidget(wm, scrollController),
RequestPermissionsStep.confirm => _ConfirmPermissionsWidget(wm),
},
);
}

class _SelectAccountWidget extends StatelessWidget {
const _SelectAccountWidget(this.wm);
const _SelectAccountWidget(this.wm, this.scrollController);

final RequestPermissionsWidgetModel wm;
final ScrollController scrollController;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -78,7 +80,12 @@ class _SelectAccountWidget extends StatelessWidget {
child: DoubleSourceBuilder(
firstSource: wm.accounts,
secondSource: wm.selected,
builder: (_, accounts, selected) => ListView.separated(
builder: (_, accounts, selected) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollToActiveAccount(accounts, selected);
});
return ListView.separated(
controller: scrollController,
itemCount: accounts?.length ?? 0,
itemBuilder: (_, index) {
final account = accounts?[index];
Expand All @@ -95,7 +102,8 @@ class _SelectAccountWidget extends StatelessWidget {
separatorBuilder: (_, __) => CommonDivider(
color: theme.colors.border0,
),
),
);
},
),
),
),
Expand All @@ -113,6 +121,21 @@ class _SelectAccountWidget extends StatelessWidget {
],
);
}

void _scrollToActiveAccount(List<KeyAccount>? accounts, KeyAccount? selected) {
if (accounts != null && selected != null) {
final index = accounts!.indexWhere(
(account) => account.address == selected.address);

if (index != -1) {
scrollController.animateTo(
index * DimensSizeV2.d72,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
}
}
}
}

class _ConfirmPermissionsWidget extends StatelessWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SelectAccountWidget extends ElementaryWidget<SelectAccountWidgetModel> {
secondSource: wm.currentAccount,
builder: (_, list, currentAccount) {
return SingleChildScrollView(
controller: scrollController,
child: Column(
mainAxisSize: MainAxisSize.min,
children: List.generate(
Expand All @@ -54,6 +55,7 @@ class SelectAccountWidget extends ElementaryWidget<SelectAccountWidgetModel> {
currentAccount: currentAccount,
onTapAccount: (item) => wm.onSelect(item),
getBalanceEntity: wm.getBalanceEntity,
scrollController: scrollController,
),
);
},
Expand Down Expand Up @@ -82,6 +84,29 @@ class SelectAccountWidget extends ElementaryWidget<SelectAccountWidgetModel> {
],
);
}

void _scrollToCurrentAccount({
required KeyAccount? currentAccount,
required Map<KeyAccount, GlobalKey> itemKeys,
required ScrollController scrollController,
}) {
if (currentAccount != null && itemKeys.containsKey(currentAccount)) {
final currentKey = itemKeys[currentAccount];
final context = currentKey?.currentContext;
if (context != null) {
final renderObject = context.findRenderObject() as RenderBox?;
if (renderObject != null) {
final offset = renderObject.localToGlobal(Offset.zero);
final scrollOffset = scrollController.offset + offset.dy;
scrollController.animateTo(
scrollOffset,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
}
}
}
}
}

class _SeedItem extends StatefulWidget {
Expand All @@ -91,6 +116,7 @@ class _SeedItem extends StatefulWidget {
required this.currentAccount,
required this.onTapAccount,
required this.getBalanceEntity,
required this.scrollController,
super.key,
});

Expand All @@ -99,6 +125,7 @@ class _SeedItem extends StatefulWidget {
final KeyAccount? currentAccount;
final Function(KeyAccount) onTapAccount;
final ListenableState<Money> Function(KeyAccount) getBalanceEntity;
final ScrollController scrollController;

@override
State<_SeedItem> createState() => _SeedItemState();
Expand Down Expand Up @@ -139,6 +166,7 @@ class _SeedItemState extends State<_SeedItem> {
currentAccount: widget.currentAccount,
onTap: widget.onTapAccount,
getBalanceEntity: widget.getBalanceEntity,
scrollController: widget.scrollController,
)
: const SizedBox.shrink(),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class PrivateKeyItemWidget extends StatelessWidget {
required this.currentAccount,
required this.onTap,
required this.getBalanceEntity,
required this.scrollController,
super.key,
});

final List<SeedWithInfo> seedWithInfo;
final KeyAccount? currentAccount;
final Function(KeyAccount) onTap;
final ListenableState<Money> Function(KeyAccount) getBalanceEntity;
final ScrollController scrollController;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -74,6 +76,7 @@ class PrivateKeyItemWidget extends StatelessWidget {
currentAccount: currentAccount,
onTap: onTap,
getBalanceEntity: getBalanceEntity,
scrollController: scrollController,
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,33 @@ class PublicKeyItemWidget extends StatelessWidget {
required this.currentAccount,
required this.onTap,
required this.getBalanceEntity,
required this.scrollController,
super.key,
});

final List<KeyAccount> accounts;
final KeyAccount? currentAccount;
final Function(KeyAccount) onTap;
final ListenableState<Money> Function(KeyAccount) getBalanceEntity;
final ScrollController scrollController;

@override
Widget build(BuildContext context) {
final theme = context.themeStyleV2;

final Map<KeyAccount, GlobalKey> itemKeys = {
for (var account in accounts) account: GlobalKey(),
};

WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollToCurrentAccount(itemKeys);
});

return Column(
children: [
for (var i = 0; i < accounts.length; i++)
GestureDetector(
key: itemKeys[accounts[i]],
onTap: () => onTap(accounts[i]),
child: Container(
decoration: BoxDecoration(
Expand Down Expand Up @@ -109,4 +121,25 @@ class PublicKeyItemWidget extends StatelessWidget {
],
);
}

void _scrollToCurrentAccount(Map<KeyAccount, GlobalKey> itemKeys) {
if (currentAccount == null || !itemKeys.containsKey(currentAccount)) {
return;
}

final currentKey = itemKeys[currentAccount]!;
final context = currentKey.currentContext;

if (context != null) {
final renderBox = context.findRenderObject() as RenderBox;
final offset = renderBox.localToGlobal(Offset.zero);
final scrollOffset = scrollController.offset + offset.dy;

scrollController.animateTo(
scrollOffset,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
}
}
}

0 comments on commit e7ca864

Please sign in to comment.