how to create horizontal and vertical scrollable widgets in flutter

后端 未结 3 976
小蘑菇
小蘑菇 2021-01-01 02:36

sample Layout image

I am trying to create attached layout. It has two containers.

  • First is a fixed size box that scrolls horizontally.
相关标签:
3条回答
  • 2021-01-01 03:25

    tl;dr: the following code does what you want =D

    import 'package:flutter/material.dart';
    
    void main() async {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'stack overflow',
          theme: ThemeData(
            primarySwatch: Colors.pink,
          ),
          routes: {},
          home: KanbanState(),
        );
      }
    }
    
    class KanbanState extends StatefulWidget {
      @override
      KanbanStateState createState() {
        return KanbanStateState();
      }
    }
    
    class KanbanStateState extends State<KanbanState> {
      @override
      Widget build(BuildContext context) {
        Widget tagList = Container(
          color: Colors.green,
          height: 100.0,
          child: ListView(
            scrollDirection: Axis.horizontal,
            children: <Widget>[
              Row(
                children: <Widget>[
                  ActionChip(
                      backgroundColor: Colors.yellow,
                      label: Text('Tag1'),
                      onPressed: () {
                        // update board with selection
                      }),
                  ActionChip(
                      //backgroundColor: Colors.transparent,
                      label: Text('Tag2'),
                      onPressed: () {
                        // update board with selection
                      }),
                  ActionChip(
                      label: Text('Tag3'),
                      onPressed: () {
                        // update board with selection
                      }),
                  ActionChip(
                      label: Text('Tag4'),
                      onPressed: () {
                        // update board with selection
                      }),
                  ActionChip(
                      backgroundColor: Colors.yellow,
                      label: Text('Tag1'),
                      onPressed: () {
                        // update board with selection
                      }),
                  ActionChip(
                      //backgroundColor: Colors.transparent,
                      label: Text('Tag2'),
                      onPressed: () {
                        // update board with selection
                      }),
                  ActionChip(
                      label: Text('Tag3'),
                      onPressed: () {
                        // update board with selection
                      }),
                  ActionChip(
                      label: Text('Tag4'),
                      onPressed: () {
                        // update board with selection
                      }),
                ],
              )
            ],
          ),
        );
    
        Widget boardView = Container(
          color: Colors.blue,
          child: ListView.builder(
            scrollDirection: Axis.vertical,
            itemCount: 15,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(
                onTap: () {},
                title: Row(
                  children: <Widget>[
                    Expanded(child: Text("This is item name")),
                    Text("12 Dec 18"),
                  ],
                ),
              );
            },
          ),
        );
    
        //  int _value=0;
        return Scaffold(
            appBar: AppBar(
              elevation: 1.0,
              title: Text("Test title"),
            ),
            body: Container(
              color: Colors.amber,
              child: new Column(
                children: <Widget>[
                  tagList,
                  Expanded(
                    child: boardView,
                  )
                ],
              ),
              margin: EdgeInsets.all(10.0),
            ));
      }
    }
    

    Heres the thought process: I started cleaning up each widget and making sure they would be properly displayed. In the taglist widget, notice you have a row as the only widget in a column. In the boardView, LisView is also the only element in a column;

    Then I added more items to make sure both scrolls would work. Adding scrollDirection: Axis.horizontal int the tagList made sure of that.

    At last, time to put it all together and display both elements. Remove the top Container as Scaffold is enough. Then is was inly a matter of placing the boardView in the Expanded widget.

    This was a fun exercise. =D

    0 讨论(0)
  • 2021-01-01 03:25

    Use this trick:

    1. Wrap your Row in a SingleChildScrollView.
    2. Use scrollDirection: Axis.horizontal in the SingleChildScrollView.
    3. Wrap SingleChildScrollView in the your ListView.
    0 讨论(0)
  • 2021-01-01 03:29

    Use CustomScrollView with SliverListview then you can achieve any kind of views with vertical scrolling horizontal scrolling.

    CustomScrollView(
        slivers: <Widget>[
         SliverList(
      delegate: new SliverChildBuilderDelegate(
        (context, index) {
          return Container(
            child: Row(
              children: <Widget>[
                buildTitle(),
                Expanded(
                  child: _buildList(),
                ),
              ],
            ),
          );
        },
        childCount: array.length,
      ),
    );,
        ],
      );
    
    0 讨论(0)
提交回复
热议问题