Here’s the widget I am using:
import 'package:flutter/material.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
const settings = Center(
child: Text(
'Home 1',
style: TextStyle(
color: Colors.white
),
),
);
const settings2 = Center(
child: Text(
'Home 2',
style: TextStyle(
color: Colors.white
),
),
);
const children = <Widget>[
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
ListTile(
title: settings
),
ListTile(
title: settings2
),
];
return Center(
child: SingleChildScrollView(
key: GlobalKey(debugLabel: 'home'),
controller: ScrollController(),
child: const Column(children: children),
),
);
}
}
Here’s the way I fetch the scrollable state:
final dynamic renderObject = key.currentContext!.findRenderObject();
final dynamic scrollableContext = key.currentContext;
final ScrollableState scrollableState = Scrollable.of(scrollableContext);
Here’s the error message I received:
No Scrollable widget ancestor could be found starting from the context that was passed to Scrollable.of(). This can happen because you are using a widget that looks for a Scrollable ancestor, but no such ancestor exists.
The context can be confirmed to be attached to the SingleChildView, but it still says there’s no scrollable ancestor. I wonder what happened.
0
ScrollablePositionedList I preferred if you’d like to locate an item and scroll to it in a ListView (https://pub.dev/packages/scrollable_positioned_list).
You can use ItemScrollController to scrollTo or jumpTo the valid place wherever you want via index (item position) at ease.
itemScrollController.scrollTo({
required int index,
required Duration duration,
double alignment = 0,
});