PageView
не готов при вызове _pageController.animateToPage
Вы можете использовать addPostFrameCallback
и проверить _pageController.hasClients
фрагмент кода
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
Timer.periodic(Duration(seconds: 4), (Timer timer) {
if (_currentPage < 3) {
_currentPage++;
} else {
_currentPage = 0;
}
if(_pageController.hasClients) {
_pageController.animateToPage(
_currentPage,
duration: Duration(milliseconds: 400),
curve: Curves.easeInCubic,
);
}
});
});
}
рабочая демонстрация для моделирования этот случай
полный код теста
import 'package:flutter/material.dart';
import 'dart:async';
class Feature {
String name;
Color color;
String header;
String subHeader;
String imgUrl;
Feature({this.name, this.color, this.header, this.subHeader, this.imgUrl});
}
List<Feature> featuredList = [
Feature(
name: "a",
color: Colors.pink,
header: "ah",
subHeader: "a sub",
imgUrl: "https://picsum.photos/250?image=9"),
Feature(
name: "b",
color: Colors.blue,
header: "bh",
subHeader: "b sub",
imgUrl: "https://picsum.photos/250?image=10"),
Feature(
name: "c",
color: Colors.yellow,
header: "ch",
subHeader: "c sub",
imgUrl: "https://picsum.photos/250?image=11")
];
class WidgetFeatured extends StatefulWidget {
@override
_WidgetFeaturedState createState() => _WidgetFeaturedState();
}
class _WidgetFeaturedState extends State<WidgetFeatured> {
int _currentPage = 0;
final PageController _pageController = PageController(
initialPage: 0,
);
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
Timer.periodic(Duration(seconds: 4), (Timer timer) {
if (_currentPage < 3) {
_currentPage++;
} else {
_currentPage = 0;
}
if(_pageController.hasClients) {
_pageController.animateToPage(
_currentPage,
duration: Duration(milliseconds: 400),
curve: Curves.easeInCubic,
);
}
});
});
}
@override
void dispose() {
super.dispose();
_pageController.dispose();
}
_onPageChanged(int index) {
setState(() {
_currentPage = index;
});
}
@override
Widget build(BuildContext context) {
return Container(
height: 200,
child: PageView.builder(
physics: BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
controller: _pageController,
onPageChanged: _onPageChanged,
itemBuilder: (context, index) => WidgetFeaturedItem(index),
itemCount: featuredList.length,
),
);
}
}
class WidgetFeaturedItem extends StatelessWidget {
final int indexItem;
WidgetFeaturedItem(this.indexItem);
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.all(20),
color: featuredList[indexItem].color,
width: double.infinity,
height: 180,
child: Stack(
children: <Widget>[
Row(
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
featuredList[indexItem].header,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
Text(
featuredList[indexItem].subHeader,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
FlatButton(
onPressed: () {},
padding: const EdgeInsets.symmetric(
horizontal: 15,
vertical: 3,
),
color: Theme.of(context).primaryColor,
child: Text(
'Order now',
style: TextStyle(
color: Colors.white,
fontSize: 12,
fontWeight: FontWeight.w100,
),
),
),
],
),
Image.network(
featuredList[indexItem].imgUrl,
height: 120,
width: 120,
//width: 335,
fit: BoxFit.cover,
),
],
)
],
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(child: WidgetFeatured()),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}