Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Sliver Layout horizontal scroll inside Sliver List

I try to make horizontal scrollable list inside Sliver List (CustomScrollview - SliverList)

This is my Code:

import 'package:flutter/material.dart';
class DetailScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
   return Scaffold(
    body: CustomScrollView(
        slivers: <Widget>[
        DetailAppBar(),
        SliverPadding(
          padding: EdgeInsets.all(16.0),
          sliver: SliverList(
            delegate: SliverChildListDelegate(
              [
                Card(child: Text('data'),),
                Card(child: Text('data'),),
                Card(child: Text('data'),),
                Card(child: Text('data'),),

                // Scrollable horizontal widget here
              ],
            ),
          ),
        ),
      ],
    ),
    bottomNavigationBar: NavigationButton());


 }

}

Can you give me example or solution to this case? i really need some help.

like image 499
Dion Fananie Avatar asked Oct 10 '18 10:10

Dion Fananie


People also ask

How do you make a horizontal scrollable list in flutter?

You might want to create a list that scrolls horizontally rather than vertically. The ListView widget supports horizontal lists. Use the standard ListView constructor, passing in a horizontal scrollDirection , which overrides the default vertical direction.

What is a sliver in flutter?

In Flutter, slivers (not silver) are nothing but parts of a scrollable area. Using sliver stuff helps us create many fancy scrolling effects and can make the scrolling process through a large number of children more efficient due to the ability to lazily build each item when it scrolls into view.

What are the best scrolling widgets in flutter?

Flutter itself provides you with a number of different type of scrolling widgets for this very purpose. Some of them include ListView, GridView, CustomScrollView, SingleChildScrollView, PageView, Scrollable, Scrollbar etc. Among these the most frequently used ones include the ListView and the GridView.

Where do the sliver components go inside a customscrollview?

All sliver components go inside a CustomScrollView. The components are assigned to the slivers argument of the CustomScrollView class in the form of a List. The list of slivers can be combined in any which way to obtain a desirable custom scrollable area.

What is sliverlist and how do I use it?

Using sliver stuff helps us create many fancy scrolling effects and can make the scrolling process through a large number of children more efficient due to the ability to lazily build each item when it scrolls into view. SliverList needs to be implemented inside a silver group and a CustomScrollView, like this: Here’s the SliverList constructor:


2 Answers

Use a ListView inside of a SliverToBoxAdapter.

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverPadding(
            padding: EdgeInsets.all(16.0),
            sliver: SliverList(
              delegate: SliverChildListDelegate(
                [
                  Card(
                    child: Text('data'),
                  ),
                  Card(
                    child: Text('data'),
                  ),
                  Card(
                    child: Text('data'),
                  ),
                  Card(
                    child: Text('data'),
                  ),
                ],
              ),
            ),
          ),
          SliverToBoxAdapter(
            child: Container(
              height: 100.0,
              child: ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: 10,
                itemBuilder: (context, index) {
                  return Container(
                    width: 100.0,
                    child: Card(
                      child: Text('data'),
                    ),
                  );
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
like image 114
Albert Lardizabal Avatar answered Oct 16 '22 20:10

Albert Lardizabal


the other solution doesn't work for me as it gives error when I use a ListView

here is a class I wrote called HorizontalSliverList which does the job nice and easy

here is the class you need to copy:

class HorizontalSliverList extends StatelessWidget {
  final List<Widget> children;
  final EdgeInsets listPadding;
  final Widget divider;

  const HorizontalSliverList({
    Key key,
    @required this.children,
    this.listPadding = const EdgeInsets.all(8),
    this.divider,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SliverToBoxAdapter(
      child: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Padding(
          padding: listPadding,
          child: Row(children: [
            for (var i = 0; i < children.length; i++) ...[
              children[i],
              if (i != children.length - 1) addDivider(),
            ],
          ]),
        ),
      ),
    );
  }

  Widget addDivider() => divider ?? Padding(padding: const EdgeInsets.symmetric(horizontal: 8));
}

like image 2
alireza easazade Avatar answered Oct 16 '22 20:10

alireza easazade