How to make a flutter app scroll both horizontally and vertically

Viewed 186

I'm having a hard time making a flutter screen be scrollable both horizontally and vertically. I can make it work in one direction but fail when I try it in both directions.

Here is a mock app. For background, I'm targeting the web, where the information should be accessible by keyboard & mouse even if it's outside the immediate viewing area.

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: ListView(
        scrollDirection: Axis.horizontal,
        children: [
          ListView(
            scrollDirection: Axis.vertical,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    children: [
                      for (var i = 0; i < 4; i++)
                        Container(
                            child: Text('Header $i'),
                            width: 300,
                            height: 100,
                            color: Colors.greenAccent,
                            margin: const EdgeInsets.all(20)),
                    ],
                  ),
                  ...[
                    for (var i = 0; i < 50; i++)
                      Container(
                        child: Text('Column $i'),
                        width: 150,
                        height: 50,
                        color: Colors.orange,
                        margin: const EdgeInsets.all(10),
                      )
                  ],
                ],
              )
            ],
          )
        ],
      ),
    );
  }
}

Thank you for any suggestions on how to make this work!

Tony

3 Answers

Still not exactly what I want, but it's closer and may be good enough.

I added a scroll controller.

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> {
  late ScrollController _scrollControllerH;

  @override
  void initState() {
    _scrollControllerH = ScrollController();
    super.initState();
  }

  @override
  void dispose() {
    _scrollControllerH.dispose();
    super.dispose();
  }

  /// scroll only the header independent of the table
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: ListView(
      scrollDirection: Axis.vertical,
      children: [
        Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Scrollbar(
              controller: _scrollControllerH,
              child: SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                controller: _scrollControllerH,
                child: Row(
                  children: [
                    for (var i = 0; i < 4; i++)
                      Container(
                          child: Text('Header $i'),
                          width: 300,
                          height: 100,
                          color: Colors.greenAccent,
                          margin: const EdgeInsets.all(20)),
                  ],
                ),
              ),
            ),
            ...[
              for (var i = 0; i < 50; i++)
                Container(
                  child: Text('Column $i'),
                  width: 150,
                  height: 50,
                  color: Colors.orange,
                  margin: const EdgeInsets.all(10),
                )
            ],
          ],
        )
      ],
    ));
  }
}

You can make any view scrollable both vertically and horizontally in Flutter using the following snippet (tested and confirmed to work on a Linux app using Flutter 3.0.1):

SingleChildScrollView(
  scrollDirection: Axis.vertical,
  child: SingleChildScrollView(
    scrollDirection: Axis.horizontal,
    child: <widget-to-be-scrollable-both-vertically-and-horizontally>,
  ),
)
Related