Thursday, August 13, 2020

PageView and PageController || Flutter

 PageView and PageController | Flutter Tutorial 

.....................................................................................................................................................................................................................................
Example :#1

import 'package:flutter/material.dart';
void main()=>runApp(MaterialApp(home:MyApp(),
debugShowCheckedModeBanner: false,));
class MyApp extends StatefulWidget{
@override
MyAppState createState()=>MyAppState();
}
class MyAppState extends State<MyApp>{
PageController pc = PageController(initialPage: 0);
int onPageChanged =0;
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text('Page view demo'),
centerTitle: true,
actions: <Widget>[
IconButton(onPressed: (){
pc.animateToPage(--onPageChanged, duration: Duration(milliseconds: 25),
curve: Curves.bounceInOut);
},icon: Icon(Icons.arrow_back_ios,)),
IconButton(onPressed: (){
pc.animateToPage(++onPageChanged, duration: Duration(milliseconds: 25),
curve: Curves.bounceInOut);
},icon: Icon(Icons.arrow_forward_ios)),
],
),
body: PageView(
pageSnapping: false,
controller: pc,
onPageChanged: (index){
setState((){
onPageChanged=index;
});
print(onPageChanged);
},
children: <Widget>[
Container(color: Colors.red,),
Container(color: Colors.green,),
Container(color: Colors.indigo,),
],
),
);
}
}

.....................................................................................................................................................................................................................................
Example :#2

import 'package:flutter/material.dart';
void main()=>runApp(MaterialApp(home: MyApp(),));

class MyApp extends StatefulWidget{
@override
MyAppState createState()=>MyAppState();
}
class MyAppState extends State<MyApp>{
PageController pc = PageController(initialPage: 0);
int PageChanged=0;

@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
title: Text('pageView Controller'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: (){
pc.animateToPage(--PageChanged, duration: Duration(milliseconds: 25), curve: Curves.easeInOut);
},
),
IconButton(
icon: Icon(Icons.arrow_forward_ios),
onPressed: (){
pc.animateToPage(++PageChanged, duration: Duration(milliseconds: 25), curve: Curves.easeInOut);
},
),
],
),
body: PageView(
pageSnapping: false,
controller: pc,
onPageChanged: (index){
setState(() {
PageChanged=index;
});
print(PageChanged);
},

children: <Widget>[
Container(color: Colors.grey),
Container(color: Colors.red),
Container(color: Colors.green),
Container(color: Colors.indigo),
],
),
);
}
}

.....................................................................................................................................................................................................................................
Example :#3

import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
List<String> list = List.generate(10, (element) => '${element}');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Material(
child: PageView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
return Container(
color: index %2==0? Colors.red: Colors.green,
child: Text(list[index], style: TextStyle(fontSize: 40),),
);
}),
),
);
}
}

No comments:

Post a Comment