В виджете imageSelector создайте переменную для доступа к контроллеру, например:
class imageSelector extends StatelessWidget {
final CarouselController _controller;
imageSelector(this._controller);
теперь передайте значение контроллера в качестве параметра при вызове imageSelector, например:
imageSelector(_controller)
Так Ваш исправленный код будет:
Для ManuallyControlledSlider:
class ManuallyControlledSlider extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _ManuallyControlledSliderState();
}
}
class _ManuallyControlledSliderState extends State<ManuallyControlledSlider> {
final CarouselController _controller = CarouselController();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Manually controlled slider')),
body: Column(
children: <Widget>[
CarouselSlider(
items: imageSliders,
options: CarouselOptions(enlargeCenterPage: true, aspectRatio: 16/9),
carouselController: _controller,
),
imageSelector(_controller), //Corrected
],
),
);
}
}
И дочерний:
class imageSelector extends StatelessWidget {
final CarouselController _controller;
imageSelector(this._controller);
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Flexible(
child: RaisedButton(
onPressed: () => _controller.previousPage(
duration: Duration(milliseconds: 300), curve: Curves.linear),
child: Text('←'),
),
),
Flexible(
child: RaisedButton(
onPressed: () => _controller.nextPage(
duration: Duration(milliseconds: 300), curve: Curves.linear),
child: Text('→'),
),
),
...Iterable<int>.generate(imgList.length).map(
(int pageIndex) => Flexible(
child: RaisedButton(
onPressed: () => _controller.animateToPage(
pageIndex,
duration: Duration(milliseconds: 300),
curve: Curves.linear),
child: Text("$pageIndex"),
),
),
),
],
);
}