I have a very long ListView with long text in the ListView item and I don't want the text to wrap automatically. How to modify the code to scroll the entire page horizontally to show the text on the right.
I think this is a very common requirement. For example, a text editor needs to display many long lines of text, and can also scroll to the right when the text is long.
import 'dart:ui';
import 'package:flutter/material.dart';
void main(List<String> args) {
runApp(const MyApp());
}
class MyCustomScrollBehavior extends MaterialScrollBehavior {
@override
Set<PointerDeviceKind> get dragDevices => {
PointerDeviceKind.touch,
PointerDeviceKind.mouse,
};
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
scrollBehavior: MyCustomScrollBehavior(),
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage());
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ListView example'),
),
body: ListView.builder(
itemExtent: 50,
itemCount: 1000 * 1000 * 1000,
itemBuilder: (context, index) {
return Container(
padding: const EdgeInsets.all(10),
color: index % 2 == 0 ? Colors.white : const Color(0xFFEFEFEF),
child: Text(
"$index very long long long long long long long long long"
"long long long long long long long long long long long"
"long long long long long long long long long long long"
"long long long long long long long long long long long"
"long long long long long long long long long long text",
softWrap: false,
),
);
},
),
);
}
}
Although I can add a SingleChildScrollView to the outer layer, it seems that children in SingleChildScrollView must specify the width, it cannot automatically adjust based on the current text length, and the scroll bar will run to the far right. How to better achieve horizontal scrolling of ListView pages.
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ListView example'),
),
body: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: SizedBox(
width: 2000,
child: ListView.builder(
itemExtent: 50,
itemCount: 1000 * 1000 * 1000,
itemBuilder: (context, index) {
return Container(
padding: const EdgeInsets.all(10),
color: index % 2 == 0 ? Colors.white : const Color(0xFFEFEFEF),
child: Text(
"$index very long long long long long long long long long"
"long long long long long long long long long long long"
"long long long long long long long long long long long"
"long long long long long long long long long long long"
"long long long long long long long long long long text",
softWrap: false,
),
);
},
),
),
),
);
}
}
Or add SingleChildScrollView to each item, but only a single line can be scrolled, which does not achieve the desired effect.
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('ListView example'),
),
body: ListView.builder(
itemExtent: 50,
itemCount: 1000 * 1000 * 1000,
itemBuilder: (context, index) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Container(
padding: const EdgeInsets.all(10),
color: index % 2 == 0 ? Colors.white : const Color(0xFFEFEFEF),
child: Text(
"$index very long long long long long long long long long"
"long long long long long long long long long long long"
"long long long long long long long long long long long"
"long long long long long long long long long long long"
"long long long long long long long long long long text",
softWrap: false,
),
),
);
},
),
);
}
}

two_dimensional_scrollablesalready handles rendering mechanism and only render visible items. Once I played with game of life,might give you some idea