Created
August 26, 2021 09:25
-
-
Save maheshj01/2e3157e5a8b9f5263be4437d8c0d6e21 to your computer and use it in GitHub Desktop.
pageview index not updating
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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