Skip to content

Instantly share code, notes, and snippets.

@sgruhier
Created April 29, 2025 08:30
Show Gist options
  • Save sgruhier/48c281b8106dd4ae421596788224cc94 to your computer and use it in GitHub Desktop.
Save sgruhier/48c281b8106dd4ae421596788224cc94 to your computer and use it in GitHub Desktop.
PageStorageKey
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Tab Scroll Position Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
late final TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Tabs with Scroll Memory'),
bottom: TabBar(
controller: _tabController,
tabs: const [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
],
),
),
body: TabBarView(
controller: _tabController,
physics: const NeverScrollableScrollPhysics(),
children: [buildListView(0), buildListView(1), buildListView(2)],
),
);
}
Widget buildListView(int tabIndex) {
return ListView.builder(
key: PageStorageKey('tab$tabIndex'),
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(title: Text('Tab $tabIndex - Item $index'));
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment