当我设置scrollDirection等于Axis.horizontal时,滚动不起作用。请帮助我更改代码以使其正常工作?当我将scrollDirection设置为Axis.vertical时,它正确工作。
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
constraints: const BoxConstraints(minHeight: 50),
height: 100,
padding: const EdgeInsets.symmetric(vertical: 15),
color: Colors.red[400],
child: ListView.builder(
scrollDirection: Axis.horizontal,
physics: const AlwaysScrollableScrollPhysics(),
itemCount: 50,
itemBuilder: (c, i) {
return Text('Item ${i + 1} ');
},
),
),
);
}
}发布于 2022-05-25 10:32:46
Axis.vertical正在工作,因为您为父级提供了一个高度。要使Aix.horizontal工作,您需要为父容器提供一个宽度。检查下面的代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
constraints: const BoxConstraints(minHeight: 50),
width: 150,
padding: const EdgeInsets.symmetric(vertical: 15),
color: Colors.red[400],
child: ListView.builder(
scrollDirection: Axis.horizontal,
physics: const AlwaysScrollableScrollPhysics(),
itemCount: 50,
itemBuilder: (c, i) {
return Text('Item ${i + 1} ');
},
),
),
);
}
}https://stackoverflow.com/questions/72375725
复制相似问题