Home > Software design >  Flutter - ListView.Builder - Horizontal scroll not working
Flutter - ListView.Builder - Horizontal scroll not working

Time:05-26

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

CodePudding user response:

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

CodePudding user response:

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

  • Related