alpharat
alpharat

Reputation: 1

How to make the center of carousel slider bigger than the others?

I want to make the center Carousel Bigger than the others

      CarouselSlider(
      items: generateImageTiles(),
      options: CarouselOptions(
        enlargeCenterPage: true,
        aspectRatio: 16 / 5,
        viewportFraction: 0.4,
        reverse: false,
        initialPage: _current,
        onPageChanged: (index, other) {
          setState(() {
            _current = index;
            pizza = images[_current];
            price = prices[_current];
            name = names[_current];      
          });
        },
      ),
    ),

This is what i'm trying to achieve APP UI

Upvotes: 0

Views: 2470

Answers (2)

Umashankar Das
Umashankar Das

Reputation: 601

You need to add an attrbute in options called as

enlargeCenterPage: true,

viewportFraction: 0.8,

That will do it for you. Also make the viewportfraction less than 1. That will make side cards visible.

Upvotes: 0

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63604

On CarouselOptions, viewportFraction is responsible for making the center widget large/small. It can be >0.0 and <=1.0. If you wish to change aspectRatio, play with aspectRatio on CarouselOptions.

If you find Ui is not changing, do flutter clean and run again.

enter image description here

Full Widget:

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';

class CurS extends StatefulWidget {
  const CurS({Key? key}) : super(key: key);

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

class _CurSState extends State<CurS> {
  int _current = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CarouselSlider(
        items: [1, 2, 3, 4, 5].map((i) {
          return Builder(
            builder: (BuildContext context) {
              return Container(
                width: MediaQuery.of(context).size.width,
                margin: EdgeInsets.symmetric(horizontal: 5.0),
                decoration: BoxDecoration(color: Colors.amber),
                child: Text(
                  'text $i',
                  style: TextStyle(fontSize: 16.0),
                ),
              );
            },
          );
        }).toList(),
        options: CarouselOptions(
          enlargeCenterPage: true,
          aspectRatio: 16 / 5,
          viewportFraction: .8,
          reverse: false,
          initialPage: _current,
          onPageChanged: (index, other) {
            setState(() {
              _current = index;
            });
          },
        ),
      ),
    );
  }
}

Upvotes: 1

Related Questions