Flutter 覆盖自定义颤振小部件
我有一个简短的问题。我想通过添加另一行来覆盖 (在前导、标题/副标题、尾随下方)。那么,我是否必须将这个小部件的整个实现复制到我的自定义小部件中,然后添加这一新行?或者我可以用这一行覆盖它吗 基础一:Flutter 覆盖自定义颤振小部件,flutter,Flutter,我有一个简短的问题。我想通过添加另一行来覆盖 (在前导、标题/副标题、尾随下方)。那么,我是否必须将这个小部件的整个实现复制到我的自定义小部件中,然后添加这一新行?或者我可以用这一行覆盖它吗 基础一: import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; const Duration _kExpand = Duration(milliseconds: 200); class Expansi
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
const Duration _kExpand = Duration(milliseconds: 200);
class ExpansionTile extends StatefulWidget {
const ExpansionTile({
Key key,
this.leading,
@required this.title,
this.subtitle,
this.backgroundColor,
this.onExpansionChanged,
this.children = const <Widget>[],
this.trailing,
this.initiallyExpanded = false,
this.maintainState = false,
this.tilePadding,
this.expandedCrossAxisAlignment,
this.expandedAlignment,
this.childrenPadding,
}) : assert(initiallyExpanded != null),
assert(maintainState != null),
assert(
expandedCrossAxisAlignment != CrossAxisAlignment.baseline,
'CrossAxisAlignment.baseline is not supported since the expanded children '
'are aligned in a column, not a row. Try to use another constant.',
),
super(key: key);
final Widget leading;
final Widget title;
final Widget subtitle;
final ValueChanged<bool> onExpansionChanged;
final List<Widget> children;
final Color backgroundColor;
final Widget trailing;
final bool initiallyExpanded;
final bool maintainState;
final EdgeInsetsGeometry tilePadding;
final Alignment expandedAlignment;
final CrossAxisAlignment expandedCrossAxisAlignment;
final EdgeInsetsGeometry childrenPadding;
@override
_ExpansionTileState createState() => _ExpansionTileState();
}
class _ExpansionTileState extends State<ExpansionTile>
with SingleTickerProviderStateMixin {
static final Animatable<double> _easeOutTween =
CurveTween(curve: Curves.easeOut);
static final Animatable<double> _easeInTween =
CurveTween(curve: Curves.easeIn);
static final Animatable<double> _halfTween =
Tween<double>(begin: 0.0, end: 0.5);
final ColorTween _borderColorTween = ColorTween();
final ColorTween _headerColorTween = ColorTween();
final ColorTween _iconColorTween = ColorTween();
final ColorTween _backgroundColorTween = ColorTween();
AnimationController _controller;
Animation<double> _iconTurns;
Animation<double> _heightFactor;
Animation<Color> _borderColor;
Animation<Color> _headerColor;
Animation<Color> _iconColor;
Animation<Color> _backgroundColor;
bool _isExpanded = false;
@override
void initState() {
super.initState();
_controller = AnimationController(duration: _kExpand, vsync: this);
_heightFactor = _controller.drive(_easeInTween);
_iconTurns = _controller.drive(_halfTween.chain(_easeInTween));
_borderColor = _controller.drive(_borderColorTween.chain(_easeOutTween));
_headerColor = _controller.drive(_headerColorTween.chain(_easeInTween));
_iconColor = _controller.drive(_iconColorTween.chain(_easeInTween));
_backgroundColor =
_controller.drive(_backgroundColorTween.chain(_easeOutTween));
_isExpanded = PageStorage.of(context)?.readState(context) as bool ??
widget.initiallyExpanded;
if (_isExpanded) _controller.value = 1.0;
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _handleTap() {
setState(() {
_isExpanded = !_isExpanded;
if (_isExpanded) {
_controller.forward();
} else {
_controller.reverse().then<void>((void value) {
if (!mounted) return;
setState(() {
// Rebuild without widget.children.
});
});
}
PageStorage.of(context)?.writeState(context, _isExpanded);
});
if (widget.onExpansionChanged != null)
widget.onExpansionChanged(_isExpanded);
}
Widget _buildChildren(BuildContext context, Widget child) {
final Color borderSideColor = _borderColor.value ?? Colors.transparent;
return Container(
decoration: BoxDecoration(
color: _backgroundColor.value ?? Colors.transparent,
border: Border(
top: BorderSide(color: borderSideColor),
bottom: BorderSide(color: borderSideColor),
),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTileTheme.merge(
iconColor: _iconColor.value,
textColor: _headerColor.value,
child: ListTile(
onTap: _handleTap,
contentPadding: widget.tilePadding,
leading: widget.leading,
title: widget.title,
subtitle: widget.subtitle,
trailing: widget.trailing ??
RotationTransition(
turns: _iconTurns,
child: const Icon(Icons.expand_more),
),
),
),
ClipRect(
child: Align(
alignment: widget.expandedAlignment ?? Alignment.center,
heightFactor: _heightFactor.value,
child: child,
),
),
],
),
);
}
@override
void didChangeDependencies() {
final ThemeData theme = Theme.of(context);
_borderColorTween.end = theme.dividerColor;
_headerColorTween
..begin = theme.textTheme.subtitle1.color
..end = theme.accentColor;
_iconColorTween
..begin = theme.unselectedWidgetColor
..end = theme.accentColor;
_backgroundColorTween.end = widget.backgroundColor;
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
final bool closed = !_isExpanded && _controller.isDismissed;
final bool shouldRemoveChildren = closed && !widget.maintainState;
final Widget result = Offstage(
child: TickerMode(
child: Padding(
padding: widget.childrenPadding ?? EdgeInsets.zero,
child: Column(
crossAxisAlignment: widget.expandedCrossAxisAlignment ??
CrossAxisAlignment.center,
children: widget.children,
),
),
enabled: !closed,
),
offstage: closed);
return AnimatedBuilder(
animation: _controller.view,
builder: _buildChildren,
child: shouldRemoveChildren ? null : result,
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:flatter/widgets.dart”;
const Duration _kExpand=持续时间(毫秒:200);
类ExpansionFile扩展StatefulWidget{
常量扩展文件({
关键点,
这个,领导,,
@需要这个标题,
这个,副标题,,
这个背景色,
这个,一个扩展改变了,
this.children=const[],
这个,拖尾,,
this.initiallyExpanded=false,
this.maintaintState=false,
这是tilePadding,
这个.expandedCrossAxisAlignment,
这个,扩大的联盟,
这是一个孩子,
}):assert(initiallyExpanded!=null),
断言(maintaintState!=null),
断言(
expandedCrossAxisAlignment!=CrossAxisAlignment.baseline,
“CrossAxisAlignment.baseline不受支持,因为扩展的子项”
'在列而不是行中对齐。请尝试使用另一个常量。',
),
超级(键:键);
最终引导;
最终小部件标题;
最终小部件字幕;
最终价值因扩张而改变;
最后儿童名单;
最终颜色背景色;
最终窗口小部件跟踪;
最终bool初始扩展;
最终布尔状态;
最终边缘几何平铺;
最终定线扩大定线;
最终横轴对齐扩展横轴对齐;
最终边插入几何子级填充;
@凌驾
_ExpansionTileState createState()=>\u ExpansionTileState();
}
类扩展文件状态扩展状态
使用SingleTickerProviderStateMixin{
静态最终可设置动画_easeoutween=
曲线之间(曲线:Curves.easeOut);
静态最终可设置动画_easeInTween=
曲线之间(曲线:Curves.easeIn);
静态最终可设置动画_halfTween=
吐温(开始:0.0,结束:0.5);
最终ColorTween_borderColorTween=ColorTween();
最终色差_headerColorTween=色差();
最终色差_iconColorTween=色差();
最终ColorTween_backgroundColorTween=ColorTween();
动画控制器_控制器;
动画_iconTurns;
动画高度因子;
动画色彩;
动画(头彩),;
动画(iconColor),;
动画背景色;
bool _isExpanded=false;
@凌驾
void initState(){
super.initState();
_控制器=动画控制器(持续时间:_kExpand,vsync:this);
_高度系数=_controller.drive(_easeInTween);
_iconTurns=_controller.drive(_halfTween.chain(_easeInTween));
_borderColor=_controller.drive(_borderColorTween.chain(_easeoutween));
_headerColor=_控制器.drive(_headerColorTween.chain(_easeInTween));
_iconColor=\u controller.drive(\u iconColorTween.chain(\u easeInTween));
_背景色=
_控制器驱动器(_backgroundColorTween.chain(_easeoutween));
_isExpanded=PageStorage.of(上下文)?.readState(上下文)为bool??
widget.initiallyExpanded;
如果(_isExpanded)_controller.value=1.0;
}
@凌驾
无效处置(){
_controller.dispose();
super.dispose();
}
void_handleTap(){
设置状态(){
_isExpanded=!\u isExpanded;
如果(_i扩展){
_controller.forward();
}否则{
_controller.reverse().then((无效值){
如果(!已安装)返回;
设置状态(){
//在没有widget.children的情况下重建。
});
});
}
PageStorage.of(context)?.writeState(context,_isExpanded);
});
if(widget.onExpansionChanged!=null)
widget.onExpansionChanged(_isexpansed);
}
Widget\u buildChildren(BuildContext,Widget子对象){
最终颜色borderSideColor=\u borderColor.value??Colors.transparent;
返回容器(
装饰:盒子装饰(
颜色:_backgroundColor.value??Colors.transparent,
边界:边界(
顶部:BorderSide(颜色:borderSideColor),
底部:BorderSide(颜色:borderSideColor),
),
),
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
ListTileTheme.merge(
iconColor:_iconColor.value,
textColor:_headerColor.value,
孩子:ListTile(
onTap:_handleTap,
contentPadding:widget.tilePadding,
引导:widget.leading,
标题:widget.title,
字幕:widget.subtitle,
尾部:widget.training??
旋转跃迁(
转弯处:_i转弯处,
子:常量图标(图标。展开更多),
),
),
),
克里普雷克特(
子对象:对齐(
对齐:widget.expandedAlignment??alignment.center,
高度因子:_heightFactor.value,
孩子:孩子,
),
),
],
),
);
}
@凌驾
void didChangeDependencies(){
最终主题数据主题=theme.of(上下文);
_borderColorTween.end=theme.dividerColor;
_头彩
…begin=theme.textTheme.subtitle1.color
..end=theme.accentColor;
_iconColorTween
…begin=theme.UNSELECTED WidgetColor
..end=theme.accentColor;
_backgroundColorTween.end=widget.backgroundColor;
super.didChangeDependencies();
}
@凌驾
小部件构建(构建上下文){
最终bool closed=!\u isExpanded和&u controller.ismissed;
最终bool shouldRemoveChildren=closed&&!widget.maintaintState;
最终窗口小部件结果=后台(
孩子:TickerMode(
孩子:填充(
填充:widget.childrenPadding??EdgeInsets.zero,
子:列(
crossAxisAlignment:widget.expandedCrossAxis
Column(
children: [
ListTile(
leading: Icon(Icons.circle),
title: Text('Title'),
subtitle: Text('subtitle'),
trailing: Icon(Icons.more_vert),
visualDensity: VisualDensity.compact,
),
Placeholder(fallbackHeight: 10.0) //place your widget/row here
],
)