Поскольку controller
требуется в MotionTabView
, а не в MotionTabBar
, переместите _eduTabController
в Education
класс. Теперь определите функцию, которая требуется при выборе вкладки, то есть onTabItemSelected
в классе Education
, и передайте ее в качестве аргумента в TestTab
.
Таким образом вы можете избежать наличия _eduTabController
в TestTab
и используйте его в MotionTabView
, которые существуют в Education
.
Вот примерная реализация:
// convert Education to StatefulWidget
class Education extends StatefulWidget {
@override
_EducationState createState() => _EducationState();
}
// Requires SingleTickerProviderStateMixin
class _EducationState extends State<Education> with SingleTickerProviderStateMixin {
final MotionTabController _eduTabController; // have your controller here
@override
void initState() {
super.initState();
// initialize controller
_eduTabController = MotionTabController(initialIndex: 1, vsync: this);
}
@override
void dispose() {
super.dispose();
// dispose controller
_eduTabController?.dispose();
}
// Extract your function from TestTab
void onTabItemSelected(int value) {
setState(() {
_eduTabController.index = value;
});
},
@override
Widget build(BuildContext context) {
//...
Align(
alignment: Alignment.bottomCenter,
child: TestTab(
// don't pass the controller
onTabItemSelected: onTabItemSelected, // pass the function instead
),
),
MotionTabBarView(
controller: _eduTabController,
// ...
),
//...
}
}
// This should be a StatelessWidget (because it has no state)
// Change to StatefulWidget if your requirements change
class TestTab extends StatefulWidget {
final Function onTabItemSelected;
TestTab({this.onTabItemSelected}); // receive your function here
@override
Widget build(BuildContext context) {
return MotionTabBar(
// ...
onTabItemSelected: onTabItemSelected, // use the function here
// ...
);
}
}