Skip to main content

Carousel in Flutter


Carousels can add a bonus point to user experience. With carousels you can show multiple elements of application in a reserved space on a single page. Carousels are like tiny community adding large impact on society. 

Flutter has special carousel package and we will see its use in another article. In this article we are going to see another method.

Prerequisties - 

1. Download any type of images you want to show on carousel. I have used cars images.

2. Create assets folder and add all images into it.

3. Add assets folder in pubspec.yaml file otherwise it may cause errors.

Let's see Flutter code for Carousel - 

import 'package:flutter/material.dart'; import 'package:carousel_widget/carousel_widget.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Carousel Theme',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context){
initializeData();
return Scaffold(
backgroundColor: Colors.white,
body: Carousel(
listViews:[
Fragment(
child: getScreen(0),
),
Fragment(
child: getScreen(1),
),
Fragment(
child: getScreen(2),
),
Fragment(
child: getScreen(3),
),
Fragment(
child: getScreen(4),
),
],
),
);
}
Widget getScreen(index){
return ListView(
children: <Widget>[
Container(
height: 300,
width:900,
margin: const EdgeInsets.fromLTRB(1.0, 90.0, 1.0, 0.0),
child: Image.asset(imageNames.elementAt(index),
),
),
Container(
height: 45.0,
margin: const EdgeInsets.fromLTRB(50.0, 12.0, 50.0, 0.0),
child: Text(description.elementAt(index), textAlign: TextAlign.center,overflow: TextOverflow.ellipsis,maxLines: 5,style: TextStyle(fontSize: 25),),
),
],
);
}
List<String> titles=List();
List<String> description=List();
List<String> imageNames=List();
void initializeData(){
titles.add("car");
description.add("My favorite car");
imageNames.add("assets/b1.jpg");
titles.add("car");
description.add("My favorite car");
imageNames.add("assets/b2.jpg");
titles.add("car");
description.add("My favorite car");
imageNames.add("assets/b3.jpg");
titles.add("car");
description.add("My favorite car");
imageNames.add("assets/b4.jpg");
}
}

 

This is the output we are going to get - 


                                         






Comments

Popular posts from this blog

List of Questions asked by Morgan Stanley

  Coding Questions -  1.  .   Exchange kth node from start and kth node from end of a singly linked list. 2.    Given a number, you have to find next greater number which has same set of digits. 3.    Given a binary tree, you have to print level order traversal of the tree (left child then right child) but every next level has to be printed in next line. 4.    Longest Common Substring 5.    Spiral level order traversal 6.    Add two linked lists 7.    Write a function to find the mirror image of binary tree 8.    WAP to find the character which occurred maximum times in the character array 9.  Given a 2d matrix find an element in a matrix which is 0 and make the entire row and column to 0 10.  Find the minimum element in the rotated array of integers 11.  Find highest length substring such that there are equal number of 0’s and 1’sin array of 1’s and 0’s only 12.  Given a dictio...

Which is best: LeetCode vs HackerEarth ?

  Well, all the platforms almost are same except the interface. That's what I think. To conclude the result, let's compare them through the features they offer.  Features of Leetcode -  Topic - wise questions Comapny-wise questions Weekly contests Interview Preparation section Discuss section Features of HackerEarth Topic-wise problems Monthly contests Discuss section Hiring Contests Hackathons Earlier, when I started coding in my first year. I was fond of HackerEarth. But when I stoped using hackerearth and shifted to Leetcode, I like it more. It depends upon how much comfortable you are with the interface. But, it can't be denied that Leetcode offer more than HackerEarth. But for the coders who love hackathons, HackerEarth is best for them and for the people looking out for job opportunities. Do share your favorite through comments!

Top Questions asked By Walmart

  Discuss in detail what happens when you write a code and compile it. What is multithreading? State any C++ multithreading framework you might have worked with. Explain encapsulation, abstraction, polymorphism. Explain inheritance. What is the Diamond problem? How memory is allocated in C++. What is malloc? Differences from calloc? What is a microprocessor? Explain microservices architecture. Explain normalization and all their forms. Differences between inner and outer joins. DFS vs. BFS What is the heap? Detect a loop in the linked list. If a loop exists, return the start node of the loop. Find the third most frequent element in an array. Finding n next permutations using some specific set of digits given.   Find the number of ways of reaching from top left to bottom right of a matrix.   Minimum number of jumps to reach end given a number N. print in how many ways it can be represented as N = a+b+c+d , 1< =a< =b< =c< = d; 1<=N< = 5000  given two ...