stack_wallet/lib/pages/receive_view/addresses/wallet_addresses_view.dart

269 lines
8.9 KiB
Dart
Raw Normal View History

2023-02-01 15:29:51 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-03-21 23:18:07 +00:00
import 'package:flutter_svg/svg.dart';
2023-02-01 15:29:51 +00:00
import 'package:isar/isar.dart';
import 'package:stackwallet/db/main_db.dart';
import 'package:stackwallet/models/isar/models/isar_models.dart';
2023-02-02 21:37:59 +00:00
import 'package:stackwallet/pages/receive_view/addresses/address_card.dart';
2023-03-21 23:18:07 +00:00
import 'package:stackwallet/pages/receive_view/addresses/address_details_view.dart';
2023-02-02 21:37:59 +00:00
import 'package:stackwallet/providers/global/wallets_provider.dart';
2023-03-06 21:09:32 +00:00
import 'package:stackwallet/utilities/constants.dart';
2023-02-01 15:29:51 +00:00
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
2023-03-06 21:09:32 +00:00
import 'package:stackwallet/utilities/util.dart';
2023-02-01 15:29:51 +00:00
import 'package:stackwallet/widgets/background.dart';
import 'package:stackwallet/widgets/conditional_parent.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/loading_indicator.dart';
2023-03-21 23:18:07 +00:00
import 'package:stackwallet/widgets/stack_text_field.dart';
import 'package:tuple/tuple.dart';
import '../../../utilities/assets.dart';
import '../../../widgets/icon_widgets/x_icon.dart';
import '../../../widgets/textfield_icon_button.dart';
2023-02-01 15:29:51 +00:00
2023-03-06 21:09:32 +00:00
class WalletAddressesView extends ConsumerStatefulWidget {
const WalletAddressesView({
2023-02-01 15:29:51 +00:00
Key? key,
required this.walletId,
}) : super(key: key);
2023-03-06 21:09:32 +00:00
static const String routeName = "/walletAddressesView";
2023-02-01 15:29:51 +00:00
final String walletId;
@override
2023-03-06 21:09:32 +00:00
ConsumerState<WalletAddressesView> createState() =>
_WalletAddressesViewState();
}
class _WalletAddressesViewState extends ConsumerState<WalletAddressesView> {
final bool isDesktop = Util.isDesktop;
2023-03-21 23:18:07 +00:00
String _searchString = "";
late final TextEditingController _searchController;
final searchFieldFocusNode = FocusNode();
Future<List<int>> _search(String term) async {
if (term.isEmpty) {
return MainDB.instance
.getAddresses(widget.walletId)
.filter()
.group((q) => q
.subTypeEqualTo(AddressSubType.change)
.or()
.subTypeEqualTo(AddressSubType.receiving)
.or()
.subTypeEqualTo(AddressSubType.paynymReceive)
.or()
.subTypeEqualTo(AddressSubType.paynymNotification))
.and()
.not()
.typeEqualTo(AddressType.nonWallet)
.sortByDerivationIndex()
.idProperty()
.findAll();
}
final labels = await MainDB.instance
.getAddressLabels(widget.walletId)
.filter()
.group(
(q) => q
.valueContains(term, caseSensitive: false)
.or()
.addressStringContains(term, caseSensitive: false)
.or()
.group(
(q) => q
.tagsIsNotNull()
.and()
.tagsElementContains(term, caseSensitive: false),
),
)
.findAll();
if (labels.isEmpty) {
return [];
}
return MainDB.instance
.getAddresses(widget.walletId)
.filter()
.anyOf<AddressLabel, Address>(
labels, (q, e) => q.valueEqualTo(e.addressString))
.group((q) => q
.subTypeEqualTo(AddressSubType.change)
.or()
.subTypeEqualTo(AddressSubType.receiving)
.or()
.subTypeEqualTo(AddressSubType.paynymReceive)
.or()
.subTypeEqualTo(AddressSubType.paynymNotification))
.and()
.not()
.typeEqualTo(AddressType.nonWallet)
.sortByDerivationIndex()
.idProperty()
.findAll();
}
@override
void initState() {
_searchController = TextEditingController();
super.initState();
}
@override
void dispose() {
_searchController.dispose();
searchFieldFocusNode.dispose();
super.dispose();
}
2023-03-06 21:09:32 +00:00
@override
Widget build(BuildContext context) {
2023-02-02 21:37:59 +00:00
final coin = ref.watch(walletsChangeNotifierProvider
2023-03-06 21:09:32 +00:00
.select((value) => value.getManager(widget.walletId).coin));
2023-02-01 15:29:51 +00:00
return ConditionalParent(
condition: !isDesktop,
builder: (child) => Background(
child: Scaffold(
backgroundColor:
Theme.of(context).extension<StackColors>()!.background,
appBar: AppBar(
backgroundColor:
Theme.of(context).extension<StackColors>()!.backgroundAppBar,
leading: AppBarBackButton(
onPressed: () {
Navigator.of(context).pop();
},
),
2023-03-06 21:09:32 +00:00
titleSpacing: 0,
2023-02-01 15:29:51 +00:00
title: Text(
2023-03-06 21:09:32 +00:00
"Wallet addresses",
2023-02-01 15:29:51 +00:00
style: STextStyles.navBarTitle(context),
),
),
body: Padding(
padding: const EdgeInsets.all(16),
child: child,
),
),
),
2023-03-06 21:09:32 +00:00
child: Column(
children: [
SizedBox(
width: isDesktop ? 490 : null,
2023-03-21 23:18:07 +00:00
child: ClipRRect(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
child: TextField(
autocorrect: !isDesktop,
enableSuggestions: !isDesktop,
controller: _searchController,
focusNode: searchFieldFocusNode,
onChanged: (value) {
setState(() {
_searchString = value;
});
},
style: isDesktop
? STextStyles.desktopTextExtraSmall(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textFieldActiveText,
height: 1.8,
)
: STextStyles.field(context),
decoration: standardInputDecoration(
"Search...",
searchFieldFocusNode,
context,
desktopMed: isDesktop,
).copyWith(
prefixIcon: Padding(
padding: EdgeInsets.symmetric(
horizontal: isDesktop ? 12 : 10,
vertical: isDesktop ? 18 : 16,
),
child: SvgPicture.asset(
Assets.svg.search,
width: isDesktop ? 20 : 16,
height: isDesktop ? 20 : 16,
),
),
suffixIcon: _searchController.text.isNotEmpty
? Padding(
padding: const EdgeInsets.only(right: 0),
child: UnconstrainedBox(
child: Row(
children: [
TextFieldIconButton(
child: const XIcon(),
onTap: () async {
setState(() {
_searchController.text = "";
_searchString = "";
});
},
),
],
),
),
)
: null,
2023-03-06 21:09:32 +00:00
),
2023-02-01 15:29:51 +00:00
),
2023-03-06 21:09:32 +00:00
),
),
SizedBox(
height: isDesktop ? 20 : 16,
),
Expanded(
child: FutureBuilder(
2023-03-21 23:18:07 +00:00
future: _search(_searchString),
2023-03-07 13:48:25 +00:00
builder: (context, AsyncSnapshot<List<int>> snapshot) {
2023-03-06 21:09:32 +00:00
if (snapshot.connectionState == ConnectionState.done &&
snapshot.data != null) {
// listview
return ListView.separated(
itemCount: snapshot.data!.length,
separatorBuilder: (_, __) => Container(
height: 10,
),
itemBuilder: (_, index) => AddressCard(
walletId: widget.walletId,
2023-03-07 13:48:25 +00:00
addressId: snapshot.data![index],
2023-03-06 21:09:32 +00:00
coin: coin,
2023-03-21 23:18:07 +00:00
onPressed: () {
Navigator.of(context).pushNamed(
AddressDetailsView.routeName,
arguments: Tuple2(
snapshot.data![index],
widget.walletId,
),
);
},
2023-03-06 21:09:32 +00:00
),
);
} else {
return const Center(
child: LoadingIndicator(
height: 200,
width: 200,
),
);
}
},
),
),
],
2023-02-01 15:29:51 +00:00
),
);
}
}