报错信息如下:
The following assertion was thrown building TweenAnimationBuilder(duration: 10000ms, dirty, state: _TweenAnimationBuilderState#7d4e7(ticker active)):
setState() or markNeedsBuild() called during build.
This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: Overlay-[LabeledGlobalKey#ae864]
state: OverlayState#f5c42(entries: [OverlayEntry#9d580(opaque: true; maintainState: false), OverlayEntry#5e034(opaque: false; maintainState: true), OverlayEntry#c03ca(opaque: false; maintainState: false), OverlayEntry#3459a(opaque: false; maintainState: true)])
The widget which was currently being built when the offending call was made was: TweenAnimationBuilder
代码如下:其中关键代码如下,需要使用 Future.delayed()方法来延迟一秒执行,如下面的代码中,方式一会导致出现问题,方式二则可以
if (value >= 1.0) {
Get.off(() => MainPage()); /// 方式一
Future.delayed(Duration(seconds: 1), () { /// 方式二
Get.off(() => MainPage());
});
}
具体代码如下:
bottomSheet: Container(
height: 15,
alignment: Alignment.bottomCenter,
margin: EdgeInsets.only(bottom: 50),
// color: Colors.black,
child: SizedBox(
width: 300,
height: 10,
child: TweenAnimationBuilder<double>(
tween: Tween(begin: 0.0, end: 1.0),
duration: Duration(seconds: 10),
builder: (context, value, child) {
if (value >= 1.0) {
Get.off(() => MainPage());
Future.delayed(Duration(seconds: 1), () {
Get.off(() => MainPage());
});
}
return GradientLinearProgressBar(
strokeCapRound: true,
strokeWidth: 10,
colors: [
HexColor(Colorss.progressSplashColor1),
HexColor(Colorss.progressSplashColor2)
],
backgroundColor: Colors.black12,
value: value,
);
},
),
),
),