【Flutter】有狀態組件StatefulWidgetScaffold組件屬性

🔥 本文由 程序喵正在路上 原創,CSDN首發!
💖 系列專欄:Flutter學習
🌠 首發時間:2024年5月26日
🦋 歡迎關注🖱點贊👍收藏🌟留言🐾

目錄

  • 有狀態組件StatefulWidget
    • 實現計數器功能
    • 實現動態列表
  • Scaffold屬性:BottomNavigationBar
    • 組件介紹
    • 自定義底部導航
    • 底部菜單選中
    • 自定義底部導航實現頁面切換
  • Scaffold屬性:FloatingActionButton
    • FloatingActionButton詳解
    • 實現類似閑魚App底部導航凸起按鈕
  • Scaffold屬性:抽屜菜單Drawer
    • DrawerHeader
    • UserAccountsDrawerHeader

有狀態組件StatefulWidget

Flutter 中自定義組件其實就是一個類,這個類需要繼承 StatelessWidget / StatefulWidget

  • StatelessWidget 是無狀態組件,狀態不可變的 widget
  • StatefulWidget 是有狀態組件,持有的狀態可能在 widget 生命周期改變
  • 通俗的講:如果我們想改變頁面中的數據的話,這個時候就需要用到 StatefulWidget

實現計數器功能

import 'package:flutter/material.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: const HomePage(),);}
}class HomePage extends StatefulWidget {const HomePage({super.key});State<HomePage> createState() => _HomePageState();
}class _HomePageState extends State<HomePage> {int _numCount = 0; //計數Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("flutter App"),),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [Text("$_numCount",style: Theme.of(context).textTheme.headlineMedium,),const SizedBox(height: 60),ElevatedButton(onPressed: () {setState(() {_numCount++;});},child: const Text("增加"))],),),//Scaffold組件中自帶的浮動按鈕floatingActionButton: FloatingActionButton(onPressed: () {setState(() {_numCount++;});},child: const Icon(Icons.add),),);}
}

在使用 StatefulWidget 后,建議我們代碼的框架寫成上面這樣,將 Scaffold 組件寫到自定義組件中,這樣我們可以使用其在有狀態組件中的一些屬性

在這里插入圖片描述

實現動態列表

需求:實現通過點擊按鈕就能添加列表項的效果

class _HomePageState extends State<HomePage> {final List<String> _list = []; //final修飾的列表可以增加元素Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("動態列表"),),floatingActionButton: FloatingActionButton(onPressed: () {setState(() {_list.add("我是一個新增的列表");});},child: const Icon(Icons.add),),body: ListView(children: _list.map((e) {return ListTile(title: Text(e),);}).toList(),),);}
}

在這里插入圖片描述

Scaffold屬性:BottomNavigationBar

組件介紹

BottomNavigationBar 是底部導航條,可以讓我們定義底部 Tab 切換,bottomNavigationBarScaffold 組件的參數。

BottomNavigationBar 常見的屬性

屬性名說明
itemsList 底部導航條按鈕集合
iconSizeicon
currentIndex默認選中第幾個
onTap選中變化回調函數
fixedColor選中的顏色
type底部超過4個菜單必須配置這個屬性:ButtomNavigationBarType.fixedButtomNavigationBarType.shifting

自定義底部導航

實現如下效果:

在這里插入圖片描述

class _HomePageState extends State<HomePage> {Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("自定義底部導航"),),body: const Center(child: Text("我是一個文本")),bottomNavigationBar: BottomNavigationBar(items: const [BottomNavigationBarItem(icon: Icon(Icons.home), label: "首頁"),BottomNavigationBarItem(icon: Icon(Icons.category), label: "分類"),BottomNavigationBarItem(icon: Icon(Icons.settings), label: "設置"),],),);}
}

底部菜單選中

前面自定義的底部導航還沒有完整實現,一般要求我們點擊哪個菜單,哪個菜單就會顯示選中的效果,下面我們來實現這個效果

在這里插入圖片描述

class _HomePageState extends State<HomePage> {int _currentIndex = 0; //當前選中索引Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("自定義底部導航"),),body: const Center(child: Text("我是一個文本")),bottomNavigationBar: BottomNavigationBar(currentIndex: _currentIndex,fixedColor: Colors.blue,onTap: (value) {setState(() {_currentIndex = value;});},items: const [BottomNavigationBarItem(icon: Icon(Icons.home), label: "首頁"),BottomNavigationBarItem(icon: Icon(Icons.category), label: "分類"),BottomNavigationBarItem(icon: Icon(Icons.settings), label: "設置"),],),);}
}

自定義底部導航實現頁面切換

為了讓代碼條理更加清晰,我們將每個頁面分文件寫。

lib 目錄下新建目錄 pages,在 pages 下新建文件 tabs.dart 和文件夾 tabs,在 tabs 下新建 3 個頁面對應的文件:

在這里插入圖片描述

home.dart

import 'package:flutter/material.dart';class HomePage extends StatefulWidget {const HomePage({super.key});State<HomePage> createState() => _HomePageState();
}class _HomePageState extends State<HomePage> {Widget build(BuildContext context) {return const Center(child: Text("首頁"),);}
}

category.dart

import 'package:flutter/material.dart';class CategoryPage extends StatefulWidget {const CategoryPage({super.key});State<CategoryPage> createState() => _CategoryPageState();
}class _CategoryPageState extends State<CategoryPage> {Widget build(BuildContext context) {return const Center(child: Text("分類"),);}
}

setting.dart

import 'package:flutter/material.dart';class SettingPage extends StatefulWidget {const SettingPage({super.key});State<SettingPage> createState() => _SettingPageState();
}class _SettingPageState extends State<SettingPage> {Widget build(BuildContext context) {return const Center(child: Text("系統設置"),);}
}

tabs.dart:前面頁面切換的代碼

import 'package:flutter/material.dart';
import './tabs/home.dart';
import './tabs/category.dart';
import './tabs/setting.dart';class Tabs extends StatefulWidget {const Tabs({super.key});State<Tabs> createState() => _TabsState();
}class _TabsState extends State<Tabs> {int _currentIndex = 0; //當前選中索引final List<Widget> _pages = const [HomePage(),CategoryPage(),SettingPage(),];Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("自定義底部導航"),),body: _pages[_currentIndex], //自動切換頁面bottomNavigationBar: BottomNavigationBar(currentIndex: _currentIndex,fixedColor: Colors.blue,onTap: (value) {setState(() {_currentIndex = value;});},items: const [BottomNavigationBarItem(icon: Icon(Icons.home), label: "首頁"),BottomNavigationBarItem(icon: Icon(Icons.category), label: "分類"),BottomNavigationBarItem(icon: Icon(Icons.settings), label: "設置"),],),);}
}

main.dart

import 'package:flutter/material.dart';
import 'pages/tabs.dart';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({super.key});Widget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),debugShowCheckedModeBanner: false,home: const Tabs(),);}
}

在這里插入圖片描述

Scaffold屬性:FloatingActionButton

FloatingActionButton詳解

FloatingActionButton 簡稱 FAB ,可以實現浮動按鈕,也可以實現類似閑魚app的底部凸起導航

屬性名稱屬性值
child子視圖,一般為 Icon,不推薦使用文字
tooltipFAB被長按時顯示,也是無障礙功能
backgroundColor背景顏色
elevation未點擊時的陰影
highlightElevation點擊時陰影值,默認12
onPressed點擊事件回調
shape可以定義FAB的形狀等
mini是否為 mini 類型,默認為 false

實現類似閑魚App底部導航凸起按鈕

我們在前面自定義底部導航的代碼稍加修改,再添加一個浮動按鈕即可實現類似閑魚底部導航效果:

在這里插入圖片描述

這里給出 tabs.dart 的代碼,其中 message.dartuser.dart 兩個頁面內容和其他頁面類似

import 'package:flutter/material.dart';
import './tabs/home.dart';
import './tabs/category.dart';
import './tabs/setting.dart';
import './tabs/message.dart';
import './tabs/user.dart';class Tabs extends StatefulWidget {const Tabs({super.key});State<Tabs> createState() => _TabsState();
}class _TabsState extends State<Tabs> {int _currentIndex = 0; //當前選中索引final List<Widget> _pages = const [HomePage(),CategoryPage(),MessagePage(),SettingPage(),UserPage(),];Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("浮動按鈕"),),body: _pages[_currentIndex], //自動切換頁面bottomNavigationBar: BottomNavigationBar(type: BottomNavigationBarType.fixed,currentIndex: _currentIndex,fixedColor: Colors.blue,onTap: (value) {setState(() {_currentIndex = value;});},items: const [BottomNavigationBarItem(icon: Icon(Icons.home), label: "首頁"),BottomNavigationBarItem(icon: Icon(Icons.category), label: "分類"),BottomNavigationBarItem(icon: Icon(Icons.message), label: "消息"),BottomNavigationBarItem(icon: Icon(Icons.settings), label: "設置"),BottomNavigationBarItem(icon: Icon(Icons.people), label: "用戶")],),floatingActionButton: Container(height: 60,width: 60,padding: const EdgeInsets.all(4),margin: const EdgeInsets.only(top: 4),decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(30)),child: FloatingActionButton(shape: const CircleBorder(),backgroundColor:_currentIndex == 2 ? Colors.yellowAccent : Colors.blueAccent,onPressed: () {setState(() {_currentIndex = 2;});},child: const Icon(Icons.add),),),floatingActionButtonLocation:FloatingActionButtonLocation.centerDocked, //浮動按鈕位置);}
}

Scaffold屬性:抽屜菜單Drawer

Scaffold 組件里面傳入 drawer 參數可以定義左側邊欄,傳入 endDrawer 可以定義右側邊欄。側邊欄默認是隱藏的,我們可以通過手指滑動顯示側邊欄,也可以通過點擊按鈕顯示側邊欄。

DrawerHeader

DrawerHeader 是側邊欄的頭部,常見屬性如下:

屬性描述
decoration設置頂部背景顏色
child配置子元素
padding內邊距
margin外邊距

在上一節代碼 tabs.dart 的基礎上,我們來給頁面添加側邊欄

class _TabsState extends State<Tabs> {int _currentIndex = 0; //當前選中索引final List<Widget> _pages = const [HomePage(),CategoryPage(),MessagePage(),SettingPage(),UserPage(),];Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text("浮動按鈕"),),//左側邊欄drawer: Drawer(child: Column(children: [DrawerHeader(decoration: const BoxDecoration(color: Colors.blueAccent,image: DecorationImage(image: NetworkImage("https://xixi-web-tlias.oss-cn-guangzhou.aliyuncs.com/1.jpg"),fit: BoxFit.cover)),child: ListView(children: const [ListTile(leading: CircleAvatar(backgroundImage: NetworkImage("https://xixi-web-tlias.oss-cn-guangzhou.aliyuncs.com/2.jpg"),),title: Text("嘻嘻",style: TextStyle(color: Colors.white,),),),ListTile(title: Text("Email: 123456@qq.com",style: TextStyle(color: Colors.white,),),),],),),const ListTile(leading: CircleAvatar(child: Icon(Icons.people)),title: Text("個人中心"),),const Divider(),const ListTile(leading: CircleAvatar(child: Icon(Icons.settings)),title: Text("系統設置"),)],),),//右側側邊欄endDrawer: Drawer(child: Column(children: [DrawerHeader(decoration: const BoxDecoration(color: Colors.blueAccent,image: DecorationImage(image: NetworkImage("https://xixi-web-tlias.oss-cn-guangzhou.aliyuncs.com/3.jpg"),fit: BoxFit.cover)),child: ListView(children: const [ListTile(leading: CircleAvatar(backgroundImage: NetworkImage("https://xixi-web-tlias.oss-cn-guangzhou.aliyuncs.com/4.jpg"),),title: Text("嘻嘻",style: TextStyle(color: Colors.white,),),),ListTile(title: Text("Email: 123456@qq.com",style: TextStyle(color: Colors.white,),),),],),),const ListTile(leading: CircleAvatar(child: Icon(Icons.people)),title: Text("個人中心"),),const Divider(),const ListTile(leading: CircleAvatar(child: Icon(Icons.settings)),title: Text("系統設置"),)],),),body: _pages[_currentIndex], //自動切換頁面bottomNavigationBar: BottomNavigationBar(type: BottomNavigationBarType.fixed,currentIndex: _currentIndex,fixedColor: Colors.blue,onTap: (value) {setState(() {_currentIndex = value;});},items: const [BottomNavigationBarItem(icon: Icon(Icons.home), label: "首頁"),BottomNavigationBarItem(icon: Icon(Icons.category), label: "分類"),BottomNavigationBarItem(icon: Icon(Icons.message), label: "消息"),BottomNavigationBarItem(icon: Icon(Icons.settings), label: "設置"),BottomNavigationBarItem(icon: Icon(Icons.people), label: "用戶")],),floatingActionButton: Container(height: 60,width: 60,padding: const EdgeInsets.all(4),margin: const EdgeInsets.only(top: 4),decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(30)),child: FloatingActionButton(shape: const CircleBorder(),backgroundColor:_currentIndex == 2 ? Colors.yellowAccent : Colors.blueAccent,onPressed: () {setState(() {_currentIndex = 2;});},child: const Icon(Icons.add),),),floatingActionButtonLocation:FloatingActionButtonLocation.centerDocked, //浮動按鈕位置);}
}

左右側邊欄只是名稱不同,其他組件的使用都是一致的

在這里插入圖片描述

在這里插入圖片描述

UserAccountsDrawerHeader

像前面這樣我們自己寫側邊欄的頭部內容,還是比較麻煩的,而且樣式也不好調整,為此 flutter 給我們提供了 UserAccountsDrawerHeader 組件,其常用屬性如下:

屬性描述
decoration設置頂部背景顏色
accountName賬戶名稱
accountEmail賬戶郵箱
currentAccountPicture用戶頭像
otherAccountsPictures用來設置當前賬戶其他賬戶頭像
margin外邊距

接下來,我們用 來寫一個側邊欄

//左側邊欄
drawer: Drawer(child: Column(children: [UserAccountsDrawerHeader(accountName: const Text("嘻嘻"),accountEmail: const Text("123456@qq.com"),currentAccountPicture: const CircleAvatar(backgroundImage: NetworkImage("https://xixi-web-tlias.oss-cn-guangzhou.aliyuncs.com/1.jpg"),),decoration: const BoxDecoration(color: Colors.blue,image: DecorationImage(image: NetworkImage("https://xixi-web-tlias.oss-cn-guangzhou.aliyuncs.com/2.jpg"),fit: BoxFit.cover),),otherAccountsPictures: [Image.network("https://xixi-web-tlias.oss-cn-guangzhou.aliyuncs.com/3.jpg"),Image.network("https://xixi-web-tlias.oss-cn-guangzhou.aliyuncs.com/4.jpg"),Image.network("https://xixi-web-tlias.oss-cn-guangzhou.aliyuncs.com/5.jpg"),],),const ListTile(leading: CircleAvatar(child: Icon(Icons.people)),title: Text("個人中心"),),const Divider(),const ListTile(leading: CircleAvatar(child: Icon(Icons.settings)),title: Text("系統設置"),)],),
),

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/15582.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/15582.shtml
英文地址,請注明出處:http://en.pswp.cn/web/15582.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

AWS聯網和內容分發之VPC

Amazon Virtual Private Cloud&#xff08;VPC&#xff09;是一項用于在AWS云中創建一個邏輯隔離的虛擬網絡的服務&#xff0c;使用戶能夠在云中啟動AWS資源&#xff08;例如EC2實例&#xff09;&#xff0c;并將其放置在自己定義的虛擬網絡中。 Amazon VPC讓您能夠全面地控制…

AEE運行機制深入剖析——閱讀筆記

AEE運行機制深入剖析——閱讀筆記 在移動設備和嵌入式系統的開發中&#xff0c;應用執行環境&#xff08;Application Execution Environment&#xff0c;簡稱AEE&#xff09;起著至關重要的作用。AEE是操作系統層面的一個框架&#xff0c;負責管理應用程序的生命周期&#xf…

JVM性能調優:內存模型及垃圾收集算法

JVM內存結構 根據Java虛擬機規范&#xff0c;JVM內存主要劃分為以下區域&#xff1a; 年輕代&#xff08;New Generation&#xff09; 包括Eden空間&#xff0c;用于存放新創建的對象。Survivor區由兩個相同大小的Survivor1和Survivor2組成&#xff0c;用于存放經過初次垃圾回…

AI菜鳥向前飛 — LangChain系列之十四 - Agent系列:從現象看機制(上篇)

上一篇介紹了Agent與LangGraph的基礎技能Tool的必知必會 AI菜鳥向前飛 — LangChain系列之十三 - 關于Tool的必知必會 前面已經詳細介紹了Promp、RAG&#xff0c;終于來到Agent系列&#xff08;別急后面還有LangGraph&#xff09;&#xff0c;大家可以先看下這張圖&#xff1…

leetcode328. 奇偶鏈表,附詳細解析和代碼注釋

leetcode328. 奇偶鏈表 給定單鏈表的頭節點 head &#xff0c;將所有索引為奇數的節點和索引為偶數的節點分別組合在一起&#xff0c;然后返回重新排序的列表。 第一個節點的索引被認為是 奇數 &#xff0c; 第二個節點的索引為 偶數 &#xff0c;以此類推。 請注意&#xff0…

Java的反射機制詳解:動態操作類和對象

Java反射是一種強大的機制&#xff0c;允許程序在運行時查詢和操作類、方法、接口等。這種能力使得Java應用可以在運行時動態地創建對象、調用方法和訪問屬性&#xff0c;極大地提升了程序的靈活性和可擴展性。本文將深入探討Java反射的原理、核心API和實際應用場景&#xff0c…

Flutter 中的 CupertinoSlidingSegmentedControl 小部件:全面指南

Flutter 中的 CupertinoSlidingSegmentedControl 小部件&#xff1a;全面指南 在Flutter框架中&#xff0c;CupertinoSlidingSegmentedControl是一個用于創建類似iOS風格的滑動分段控制器的小部件。這種控制器通常用于允許用戶在不同的視圖或設置之間切換。本文將為您提供一個…

輕量級 K8S 環境 安裝minikube

文章目錄 操作系統DockerDocker CE 鏡像源站使用官方安裝腳本自動安裝 &#xff08;僅適用于公網環境&#xff09;安裝校驗Docker代理docker permission denied while trying to connect to the Docker daemon socket minikubekubectl工具minikube dashboard參考資料 操作系統 …

Docker進入容器查看內容并從容器里拷貝文件到宿主機

工作中需要從docker正在運行的鏡像中復制文件到宿主機&#xff0c;于是便將這個過程記錄了下來。 &#xff08;1&#xff09;查看正在運行的容器 通過以下命令&#xff0c;可以查看正在運行的容器&#xff1a; docker ps &#xff08;2&#xff09;進入某個容器執行腳本 我…

前端人員選擇組件封裝

功能&#xff1a; 人員選擇&#xff0c;返回人員參數&#xff0c;以及人員參數id數組支持單選&#xff0c;多選人員支持重新選擇回顯上次選中人員 <!-- 彈窗 --><a-modal v-model"modalVisible" :footer"null" :bodyStyle"{ padding: 0 }&q…

react中子傳父信息

思路是&#xff1a; 在父組件定義一個函數接受參數&#xff0c;接收的參數用于接收子組件的信息&#xff0c;把函數傳給子組件&#xff0c;子組件調用父親傳來的函數并把要告訴父親的話傳到函數中&#xff0c;就實現了子傳父消息 import { useState } from reactimport { use…

OpenWrt 安裝Quagga 支持ospf Bgp等動態路由協議 軟路由實測 系列四

1 Quagga 是一個路由軟件套件, 提供 OSPFv2,OSPFv3,RIP v1 和 v2,RIPng 和 BGP-4 的實現. 2 web 登錄安裝 #或者ssh登錄安裝 opkg install quagga quagga-zebra quagga-bgpd quagga-watchquagga quagga-vtysh # reboot 3 ssh 登錄 #重啟服務 /etc/init.d/quagga restart #…

使用kubesphere部署微服務的時候,節點的鏡像不是最新的導致部署到舊版本問題

我使用kubesphere部署微服務的時候&#xff0c;發現有很多次&#xff0c;我修改了配置文件&#xff0c;但是部署完才發現部署的是舊版本。 然后我查看了該微服務部署在哪個節點上&#xff1a; kubectl get pods --all-namespaces -o wide例如 gulimall-gateway 這個服務&…

韭菜的自我總結

韭菜的自我總結 股市技術面量價關系左側右側右側技術左側技術洗盤 韭菜的自我修養虛擬貨幣的啟示韭菜的買入時機韭菜的心理壓力成為優秀玩家的關鍵 股市技術面 技術面分析可以作為買賣時機判定的工具&#xff0c;但是投資還是需要基本面的分析作為支撐。也就是基本面選股&…

langchain進階一:特殊的chain,輕松實現對話,與數據庫操作,抽取數據,以及基于本地知識庫的問答

特殊的chain langchain中的Chain有很多,能夠輕松實現部分需求,極致簡化代碼,但是實現效果與模型智慧程度有關 會話鏈 效果與LLMChain大致相同 javascript 復制代碼 from langchain.chains import ConversationChain from langchain_community.llms import OpenAI conversat…

Spring Boot中如何實現定時任務?

在項目開發中&#xff0c;經常需要編寫定時任務來實現一些功能&#xff1a; 定時備份數據庫、定時發送郵件、定時清理數據、定時提醒或通知、信用卡每月還款提醒 未支付的訂單15分鐘之后自動取消、未確認收貨的訂單7天之后自動確認收貨 定時任務的實現&#xff1a; Spring T…

Redis 實戰 - 緩存異常及解決方案

文章目錄 概述一、緩存穿透1.1 緩存穿透是什么1.2 解決方案 二、緩存擊穿2.1 緩存擊穿是什么2.2 解決方案 三、緩存雪崩3.1 緩存雪崩是什么3.2 解決方案 四、拓展4.1 緩存預熱4.2 緩存降級 五、結語 把今天最好的表現當作明天最新的起點……&#xff0e;&#xff5e; 概述 在實…

YoloV8改進策略:Neck層改進、注意力改進|HCANet全局與局部的注意力模塊CAFM|二次創新|即插即用

yolov9-c summary: 620 layers, 52330674 parameters, 0 gradients, 245.5 GFLOPsClass Images Instances P R mAP50 mAP50-95: 100%|██████████| 15/15 00:06all 230 1412 0.917 0.985 0.99 0.735…

實現自動化巡檢多臺交換機并將輸出信息保存到文本文件中

為了實現自動化巡檢多臺交換機并將輸出信息保存到文本文件中,可以擴展之前的 SSHInspectionTool 類,使其能夠處理多臺交換機的連接和命令執行。我們可以使用多線程來并行處理多個 SSH 連接,以提高效率。 目錄 一、導入依賴包 二、編寫Java類 (1)SSH.java (2)SSHIns…

LeetCode 第131場雙周賽個人題解

100309. 求出出現兩次數字的 XOR 值 原題鏈接 求出出現兩次數字的 XOR 值 - 力扣 (LeetCode) 競賽 思路分析 簽到題&#xff0c;一次遍歷 AC代碼 class Solution:def duplicateNumbersXOR(self, nums: List[int]) -> int:cnt Counter(nums)res 0st set(nums)for x …