我一直想知道這種視覺AnimatedContainer非常靈活。讓我感到困惑的是,如果我們可以將 aContainer設定為小于實際內容的影片,我們如何告訴它回到默認的內容高度?這是內容的高度取決于其中的內容。
我現在沒有真正的用例,但我可以看到它正在使用,例如,如果容器內部有一個“ListView”(因此不顯示偏移錯誤)。
我嘗試使用null,但顯然它不能為空。
double height = 100;
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () {
setState(() {
height = height == 100 ? null : 100;
});
},
child: Center(
child: AnimatedContainer(
curve: Curves.bounceOut,
duration: const Duration(milliseconds: 500),
width: 100,
height: height,
color: Colors.deepPurple,
child: Text('This text set to be much larger than 100 aaaaaaaaaa aaaaaaaa aaaaaaaaa aaaaaaaa.',
style: TextStyle(
fontSize: 30,
color: Colors.white,
)
)
),
),
)
);
}
錯誤
Error: A value of type 'double?' can't be assigned to a variable of type 'double' because 'double?' is nullable and 'double' isn't.
height = height == 100 ? null : 100;
我試圖通過隱式影片來解決這個問題,但我猜答案是明確的。希望聽聽大家的想法。
編輯:
此處未使用,但同樣的問題適用于使用Tween<double>(begin: 100, end: null).animate(_somecontroller);wherenull不是有效引數。
uj5u.com熱心網友回復:
當您將其定義為時傳遞null給變數,將變數更改為:heightnon-nullable
double? height = 100;
但是您不能使用boundedand unboundedheight for AnimatedContainer,這是您可以做的完整示例:
class TestAnimation extends StatefulWidget {
const TestAnimation({Key? key}) : super(key: key);
@override
State<TestAnimation> createState() => _TestAnimationState();
}
class _TestAnimationState extends State<TestAnimation> {
double? height;
GlobalKey textKey = GlobalKey();
double textHeight = 0.0;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
final textKeyContext = textKey.currentContext;
if (textKeyContext != null) {
final box = textKeyContext.findRenderObject() as RenderBox;
textHeight = box.size.height;
height = textHeight;
}
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onTap: () {
setState(() {
height = height == 100 ? textHeight : 100;
});
},
child: Center(
child: AnimatedContainer(
constraints: const BoxConstraints(
maxHeight: 1000,
),
curve: Curves.bounceOut,
duration: const Duration(milliseconds: 500),
width: 100,
height: height,
color: Colors.deepPurple,
child: Text(
'This text set to be much larger than 100 aaaaaaaaaa aaaaaaaa aaaaaaaaa aaaaaaaa.',
key: textKey,
style: TextStyle(
fontSize: 30,
color: Colors.white,
),
),
),
),
));
}
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533182.html
標籤:扑镖颤动动画飞镖零安全
