// ignore_for_file: use_build_context_synchronously import 'dart:async'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/services.dart'; import 'package:gecko/globals.dart'; import 'package:flutter/material.dart'; import 'package:gecko/models/widgets_keys.dart'; import 'package:gecko/providers/search.dart'; import 'package:gecko/providers/v2s_datapod.dart'; import 'package:gecko/widgets/commons/common_elements.dart'; import 'package:gecko/screens/search_result.dart'; import 'package:gecko/screens/wallet_view.dart'; import 'package:gecko/widgets/commons/offline_info.dart'; import 'package:provider/provider.dart'; class SearchScreen extends StatefulWidget { const SearchScreen({Key? key}) : super(key: key); @override State createState() => _SearchScreenState(); } class _SearchScreenState extends State { bool canPasteAddress = false; String pastedAddress = ''; Timer? debounce; final int debouneTime = 50; Future getClipBoard() async { final searchProvider = Provider.of(context, listen: false); final clipboard = await Clipboard.getData('text/plain'); pastedAddress = clipboard?.text ?? ''; canPasteAddress = isAddress(pastedAddress); searchProvider.reload(); } @override void initState() { WidgetsBinding.instance.addPostFrameCallback((_) async { await getClipBoard(); // final datapod = Provider.of(context, listen: false); // datapod.updateProfile( // address: '5CQ8T4qpbYJq7uVsxGPQ5q2df7x3Wa4aRY6HUWMBYjfLZhnn', // // address: '5CJKhFCpdSpumgWjSZ3TQmejJuHV6iELJrtdrfs38SXuiQeB', // geoloc: {"latitude": 48.536883, "longitude": 2.661986}, // title: 'Ta mère en string', // socials: [ // {'type': "website", 'url': "https://tamere.com"} // ], // city: "Melun 77000", // description: 'new prof !'); // // datapod.deleteProfile( // // address: '5CJKhFCpdSpumgWjSZ3TQmejJuHV6iELJrtdrfs38SXuiQeB'); // // datapod.migrateProfile( // // addressOld: '5CJKhFCpdSpumgWjSZ3TQmejJuHV6iELJrtdrfs38SXuiQeB', // // addressNew: '5CQ8T4qpbYJq7uVsxGPQ5q2df7x3Wa4aRY6HUWMBYjfLZhnn'); }); super.initState(); } @override Widget build(BuildContext context) { final searchProvider = Provider.of(context); final canValidate = searchProvider.searchController.text.length >= 2; return PopScope( onPopInvoked: (_) { searchProvider.searchController.text = ''; }, child: Scaffold( backgroundColor: backgroundColor, appBar: AppBar( elevation: 1, toolbarHeight: 60, title: Text('search'.tr()), ), body: SafeArea( child: Stack(children: [ Column(children: [ const SizedBox(height: 165), Padding( padding: const EdgeInsets.symmetric(horizontal: 17), child: TextField( onSubmitted: searchProvider.searchController.text.length >= 2 ? (_) { Navigator.push( context, MaterialPageRoute(builder: (context) { return const SearchResultScreen(); }), ); } : (value) {}, textInputAction: TextInputAction.search, key: keySearchField, controller: searchProvider.searchController, autofocus: true, maxLines: 1, textAlign: TextAlign.left, onChanged: (v) => { if (debounce?.isActive ?? false) {debounce!.cancel()}, debounce = Timer(Duration(milliseconds: debouneTime), () { getClipBoard(); searchProvider.reload(); }) }, decoration: InputDecoration( filled: true, fillColor: Colors.white, prefixIconConstraints: const BoxConstraints( minHeight: 32, ), suffixIcon: searchProvider.searchController.text == '' ? null : Padding( padding: const EdgeInsets.symmetric(horizontal: 10), child: IconButton( onPressed: (() async => { searchProvider.searchController.text = '', await getClipBoard(), searchProvider.reload(), }), icon: Icon( Icons.close, color: Colors.grey[600], size: 30, ), ), ), prefixIcon: const Padding( padding: EdgeInsets.symmetric(horizontal: 13), child: Image( image: AssetImage('assets/loupe-noire.png'), height: 30), ), border: OutlineInputBorder( borderSide: BorderSide(color: Colors.grey[500]!, width: 2), borderRadius: BorderRadius.circular(8)), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.grey[500]!, width: 2.5), borderRadius: BorderRadius.circular(8), ), contentPadding: const EdgeInsets.all(13), ), style: const TextStyle( fontSize: 18, color: Colors.black, fontWeight: FontWeight.w400, ), ), ), const Spacer(), SizedBox( width: 280, height: 80, child: ElevatedButton( key: keyConfirmSearch, style: ElevatedButton.styleFrom( foregroundColor: Colors.white, elevation: 4, backgroundColor: orangeC, // foreground ), onPressed: canValidate ? () { Navigator.push( context, MaterialPageRoute(builder: (context) { return const SearchResultScreen(); }), ); } : canPasteAddress ? () async { Navigator.push( context, MaterialPageRoute(builder: (context) { return WalletViewScreen( address: pastedAddress, username: null); }), ); } : null, child: Text( canValidate ? 'search'.tr() : canPasteAddress ? 'pasteAddress'.tr() : 'search'.tr(), textAlign: TextAlign.center, style: const TextStyle( fontSize: 19, fontWeight: FontWeight.w600), ), ), ), const Spacer(), ]), const OfflineInfo(), ]), ), ), ); } }