06_Flutter自定义锚点分类列表

2024-05-01 01:44

本文主要是介绍06_Flutter自定义锚点分类列表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

06_Flutter自定义锚点分类列表

在这里插入图片描述

这样的效果,大家在一些商超应用里,应该也看到过。接下来咱们就用Flutter一步一步的来实现。

一.自定义属性抽取

在这里插入图片描述

  • categoryWidth: 左侧边栏的宽度,右侧区域的宽度填充剩余空间即可。
  • itemCount: 总共有多少个分类项,也就是左侧边栏中有多少个字项。
  • sticky: 滑动过程中,右侧标题是否吸顶。
  • controller: 外部通过controller可以控制左侧边栏中子项的选中以及右侧列表滑动位置的联动,同时监听选中状态。
  • categoryItemBuilder: 创建左侧边栏中的每一个分类项。
  • sectionItemBuilder: 创建右侧滑动列表中的每一个标题项。
  • sectionOfChildrenBuilder: 创建右侧滑动列表中的每一个标题项对应的子列表
class AnchorCategoryController extends ChangeNotifier {int selectedIndex = 0;void selectTo(int value) {selectedIndex = value;notifyListeners();}void dispose() {selectedIndex = 0;super.dispose();}
}class _HomePageState extends State<HomePage> {final List<String> _sections = ["标题1", "标题2", "标题3", "标题4", "标题5", "标题6", "标题7", "标题8", "标题9", "标题10"];final List<List<String>> _childrenList = [["item1", "item2", "item3", "item4", "item5"],["item1", "item2", "item3"],["item1", "item2", "item3", "item4"],["item1"],["item1", "item2"],["item1", "item2", "item3", "item4", "item5", "item6"],["item1", "item2", "item3", "item4"],["item1", "item2", "item3", "item4", "item5"],["item1", "item2", "item3"],["item1", "item2", "item3", "item4", "item5"]];int _selectedSectionsIndex = 0;final AnchorCategoryController _controller = AnchorCategoryController();void initState() {super.initState();_controller.addListener(_onCategoryChanged);}void _onCategoryChanged() {setState(() {_selectedSectionsIndex = _controller.selectedIndex;});}void dispose() {_controller.removeListener(_onCategoryChanged);_controller.dispose();super.dispose();}Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text(widget.title),),body: SafeArea(child: AnchorCategoryList(controller: _controller,itemCount: _sections.length,sticky: true,categoryItemBuilder: (BuildContext context, int index) {return AlphaButton(onTap: () {_controller.selectTo(index);},child: Container(padding: const EdgeInsets.all(10),color: _selectedSectionsIndex == index ? const Color(0xFFFFFFFF): const Color(0xFFF2F2F2),child: Text(_sections[index]),),);},sectionItemBuilder: (BuildContext context, int index) {return Container(padding: const EdgeInsets.symmetric(vertical: 10),alignment: Alignment.centerLeft,color: const Color(0xFFF2F2F2),child: Text(_sections[index]),);},sectionOfChildrenBuilder: (BuildContext context, int index) {return List<Widget>.generate(_childrenList[index].length, (childIndex) {return Container(padding: const EdgeInsets.symmetric(vertical: 10),alignment: Alignment.centerLeft,child: Text(_childrenList[index][childIndex]),);});},)));}
}
二.组件基本布局
class AnchorCategoryList extends StatefulWidget {final double categoryWidth;final int itemCount;final IndexedWidgetBuilder categoryItemBuilder;final IndexedWidgetBuilder sectionItemBuilder;final IndexedWidgetListBuilder sectionOfChildrenBuilder;final bool sticky;final AnchorCategoryController? controller;const AnchorCategoryList({super.key,required this.categoryItemBuilder,required this.sectionItemBuilder,required this.sectionOfChildrenBuilder,this.controller,double? categoryWidth,int? itemCount,bool? sticky}): categoryWidth = categoryWidth ?? 112,itemCount = itemCount ?? 0,sticky = sticky ?? true;State<StatefulWidget> createState() => _AnchorCategoryListState();}class _AnchorCategoryListState extends State<AnchorCategoryList> {Widget build(BuildContext context) {return Row(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: [SizedBox(width: widget.categoryWidth,child: LayoutBuilder(builder: (context, viewportConstraints) {return SingleChildScrollView(child: ConstrainedBox(constraints: BoxConstraints(minHeight: viewportConstraints.maxHeight != double.infinity ? viewportConstraints.maxHeight:0),child: Column(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: List.generate(widget.itemCount, (index) {return widget.categoryItemBuilder.call(context, index);}),),),);},)),Expanded(child: CustomScrollView(physics: const ClampingScrollPhysics(),slivers: [...(List<Widget>.generate(widget.itemCount * 2, (allIndex) {int index = allIndex ~/ 2;if(allIndex.isEven) {//sectionreturn SliverToBoxAdapter(child: widget.sectionItemBuilder.call(context, index),);} else {//childrenreturn SliverToBoxAdapter(child: Column(children: widget.sectionOfChildrenBuilder.call(context, index),),);}})),]))],);}}
三.获取并保存标题项、标题项对应子列表的高度

这里获取标题项、标题项对应子列表的高度,需要等到控件build完成后,才能获取到,因此需要自定义一个控件继承SingleChildRenderObjectWidget,并指定一个自定义的RenderBox,在performLayout中通过回调通知外部,控件layout完成了

typedef AfterLayoutCallback = Function(RenderBox ral);class AfterLayout extends SingleChildRenderObjectWidget {final AfterLayoutCallback callback;const AfterLayout({Key? key,required this.callback,Widget? child,}) : super(key: key, child: child);RenderObject createRenderObject(BuildContext context) {return RenderAfterLayout(callback);}void updateRenderObject(context, RenderAfterLayout renderObject) {renderObject.callback = callback;}
}class RenderAfterLayout extends RenderProxyBox {AfterLayoutCallback callback;RenderAfterLayout(this.callback);void performLayout() {super.performLayout();SchedulerBinding.instance.addPostFrameCallback((timeStamp) => callback(this));}}

使用AfterLayout获取并保存标题项、标题项对应子列表的高度


Widget build(BuildContext context) {return Row(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: [SizedBox(width: widget.categoryWidth,child: LayoutBuilder(builder: (context, viewportConstraints) {return SingleChildScrollView(child: ConstrainedBox(constraints: BoxConstraints(minHeight: viewportConstraints.maxHeight != double.infinity ? viewportConstraints.maxHeight:0),child: Column(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: List.generate(widget.itemCount, (index) {return widget.categoryItemBuilder.call(context, index);}),),),);},)),Expanded(child: CustomScrollView(physics: const ClampingScrollPhysics(),slivers: [...(List<Widget>.generate(widget.itemCount * 2, (allIndex) {int index = allIndex ~/ 2;if(allIndex.isEven) {//sectionreturn SliverToBoxAdapter(child: AfterLayout(callback: (renderBox) {double height = renderBox.size.height;setState(() {if(_sectionHeightList.length > index) {_sectionHeightList[index] = height;} else {_sectionHeightList.add(height);}});},child: widget.sectionItemBuilder.call(context, index),),);} else {//childrenreturn SliverToBoxAdapter(child: AfterLayout(callback: (renderBox) {double height = renderBox.size.height;setState(() {if(_childrenHeightList.length > index) {_childrenHeightList[index] = height;} else {_childrenHeightList.add(height);}});},child: Column(children: widget.sectionOfChildrenBuilder.call(context, index),),),);}})),]))],);
}

计算并保存右侧面板每一项选中时的初始滑动偏移量

在这里插入图片描述


Widget build(BuildContext context) {return Row(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: [SizedBox(width: widget.categoryWidth,child: LayoutBuilder(builder: (context, viewportConstraints) {return SingleChildScrollView(child: ConstrainedBox(constraints: BoxConstraints(minHeight: viewportConstraints.maxHeight != double.infinity ? viewportConstraints.maxHeight:0),child: Column(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: List.generate(widget.itemCount, (index) {return widget.categoryItemBuilder.call(context, index);}),),),);},)),Expanded(child: AfterLayout(callback: (renderBox) {setState(() {for(int i = 0; i < widget.itemCount; i ++) {double scrollOffset = 0;for(int j=0; j<i; j++) {scrollOffset += _sectionHeightList[j] + _childrenHeightList[j];}if(_scrollOffsetList.length > i) {_scrollOffsetList[i] = scrollOffset;} else {_scrollOffsetList.add(scrollOffset);}}debugPrint("CustomScrollView AfterLayout: $_scrollOffsetList");});},child: CustomScrollView(physics: const ClampingScrollPhysics(),slivers: [...(List<Widget>.generate(widget.itemCount * 2, (allIndex) {int index = allIndex ~/ 2;if(allIndex.isEven) {//sectionreturn SliverToBoxAdapter(child: AfterLayout(callback: (renderBox) {double height = renderBox.size.height;setState(() {if(_sectionHeightList.length > index) {_sectionHeightList[index] = height;} else {_sectionHeightList.add(height);}});},child: widget.sectionItemBuilder.call(context, index),),);} else {//childrenreturn SliverToBoxAdapter(child: AfterLayout(callback: (renderBox) {double height = renderBox.size.height;setState(() {if(_childrenHeightList.length > index) {_childrenHeightList[index] = height;} else {_childrenHeightList.add(height);}});},child: Column(children: widget.sectionOfChildrenBuilder.call(context, index),),),);}})),]),))],);
}
四.点击选中分类项时,右侧自动滑动至相应位置

首先,这里需要把右侧列表最后一项的高度设置为ViewPort的高度,保证最后能够滑动到最后一项。只需要在右侧列表添加一个空白区域即可。


Widget build(BuildContext context) {return Row(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: [...,Expanded(child: AfterLayout(callback: (renderBox) {setState(() {...if(widget.itemCount > 0) {_extraHeight = max(renderBox.size.height - _childrenHeightList[widget.itemCount - 1], 0);} else {_extraHeight = 0;}});},child: CustomScrollView(physics: const ClampingScrollPhysics(),slivers: [...,SliverToBoxAdapter(child: SizedBox(height: _extraHeight,),)]),))],);
}

根据前面确定好初始的滑动偏移量之后,就能很方便的控制右侧列表的滑动了,我们通过给右侧列表指定ScrollController,同时调用ScrollController的animateTo(double offset, {required Duration duration, required Curve curve})方法即可。

class _AnchorCategoryListState extends State<AnchorCategoryList> {...final ScrollController _scrollController = ScrollController();int _selectedIndex = 0;bool _scrollLocked = false;void initState() {super.initState();if(widget.controller != null) {widget.controller!.addListener(_onIndexChange);}}void _onIndexChange() {if(_selectedIndex == widget.controller!.selectedIndex) {return;}_scrollLocked = true;_selectedIndex = widget.controller!.selectedIndex;widget.controller!.selectTo(_selectedIndex);_scrollController.animateTo(_scrollOffsetList[widget.controller!.selectedIndex],duration: const Duration(milliseconds: 300),curve: Curves.linear).then((value) {_scrollLocked = false;});}void dispose() {_scrollController.dispose();if(widget.controller != null) {widget.controller!.removeListener(_onIndexChange);}super.dispose();}Widget build(BuildContext context) {return Row(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: [...,Expanded(child: AfterLayout(callback: (renderBox) {setState(() {for(int i = 0; i < widget.itemCount; i ++) {double scrollOffset = 0;for(int j=0; j<i; j++) {scrollOffset += _sectionHeightList[j] + _childrenHeightList[j];}if(_scrollOffsetList.length > i) {_scrollOffsetList[i] = scrollOffset;} else {_scrollOffsetList.add(scrollOffset);}}if(widget.itemCount > 0) {_extraHeight = max(renderBox.size.height - _childrenHeightList[widget.itemCount - 1], 0);} else {_extraHeight = 0;}});},child: CustomScrollView(physics: const ClampingScrollPhysics(),controller: _scrollController,slivers: [...]),))],);}}

在这里插入图片描述

五.右侧列表滚动时,动态改变左侧边栏的选中状态

监听右侧列表的滑动,获取滑动位置,与所有子项的初始滑动偏移量对比,可以计算出左侧边栏的哪一个子项应该被选中,然后通过AnchorCategoryController的selectTo(int value)方法更新选中状态即可。

class _AnchorCategoryListState extends State<AnchorCategoryList> {...void initState() {super.initState();if(widget.controller != null) {widget.controller!.addListener(_onIndexChange);}_scrollController.addListener(_onScrollChange);}...void _onScrollChange() {if(_scrollLocked) {return;}double scrollOffset = _scrollController.offset;int selectedIndex = 0;for(int index = _scrollOffsetList.length - 1; index >= 0; index --) {selectedIndex = index;if(scrollOffset.roundToDouble() >= _scrollOffsetList[index]) {break;}}if(_selectedIndex != selectedIndex) {_selectedIndex = selectedIndex;widget.controller!.selectTo(selectedIndex);}}void dispose() {_scrollController.removeListener(_onScrollChange);_scrollController.dispose();if(widget.controller != null) {widget.controller!.removeListener(_onIndexChange);}super.dispose();}...}
六.控制标题项吸顶

将标题项的SliverToBoxAdapter替换成StickySliverToBoxAdapter即可,关于StickySliverToBoxAdapter可以查看这篇文章02_Flutter自定义Sliver组件实现分组列表吸顶效果。

class _AnchorCategoryListState extends State<AnchorCategoryList> {...Widget build(BuildContext context) {return Row(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: [...,Expanded(child: AfterLayout(callback: (renderBox) {...},child: CustomScrollView(physics: const ClampingScrollPhysics(),controller: _scrollController,slivers: [...(List<Widget>.generate(widget.itemCount * 2, (allIndex) {int index = allIndex ~/ 2;if(allIndex.isEven) {//sectionWidget sectionItem = AfterLayout(callback: (renderBox) {double height = renderBox.size.height;setState(() {if(_sectionHeightList.length > index) {_sectionHeightList[index] = height;} else {_sectionHeightList.add(height);}});},child: widget.sectionItemBuilder.call(context, index),);if(widget.sticky) {return StickySliverToBoxAdapter(child: sectionItem,);} else {return SliverToBoxAdapter(child: sectionItem,);}} else {//children...}})),...]),))],);}}

在这里插入图片描述

搞定,模拟器录屏掉帧了,改用真机录屏😄。

七.完整代码
typedef IndexedWidgetListBuilder = List<Widget> Function(BuildContext context, int index);class AnchorCategoryController extends ChangeNotifier {int selectedIndex = 0;void selectTo(int value) {selectedIndex = value;notifyListeners();}void dispose() {selectedIndex = 0;super.dispose();}
}class AnchorCategoryList extends StatefulWidget {final double categoryWidth;final int itemCount;final IndexedWidgetBuilder categoryItemBuilder;final IndexedWidgetBuilder sectionItemBuilder;final IndexedWidgetListBuilder sectionOfChildrenBuilder;final bool sticky;final AnchorCategoryController? controller;const AnchorCategoryList({super.key,required this.categoryItemBuilder,required this.sectionItemBuilder,required this.sectionOfChildrenBuilder,this.controller,double? categoryWidth,int? itemCount,bool? sticky}): categoryWidth = categoryWidth ?? 112,itemCount = itemCount ?? 0,sticky = sticky ?? true;State<StatefulWidget> createState() => _AnchorCategoryListState();}class _AnchorCategoryListState extends State<AnchorCategoryList> {final List<double> _sectionHeightList = [];final List<double> _childrenHeightList = [];final List<double> _scrollOffsetList = [];double _extraHeight = 0;final ScrollController _scrollController = ScrollController();int _selectedIndex = 0;bool _scrollLocked = false;void initState() {super.initState();if(widget.controller != null) {widget.controller!.addListener(_onIndexChange);}_scrollController.addListener(_onScrollChange);}void _onIndexChange() {if(_selectedIndex == widget.controller!.selectedIndex) {return;}_scrollLocked = true;_selectedIndex = widget.controller!.selectedIndex;widget.controller!.selectTo(_selectedIndex);_scrollController.animateTo(_scrollOffsetList[widget.controller!.selectedIndex],duration: const Duration(milliseconds: 300),curve: Curves.linear).then((value) {_scrollLocked = false;});}void _onScrollChange() {if(_scrollLocked) {return;}double scrollOffset = _scrollController.offset;int selectedIndex = 0;for(int index = _scrollOffsetList.length - 1; index >= 0; index --) {selectedIndex = index;if(scrollOffset.roundToDouble() >= _scrollOffsetList[index]) {break;}}if(_selectedIndex != selectedIndex) {_selectedIndex = selectedIndex;widget.controller!.selectTo(selectedIndex);}}void dispose() {_scrollController.removeListener(_onScrollChange);_scrollController.dispose();if(widget.controller != null) {widget.controller!.removeListener(_onIndexChange);}super.dispose();}Widget build(BuildContext context) {return Row(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: [SizedBox(width: widget.categoryWidth,child: LayoutBuilder(builder: (context, viewportConstraints) {return SingleChildScrollView(child: ConstrainedBox(constraints: BoxConstraints(minHeight: viewportConstraints.maxHeight != double.infinity ? viewportConstraints.maxHeight:0),child: Column(mainAxisSize: MainAxisSize.max,mainAxisAlignment: MainAxisAlignment.start,crossAxisAlignment: CrossAxisAlignment.stretch,children: List.generate(widget.itemCount, (index) {return widget.categoryItemBuilder.call(context, index);}),),),);},)),Expanded(child: AfterLayout(callback: (renderBox) {setState(() {for(int i = 0; i < widget.itemCount; i ++) {double scrollOffset = 0;for(int j=0; j<i; j++) {scrollOffset += _sectionHeightList[j] + _childrenHeightList[j];}if(_scrollOffsetList.length > i) {_scrollOffsetList[i] = scrollOffset;} else {_scrollOffsetList.add(scrollOffset);}}if(widget.itemCount > 0) {_extraHeight = max(renderBox.size.height - _childrenHeightList[widget.itemCount - 1], 0);} else {_extraHeight = 0;}});},child: CustomScrollView(physics: const ClampingScrollPhysics(),controller: _scrollController,slivers: [...(List<Widget>.generate(widget.itemCount * 2, (allIndex) {int index = allIndex ~/ 2;if(allIndex.isEven) {//sectionWidget sectionItem = AfterLayout(callback: (renderBox) {double height = renderBox.size.height;setState(() {if(_sectionHeightList.length > index) {_sectionHeightList[index] = height;} else {_sectionHeightList.add(height);}});},child: widget.sectionItemBuilder.call(context, index),);if(widget.sticky) {return StickySliverToBoxAdapter(child: sectionItem,);} else {return SliverToBoxAdapter(child: sectionItem,);}} else {//childrenreturn SliverToBoxAdapter(child: AfterLayout(callback: (renderBox) {double height = renderBox.size.height;setState(() {if(_childrenHeightList.length > index) {_childrenHeightList[index] = height;} else {_childrenHeightList.add(height);}});},child: Column(children: widget.sectionOfChildrenBuilder.call(context, index),),),);}})),SliverToBoxAdapter(child: SizedBox(height: _extraHeight,),)]),))],);}}

这篇关于06_Flutter自定义锚点分类列表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/950434

相关文章

Python 常用数据类型详解之字符串、列表、字典操作方法

《Python常用数据类型详解之字符串、列表、字典操作方法》在Python中,字符串、列表和字典是最常用的数据类型,它们在数据处理、程序设计和算法实现中扮演着重要角色,接下来通过本文给大家介绍这三种... 目录一、字符串(String)(一)创建字符串(二)字符串操作1. 字符串连接2. 字符串重复3. 字

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

SpringBoot AspectJ切面配合自定义注解实现权限校验的示例详解

《SpringBootAspectJ切面配合自定义注解实现权限校验的示例详解》本文章介绍了如何通过创建自定义的权限校验注解,配合AspectJ切面拦截注解实现权限校验,本文结合实例代码给大家介绍的非... 目录1. 创建权限校验注解2. 创建ASPectJ切面拦截注解校验权限3. 用法示例A. 参考文章本文

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Python进阶之列表推导式的10个核心技巧

《Python进阶之列表推导式的10个核心技巧》在Python编程中,列表推导式(ListComprehension)是提升代码效率的瑞士军刀,本文将通过真实场景案例,揭示列表推导式的进阶用法,希望对... 目录一、基础语法重构:理解推导式的底层逻辑二、嵌套循环:破解多维数据处理难题三、条件表达式:实现分支

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

把Python列表中的元素移动到开头的三种方法

《把Python列表中的元素移动到开头的三种方法》在Python编程中,我们经常需要对列表(list)进行操作,有时,我们希望将列表中的某个元素移动到最前面,使其成为第一项,本文给大家介绍了把Pyth... 目录一、查找删除插入法1. 找到元素的索引2. 移除元素3. 插入到列表开头二、使用列表切片(Lis