Last active
April 11, 2019 18:18
-
-
Save vizioners/62570cc067666747f7d9000360df0220 to your computer and use it in GitHub Desktop.
gird view load more
This file contains 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
fetchData() async { | |
var _duration = new Duration(seconds: 2); | |
return new Timer(_duration, onResponse); | |
} | |
void onResponse() { | |
setState(() { | |
isLoading = !isLoading; | |
var arr = <int>[]; | |
for(int i = 0; i < 30 ;i++){ | |
arr.add(itemCount); | |
itemCount += 1; | |
} | |
_all.addAll(arr); | |
}); | |
} |
This file contains 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 'dart:async'; | |
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
class Home extends StatefulWidget { | |
@override | |
createState() => new HomeState(); | |
} | |
class HomeState extends State<Home> { | |
ScrollController controller; | |
final _all = <int>[]; | |
GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>(); | |
bool isLoading = false; | |
bool loadMore = true; | |
int itemCount = 0; | |
@override | |
void initState() { | |
super.initState(); | |
var arr = <int>[]; | |
for(int i = 0; i < 30 ;i++){ | |
arr.add(itemCount); | |
itemCount += 1; | |
} | |
itemCount = 30; | |
_all.addAll(arr); | |
controller = new ScrollController()..addListener(_scrollListener); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
controller.dispose(); | |
} | |
void _scrollListener() { | |
if ((controller.position.pixels > controller.position.maxScrollExtent - 300.0) && loadMore) { | |
loadMore = false; | |
startLoader(); | |
} | |
} | |
void startLoader() { | |
setState(() { | |
isLoading = !isLoading; | |
fetchData(); | |
}); | |
} | |
fetchData() async { | |
var _duration = new Duration(seconds: 2); | |
return new Timer(_duration, onResponse); | |
} | |
void onResponse() { | |
setState(() { | |
isLoading = !isLoading; | |
var arr = <int>[]; | |
for(int i = 0; i < 30 ;i++){ | |
arr.add(itemCount); | |
itemCount += 1; | |
} | |
_all.addAll(arr); | |
}); | |
loadMore = true; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
key: scaffoldKey, | |
appBar: new AppBar( | |
title: new Text( | |
"Gird view load more", | |
style: TextStyle(color: Colors.white), | |
), | |
), | |
body: new Stack( | |
children: <Widget>[ | |
_buildSuggestions(), | |
_loader(), | |
], | |
), | |
); | |
} | |
Widget _buildRow(int index) { | |
return new GestureDetector( | |
child: new Card( | |
elevation: 5.0, | |
child: new Container( | |
alignment: Alignment.center, | |
child: new Text('Item $index'), | |
), | |
), | |
onTap: () { | |
showDialog( | |
context: context, | |
barrierDismissible: false, | |
child: new CupertinoAlertDialog( | |
title: new Column( | |
children: <Widget>[ | |
new Text("Girdview"), | |
new Icon( | |
Icons.favorite, | |
color: Colors.green, | |
) | |
], | |
), | |
content: new Text("Selected Item $index"), | |
actions: <Widget>[ | |
new FlatButton(onPressed: () { | |
Navigator.of(context).pop(); | |
}, | |
child: new Text("OK") | |
) | |
], | |
) | |
); | |
}, | |
); | |
} | |
Widget _buildSuggestions() { | |
return new GridView.builder( | |
padding: const EdgeInsets.all(16.0), | |
// The itemBuilder callback is called once per suggested word pairing, | |
// and places each suggestion into a ListTile row. | |
// For even rows, the function adds a ListTile row for the word pairing. | |
// For odd rows, the function adds a Divider widget to visually | |
// separate the entries. Note that the divider may be difficult | |
// to see on smaller devices. | |
controller: controller, | |
itemCount: _all.length, | |
gridDelegate: | |
new SliverGridDelegateWithFixedCrossAxisCount( | |
crossAxisCount: 3), | |
itemBuilder: (context, index) { | |
// Add a one-pixel-high divider widget before each row in theListView. | |
return _buildRow(_all[index]); | |
}); | |
} | |
Widget _loader() { | |
return isLoading | |
? new Align( | |
child: new Container( | |
width: 70.0, | |
height: 70.0, | |
child: new Padding( | |
padding: const EdgeInsets.all(5.0), | |
child: new Center(child: new CircularProgressIndicator())), | |
), | |
alignment: FractionalOffset.bottomCenter, | |
) | |
: new SizedBox( | |
width: 0.0, | |
height: 0.0, | |
); | |
} | |
} |
This file contains 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'; | |
import 'package:girdview/home.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
home: new Home(), | |
); | |
} | |
} |
This file contains 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
void _scrollListener() { | |
if (controller.position.pixels == controller.position.maxScrollExtent) { | |
startLoader(); | |
} | |
} |
This file contains 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
void startLoader() { | |
setState(() { | |
isLoading = !isLoading; | |
fetchData(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment