Skip to content

Instantly share code, notes, and snippets.

@maheshj01
Created August 26, 2021 09:25
Show Gist options
  • Save maheshj01/2e3157e5a8b9f5263be4437d8c0d6e21 to your computer and use it in GitHub Desktop.
Save maheshj01/2e3157e5a8b9f5263be4437d8c0d6e21 to your computer and use it in GitHub Desktop.
pageview index not updating
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage());
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
PageController _controller = PageController(initialPage: 0);
int length = 25;
@override
void initState() {
// TODO: implement initState
super.initState();
if (_controller.hasClients) {
_controller.addListener(increment);
}
}
void increment() {
index++;
print(index);
}
int index = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'${index}',
),
),
body: PageView.builder(
itemCount: length,
scrollDirection: Axis.horizontal,
controller: _controller,
onPageChanged: (x) {
print(x);
},
itemBuilder: (BuildContext context, int x) {
return Center(
child: Text('item $x'),
);
},
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment