在下面的这段代码中,我想要设置{@ {1}}的边框半径的动画效果,然后坚持到屏幕顶部,例如DraggableScrollableSheet
,为此实现了动画更改边框半径的效果,但是不起作用我得到这个错误:
在构建_BottomBarControllerScope时引发了以下断言: 'package:flutter / src / animation / animations.dart':失败的断言: 第376行pos 15:“ parent!= null”:不正确。
无论是断言 表示框架本身有错误,或者我们应该提供 此错误消息中的大量信息可以帮助您 确定并解决根本原因。无论哪种情况,请报告 通过在GitHub上提交错误来确定该断言:
https://github.com/flutter/flutter/issues/new?template=BUG.md 抛出异常,这是堆栈: 2个新的CurvedAnimation(package:flutter / src / animation / animations.dart:376:15) 3 _HomeState.initState(package:cheetah / screens / home / main / view / home.dart:45:7)
在该部分代码中,AppBar
是:home.dart:45:7
:
CurvedAnimation
我的代码:
borderRadius = BorderRadiusTween(
begin: BorderRadius.circular(75.0),
end: BorderRadius.circular(0.0),
).animate(
CurvedAnimation(
parent: _borderRadiusController,
curve: Curves.ease,
),
);
答案 0 :(得分:1)
这应该是正确的方法。
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
AnimationController _controller;
BorderRadiusTween borderRadius;
Duration _duration = Duration(milliseconds: 500);
Tween<Offset> _tween = Tween(begin: Offset(0, 1), end: Offset(0, 0));
double _height, min = 0.1, initial = 0.3, max = 0.7;
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this, duration: _duration);
borderRadius = BorderRadiusTween(
begin: BorderRadius.circular(75.0),
end: BorderRadius.circular(0.0),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: GestureDetector(
child: FloatingActionButton(
child: AnimatedIcon(icon: AnimatedIcons.menu_close, progress: _controller),
elevation: 5,
backgroundColor: Colors.deepOrange,
foregroundColor: Colors.white,
onPressed: () async {
if (_controller.isDismissed)
_controller.forward();
else if (_controller.isCompleted) _controller.reverse();
},
),
),
body: SizedBox.expand(
child: Stack(
children: <Widget>[
FlutterLogo(size: 500),
SizedBox.expand(
child: SlideTransition(
position: _tween.animate(_controller),
child: DraggableScrollableSheet(
minChildSize: min, // 0.1 times of available height, sheet can't go below this on dragging
maxChildSize: max, // 0.7 times of available height, sheet can't go above this on dragging
initialChildSize: initial, // 0.1 times of available height, sheet start at this size when opened for first time
builder: (BuildContext context, ScrollController controller) {
return AnimatedBuilder(
animation: controller,
builder: (context, child) {
return ClipRRect(
borderRadius: borderRadius.evaluate(CurvedAnimation(parent: _controller, curve: Curves.ease)),
child: Container(
height: 500.0,
color: Colors.blue[800],
child: ListView.builder(
controller: controller,
itemCount: 5,
itemBuilder: (BuildContext context, int index) {
return ListTile(title: Text('Item $index'));
},
),
),
);
},
);
},
),
),
),
],
),
),
);
}
}