From 341db327e9464d583428689bbaabb7653ad0c32b Mon Sep 17 00:00:00 2001 From: Yii Date: Fri, 22 Apr 2022 11:35:03 +0800 Subject: [PATCH] add NFTAttributes --- lib/core/util/web3/wallet_helper.dart | 3 +- lib/pages/begin/data/models/nft.dart | 47 +++++++++++++++++++ lib/pages/begin/data/models/nft.g.dart | 29 ++++++++++++ lib/pages/begin/data/models/nft_info.dart | 36 -------------- lib/pages/begin/data/models/wallet_info.dart | 2 +- .../repositories/begin_repository_impl.dart | 13 +++++ .../begin/data/states/load_nft_state.dart | 2 +- .../dialogs/transfer_nft_dialog.dart | 2 +- .../presentation/pages/nft_detail_page.dart | 25 ++++------ .../presentation/widgets/home/nft_tab.dart | 6 ++- 10 files changed, 107 insertions(+), 58 deletions(-) create mode 100644 lib/pages/begin/data/models/nft.dart create mode 100644 lib/pages/begin/data/models/nft.g.dart delete mode 100644 lib/pages/begin/data/models/nft_info.dart diff --git a/lib/core/util/web3/wallet_helper.dart b/lib/core/util/web3/wallet_helper.dart index 3077f81..d6569b6 100644 --- a/lib/core/util/web3/wallet_helper.dart +++ b/lib/core/util/web3/wallet_helper.dart @@ -5,7 +5,7 @@ import 'package:decimal/decimal.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:web3dart/web3dart.dart'; -import '../../../pages/begin/data/models/nft_info.dart'; +import '../../../pages/begin/data/models/nft.dart'; import '../../../pages/begin/data/models/token_info.dart'; import '../../../pages/begin/data/models/wallet_info.dart'; import '../../error/exceptions.dart'; @@ -134,7 +134,6 @@ class WalletHelper { (int index) => NFTInfo( tokenId: ownTokenIdList[index], imgPath: nftImageUrlList[index], - bgColor: CustomTheme.nftBgColors[index % 3], ), growable: true, ); diff --git a/lib/pages/begin/data/models/nft.dart b/lib/pages/begin/data/models/nft.dart new file mode 100644 index 0000000..a4c93b3 --- /dev/null +++ b/lib/pages/begin/data/models/nft.dart @@ -0,0 +1,47 @@ +import 'package:json_annotation/json_annotation.dart'; + +import '../../../../core/util/web3/abi/stream_chicken_2.g.dart'; + +part 'nft.g.dart'; + +class NFTCollection { + final List nftInfoList; + final String name; + final String tokenName; + final Stream_chicken_2 contract; + + NFTCollection({ + required this.nftInfoList, + required this.name, + required this.tokenName, + required this.contract, + }); +} + +@JsonSerializable(fieldRename: FieldRename.snake) +class NFTInfo { + final int tokenId; + final String imgPath; + List attributes = []; + + NFTInfo({ + required this.tokenId, + required this.imgPath, + }); +} + +@JsonSerializable(fieldRename: FieldRename.snake) +class NFTAttributes { + @JsonKey(defaultValue: '') + String type = ''; + @JsonKey(defaultValue: '') + String value = ''; + + NFTAttributes({ + required this.type, + required this.value, + }); + + factory NFTAttributes.fromJson(Map json) => _$NFTAttributesFromJson(json); + Map toJson() => _$NFTAttributesToJson(this); +} diff --git a/lib/pages/begin/data/models/nft.g.dart b/lib/pages/begin/data/models/nft.g.dart new file mode 100644 index 0000000..392a1a6 --- /dev/null +++ b/lib/pages/begin/data/models/nft.g.dart @@ -0,0 +1,29 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'nft.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +NFTInfo _$NFTInfoFromJson(Map json) => NFTInfo( + tokenId: json['token_id'] as int, + imgPath: json['img_path'] as String, + )..attributes = + (json['attributes'] as List).map((e) => NFTAttributes.fromJson(e as Map)).toList(); + +Map _$NFTInfoToJson(NFTInfo instance) => { + 'token_id': instance.tokenId, + 'img_path': instance.imgPath, + 'attributes': instance.attributes, + }; + +NFTAttributes _$NFTAttributesFromJson(Map json) => NFTAttributes( + type: json['type'] as String? ?? '', + value: json['value'] as String? ?? '', + ); + +Map _$NFTAttributesToJson(NFTAttributes instance) => { + 'type': instance.type, + 'value': instance.value, + }; diff --git a/lib/pages/begin/data/models/nft_info.dart b/lib/pages/begin/data/models/nft_info.dart deleted file mode 100644 index 994f374..0000000 --- a/lib/pages/begin/data/models/nft_info.dart +++ /dev/null @@ -1,36 +0,0 @@ -import 'package:flutter/material.dart'; - -import '../../../../core/util/web3/abi/stream_chicken_2.g.dart'; - -class NFTCollection { - final List nftInfoList; - final String name; - final String tokenName; - final Stream_chicken_2 contract; - - NFTCollection({ - required this.nftInfoList, - required this.name, - required this.tokenName, - required this.contract, - }); -} - -class NFTInfo { - final int tokenId; - final String imgPath; - final Color bgColor; - final NFTMetaData metaData = NFTMetaData(); - - NFTInfo({ - required this.tokenId, - required this.imgPath, - required this.bgColor, - }); -} - -class NFTMetaData { - final String body = '1'; - final String head = '5'; - final String beak = '3'; -} diff --git a/lib/pages/begin/data/models/wallet_info.dart b/lib/pages/begin/data/models/wallet_info.dart index 96d2ff9..5b2c9fe 100644 --- a/lib/pages/begin/data/models/wallet_info.dart +++ b/lib/pages/begin/data/models/wallet_info.dart @@ -1,4 +1,4 @@ -import 'nft_info.dart'; +import 'nft.dart'; import 'token_info.dart'; enum WalletImportMethod { metamask, privateKey, local } diff --git a/lib/pages/begin/data/repositories/begin_repository_impl.dart b/lib/pages/begin/data/repositories/begin_repository_impl.dart index 03c1c2d..2bb2122 100644 --- a/lib/pages/begin/data/repositories/begin_repository_impl.dart +++ b/lib/pages/begin/data/repositories/begin_repository_impl.dart @@ -1,7 +1,12 @@ +import 'dart:convert'; + +import 'package:dio/dio.dart'; + import '../../../../core/network/network_connection.dart'; import '../../domain/repositories/begin_repository.dart'; import '../datasources/begin_local_data_source.dart'; import '../datasources/begin_remote_data_source.dart'; +import '../models/nft.dart'; import '../models/user.dart'; class BeginRepositoryImpl implements BeginRepository { @@ -27,4 +32,12 @@ class BeginRepositoryImpl implements BeginRepository { return data; } } + + // Future getTokenMetaData({required String uri}) async { + // final response = await Dio().get(uri); + // if (response.statusCode != 200) return null; + // final data = const JsonDecoder().convert(response.data); + // return NFTAttributes.fromJson(data); + // } + } diff --git a/lib/pages/begin/data/states/load_nft_state.dart b/lib/pages/begin/data/states/load_nft_state.dart index 7248b13..7d03de2 100644 --- a/lib/pages/begin/data/states/load_nft_state.dart +++ b/lib/pages/begin/data/states/load_nft_state.dart @@ -1,6 +1,6 @@ import 'package:freezed_annotation/freezed_annotation.dart'; -import '../models/nft_info.dart'; +import '../models/nft.dart'; part 'load_nft_state.freezed.dart'; diff --git a/lib/pages/begin/presentation/dialogs/transfer_nft_dialog.dart b/lib/pages/begin/presentation/dialogs/transfer_nft_dialog.dart index 7c6bb40..8150315 100644 --- a/lib/pages/begin/presentation/dialogs/transfer_nft_dialog.dart +++ b/lib/pages/begin/presentation/dialogs/transfer_nft_dialog.dart @@ -8,7 +8,7 @@ import 'package:nft_wallet/pages/begin/domain/providers/home_provider.dart'; import '../../../../core/provider/shared_provider.dart'; import '../../../../core/util/theme.dart'; import '../../../../core/util/web3/abi/stream_chicken_2.g.dart'; -import '../../data/models/nft_info.dart'; +import '../../data/models/nft.dart'; import '../../data/models/wallet_info.dart'; import '../widgets/common_button.dart'; import '../widgets/common_loading.dart'; diff --git a/lib/pages/begin/presentation/pages/nft_detail_page.dart b/lib/pages/begin/presentation/pages/nft_detail_page.dart index 37f1995..1f015f4 100644 --- a/lib/pages/begin/presentation/pages/nft_detail_page.dart +++ b/lib/pages/begin/presentation/pages/nft_detail_page.dart @@ -4,7 +4,7 @@ import 'package:simple_animations/simple_animations.dart'; import '../../../../core/router/router.dart'; import '../../../../core/util/theme.dart'; -import '../../data/models/nft_info.dart'; +import '../../data/models/nft.dart'; import '../dialogs/transfer_nft_dialog.dart'; import '../widgets/common_button.dart'; @@ -149,15 +149,12 @@ class _NftDetailPageState extends State { CommonButton( onPress: () => _showTransferNFTDialog(), color: CustomTheme.secondColor, - child: SizedBox( - width: double.infinity, - child: Container( - padding: const EdgeInsets.all(20), - alignment: Alignment.center, - child: const Text( - 'send', - style: CustomTheme.textWhite, - ), + child: Container( + padding: const EdgeInsets.all(12), + alignment: Alignment.center, + child: const Text( + 'Transfer', + style: CustomTheme.textWhite, ), ), ), @@ -165,15 +162,13 @@ class _NftDetailPageState extends State { CommonButton( onPress: () {}, color: Colors.white, - child: Container( - width: double.infinity, - padding: const EdgeInsets.all(20), - alignment: Alignment.center, + child: Padding( + padding: const EdgeInsets.all(12), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Image.asset('assets/images/opensea.png', width: 24), - const SizedBox(width: 5), + const SizedBox(width: 8), const Text('Sell on Opensea', style: CustomTheme.textBlack), ], ), diff --git a/lib/pages/begin/presentation/widgets/home/nft_tab.dart b/lib/pages/begin/presentation/widgets/home/nft_tab.dart index 97b171e..5fe9dda 100644 --- a/lib/pages/begin/presentation/widgets/home/nft_tab.dart +++ b/lib/pages/begin/presentation/widgets/home/nft_tab.dart @@ -1,8 +1,10 @@ +import 'dart:math'; + import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import '../../../../../core/util/theme.dart'; -import '../../../data/models/nft_info.dart'; +import '../../../data/models/nft.dart'; import '../../pages/nft_detail_page.dart'; class NFTTab extends StatelessWidget { @@ -118,7 +120,7 @@ class _NFTItem extends StatelessWidget { margin: const EdgeInsets.only(right: 8), decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), - color: nftInfo.bgColor, + color: CustomTheme.nftBgColors[Random().nextInt(2) % 3], image: DecorationImage( image: CachedNetworkImageProvider(nftInfo.imgPath), fit: BoxFit.cover,