Created
November 4, 2022 15:41
-
-
Save leighajarett/63039c5371995ae53d971d613a936f7b to your computer and use it in GitHub Desktop.
Flutter Scroll Example
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'; | |
import 'package:flutter/cupertino.dart'; | |
void main() { | |
runApp( | |
const App(), | |
); | |
} | |
class App extends StatelessWidget { | |
const App({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return const CupertinoApp( | |
home: HomePage(), | |
); | |
} | |
} | |
// create a class that holds each person's data | |
@immutable | |
class Person { | |
final String name; | |
final int age; | |
const Person({ | |
required this.name, | |
required this.age, | |
}); | |
} | |
// like in SwiftUI, we create a widget (view in SwiftUI), | |
// that represents each person visually on the screen | |
class PersonView extends StatelessWidget { | |
final Person person; | |
const PersonView({ | |
super.key, | |
required this.person, | |
}); | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: [ | |
Row( | |
children: [ | |
const Text('Name:'), | |
Text(person.name), | |
], | |
), | |
Row( | |
children: [ | |
const Text('Age:'), | |
Text(person.age.toString()), | |
], | |
), | |
const Divider(), | |
], | |
); | |
} | |
} | |
// then we create a list of people | |
final mockPersons = Iterable.generate( | |
100, | |
(index) => Person( | |
name: 'Person #${index + 1}', | |
age: 10 + index, | |
), | |
); | |
class HomePage extends StatelessWidget { | |
const HomePage({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
// and last but not least, we display the list | |
// of people on the screen inside a scroll viwe of type | |
// SingleChildScrollView (equivalent of ScrollView in SwiftUI) | |
body: | |
// #docregion ScrollExample | |
SingleChildScrollView( | |
child: Column( | |
children: mockPersons | |
.map( | |
(person) => PersonView( | |
person: person, | |
), | |
) | |
.toList(), | |
), | |
), | |
// #enddocregion ScrollExample | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment