0

I'm trying to build a horizontal carousel of container widgets - i.e. the user simply scrolls horizontally to view the 6 container widgets I have.

How can I implement this?

I have tried using SingleChildScrollView() - and placed in there a Row() widget, but that didn't do anything.. nothing scrolled.

Row(children: [
Container(child: Card(child: Text('Unit 1'))),
Container(child: Card(child: Text('Unit 2'))),
Container(child: Card(child: Text('Unit 3'))),
Container(child: Card(child: Text('Unit 4'))),
Container(child: Card(child: Text('Unit 5'))),
Container(child: Card(child: Text('Unit 6'))),

]),

I'd like each container to be on the same row, and the end user able to scroll horizontally to view each item,

How would you implement this?

houba
  • 496
  • 7
  • 20

1 Answers1

1

You can copy paste run full code below
You can use package https://pub.dev/packages/flutter_mobile_carousel
You can use rowCount to define how many items need to show at the same time

code snippet

 Carousel(rowCount: 4, children: [
              Container(child: Card(child: Text('Unit 1'))),
              Container(child: Card(child: Text('Unit 2'))),
              Container(child: Card(child: Text('Unit 3'))),
              Container(child: Card(child: Text('Unit 4'))),
              Container(child: Card(child: Text('Unit 5'))),
              Container(child: Card(child: Text('Unit 6'))),
            ]),

working demo

enter image description here

full code

import 'package:flutter/material.dart';
import 'package:flutter_mobile_carousel/carousel.dart';
import 'package:flutter_mobile_carousel/carousel_arrow.dart';
import 'package:flutter_mobile_carousel/default_carousel_item.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Carousel(rowCount: 4, children: [
              Container(child: Card(child: Text('Unit 1'))),
              Container(child: Card(child: Text('Unit 2'))),
              Container(child: Card(child: Text('Unit 3'))),
              Container(child: Card(child: Text('Unit 4'))),
              Container(child: Card(child: Text('Unit 5'))),
              Container(child: Card(child: Text('Unit 6'))),
            ]),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
chunhunghan
  • 51,087
  • 5
  • 102
  • 120