1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 22:09:06 +00:00
Files
samples/infinite_list/lib/main.dart
Brett Morgan 36e7a6ab04 Update for Flutter 3.10 beta (#1746)
## Pre-launch Checklist

- [x] I read the [Flutter Style Guide] _recently_, and have followed its
advice.
- [x] I signed the [CLA].
- [x] I read the [Contributors Guide].
- [x] I updated/added relevant documentation (doc comments with `///`).
- [ ] All existing and new tests are passing.

---------

Co-authored-by: David Iglesias <ditman@gmail.com>
Co-authored-by: Mark Thompson <2554588+MarkTechson@users.noreply.github.com>
Co-authored-by: John Ryan <ryjohn@google.com>
2023-05-11 06:16:31 +10:00

94 lines
2.8 KiB
Dart

// Copyright 2020 The Chromium 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 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:window_size/window_size.dart';
import 'src/api/item.dart';
import 'src/catalog.dart';
import 'src/item_tile.dart';
void main() {
setupWindow();
runApp(const MyApp());
}
const double windowWidth = 480;
const double windowHeight = 854;
void setupWindow() {
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
WidgetsFlutterBinding.ensureInitialized();
setWindowTitle('Infinite List');
setWindowMinSize(const Size(windowWidth, windowHeight));
setWindowMaxSize(const Size(windowWidth, windowHeight));
getCurrentScreen().then((screen) {
setWindowFrame(Rect.fromCenter(
center: screen!.frame.center,
width: windowWidth,
height: windowHeight,
));
});
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<Catalog>(
create: (context) => Catalog(),
child: MaterialApp(
title: 'Infinite List Sample',
theme: ThemeData.light(useMaterial3: true),
home: const MyHomePage(),
),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Infinite List Sample'),
),
body: Selector<Catalog, int?>(
// Selector is a widget from package:provider. It allows us to listen
// to only one aspect of a provided value. In this case, we are only
// listening to the catalog's `itemCount`, because that's all we need
// at this level.
selector: (context, catalog) => catalog.itemCount,
builder: (context, itemCount, child) => ListView.builder(
// When `itemCount` is null, `ListView` assumes an infinite list.
// Once we provide a value, it will stop the scrolling beyond
// the last element.
itemCount: itemCount,
padding: const EdgeInsets.symmetric(vertical: 18),
itemBuilder: (context, index) {
// Every item of the `ListView` is individually listening
// to the catalog.
var catalog = Provider.of<Catalog>(context);
// Catalog provides a single synchronous method for getting the
// current data.
return switch (catalog.getByIndex(index)) {
Item(isLoading: true) => const LoadingItemTile(),
var item => ItemTile(item: item)
};
},
),
),
);
}
}