简体   繁体   中英

Flutter - ListView.Builder - Horizontal scroll not working

When I set scrollDirection equal Axis.horizontal, scrolling did not work. Please help me to changes my code to work properly? When I set scrollDirection equal Axis.vertical, it work correctly.

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} ');
          },
        ),
      ),
    );
  }
}

Axis.vertical is working because you have provided the parent a height. For Aix.horizontal to work you need to provide the parent Container a width. Check the code below

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} ');
          },
        ),
      ),
    );
  }
}

Please Refer Below Code. It's working for me. Please check once.

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: Padding(
        padding: const EdgeInsets.only(top: 50),
        child: 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} ');
            },
          ),
        ),
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM