positioned_scroll_observer 2.1.0 positioned_scroll_observer: ^2.1.0 copied to clipboard
An elegant scroll observer that support most scroll views could jump/animateToIndex without breaking current widgets.
import 'package:flutter/material.dart';
import 'example/examples.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Indexed Scroll Observer Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
OutlinedButton(
onPressed: () {
context.push(
const Exmaple(),
);
},
child: const Text("Test Example"),
),
OutlinedButton(
onPressed: () {
context.push(
const OfficialListExample(),
);
},
child: const Text("List view Usage"),
),
OutlinedButton(
onPressed: () {
context.push(
const OfficialReorderableListExample(),
);
},
child: const Text("Reorderable List Usage"),
),
OutlinedButton(
onPressed: () {
context.push(
const OfficialSeparatedListExample(),
);
},
child: const Text("Separated List Usage"),
),
OutlinedButton(
onPressed: () {
context.push(const PositionedGridExample());
},
child: const Text("Encapsulated Grid Usage"),
),
OutlinedButton(
onPressed: () {
context.push(const CustomViewExample());
},
child: const Text("Encapsulated CustomScrollView Usage"),
),
OutlinedButton(
onPressed: () {
context.push(const SingleChildScrollExample());
},
child: const Text("SingleChildScrollView Usage"),
),
OutlinedButton(
onPressed: () {
context.push(const ListWheelExample());
},
child: const Text("List Wheel Usage"),
),
],
),
),
);
}
}
class Exmaple extends StatefulWidget {
const Exmaple({super.key});
@override
State<Exmaple> createState() => _ExmapleState();
}
class _ExmapleState extends State<Exmaple> {
final List<Widget> items = List.generate(30, (index) => index)
.map(
(e) => ListTile(
title: Text("Item $e"),
),
)
.toList();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("test Example"),
),
body: Column(
children: [
OutlinedButton(
onPressed: _add,
child: const Text("insert"),
),
Expanded(
child: ListView.builder(
reverse: true,
itemCount: items.length,
itemBuilder: (_, index) => items[index],
),
)
],
),
);
}
void _add() {
// items.insert(
// 0,
// ListTile(
// title: Text("item ${items.length}"),
// ));
items.add(ListTile(
title: Text("item ${items.length}"),
));
setState(() {});
}
}
extension Navigation on BuildContext {
void push(Widget page) {
Navigator.of(this).push(
MaterialPageRoute(builder: (_) => page),
);
}
}