mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

Partially addresses https://github.com/flutter/flutter/issues/154138, specifically [#162025](https://github.com/flutter/flutter/issues/162025), [#162026](https://github.com/flutter/flutter/issues/162026), [#162028](https://github.com/flutter/flutter/issues/162028), [#162029](https://github.com/flutter/flutter/issues/162029) ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
83 lines
2.0 KiB
Dart
83 lines
2.0 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
void main() {
|
|
runApp(InfiniteScrollApp());
|
|
}
|
|
|
|
class InfiniteScrollApp extends StatelessWidget {
|
|
const InfiniteScrollApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
title: 'Infinite Scrolling Flutter',
|
|
home: InfiniteScrollList(),
|
|
);
|
|
}
|
|
}
|
|
|
|
class InfiniteScrollList extends StatefulWidget {
|
|
const InfiniteScrollList({super.key});
|
|
|
|
@override
|
|
InfiniteScrollListState createState() => InfiniteScrollListState();
|
|
}
|
|
|
|
class InfiniteScrollListState extends State<InfiniteScrollList> {
|
|
final List<String> items = [];
|
|
final int itemsPerPage = 20;
|
|
final List<String> staticData = [
|
|
"Hello Flutter",
|
|
"Hello Flutter",
|
|
"Hello Flutter",
|
|
"Hello Flutter",
|
|
"Hello Flutter",
|
|
];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadMoreData(); // Load initial data
|
|
}
|
|
|
|
void _loadMoreData() {
|
|
setState(() {
|
|
final newItems = List.generate(itemsPerPage, (i) {
|
|
return staticData[i % staticData.length];
|
|
});
|
|
items.addAll(newItems);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
home: Scaffold(
|
|
appBar: AppBar(
|
|
title: Text("Infinite Scrolling ListView (Static Data)"),
|
|
),
|
|
body: NotificationListener<ScrollNotification>(
|
|
onNotification: (ScrollNotification scrollInfo) {
|
|
if (scrollInfo.metrics.pixels >=
|
|
scrollInfo.metrics.maxScrollExtent - 50) {
|
|
_loadMoreData();
|
|
return true;
|
|
}
|
|
return false;
|
|
},
|
|
child: ListView.builder(
|
|
itemCount: items.length,
|
|
itemBuilder: (context, index) {
|
|
return ListTile(title: Text(items[index]));
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|