我正在寻找创建两个相似的小部件,因此应该共享一些代码。
我制作了一个看起来像这样的mixin
import 'package:flutter/material.dart';
mixin WidgetShared <T extends StatefulWidget> on State<T>, TickerProviderStateMixin<T>{
AnimationController controlsAnimationController;
Animation<double> controlsAnimation;
bool controlsOpen = false;
@override
void initState() {
super.initState();
print("This was the init");
controlsAnimationController = AnimationController(
duration: const Duration(milliseconds: 400), vsync: this);
controlsAnimation = CurvedAnimation(
parent: controlsAnimationController, curve: Curves.easeInOut);
}
@override
void dispose() {
controlsAnimationController.dispose();
super.dispose();
}
void toggleControls(bool disableClicks) {
if (disableClicks == false) {
if (controlsAnimationController.value > 0) {
setState(() {
controlsOpen = false;
});
controlsAnimationController.reverse();
} else {
setState(() {
controlsOpen = true;
});
controlsAnimationController.forward();
}
}
}
void closeControls() {
if (controlsAnimationController.value > 0) {
setState(() {
controlsOpen = false;
});
controlsAnimationController.reverse();
}
}
}
两个小部件都使用例如
class _Widget1State extends State<MediaViewer>
with TickerProviderStateMixin, SuperMediaViewer{
然后我可以轻松地在两个小部件中引用controlsOpen,controlsOpen和toggleControls()。
但是,如果我不断更改应用程序上的切换页面,则会收到此错误,该错误源自我的提供者状态对象之一。
I/flutter (31161): ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
I/flutter (31161): The following assertion was thrown while dispatching notifications for NewUserDetailsState:
I/flutter (31161): setState() or markNeedsBuild() called when widget tree was locked.
I/flutter (31161): This _DefaultInheritedProviderScope<NewUserDetailsState> widget cannot be marked as needing to build
I/flutter (31161): because the framework is locked.
I/flutter (31161): The widget on which setState() or markNeedsBuild() was called was:
我显然没有正确地做某事,但不确定。如果有人能指出正确的方向,我将不胜感激。
谢谢