Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I use switch statement to change listview colors #1

Open
ABINASH56 opened this issue Nov 6, 2019 · 4 comments
Open

How can I use switch statement to change listview colors #1

ABINASH56 opened this issue Nov 6, 2019 · 4 comments

Comments

@ABINASH56
Copy link

here is the code .i need help in switch case. I watched this video but I need more examples to completely understand switch case..

How can i use switch case to change bg color of listview .I want to add 10 different types of colors in listview


ListView.builder(
  itemBuilder: (BuildContext context, int index) {
    return Container(
      color: (index % 10 == 0) ? Colors.red : Colors.green,
      child: ListTile(
        title: ...
      ),
    );
  },
)
@cv692001
Copy link

cv692001 commented Aug 20, 2020

Hey @ABINASH56 you can follow up with the following code .I hope this might helps you 👍

ListView.builder(
itemBuilder: (BuildContext context, int index) {
return Container(
color: (index % 10 == 0) ? Colors.red : Colors.green,
child: ListTile(
title: Text(string),
onTap: () => setState(() => null ),
selected: null,
style: ListTileTheme(selectedColor: Colors.white,),
);
);
},
)

@cv692001
Copy link

Also I am new to contributing , cam you please help me out to contribute further :)

@cv692001
Copy link

cv692001 commented Sep 2, 2020

Hey @ABINASH56 , have you see it

@shatanikmahanty
Copy link

Hi @ABINASH56 I have a solution for you :

You can try this code out and see the output. This is how you can use function in dart to get Colors from switch

import 'package:flutter/material.dart';

main() {
  runApp(
    MaterialApp(
      home: SwitchColors(),
    ),
  );
}

class SwitchColors extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
        body: ListView.builder(
      itemCount: 5,
          shrinkWrap: true,
      itemBuilder: (BuildContext context, int index) {
        return Container(
            color: getColor(index),
            child: ListTile(
              title: Text("string")
            ));
      },
    ));
  }

  Color getColor(int index) {
    switch (index) {
      case 0:
        return Colors.amber;
      case 1:
        return Colors.blue;
      case 2: return Colors.green;
      case 3: return Colors.grey;
      case 4: return Colors.purple;
      default:
        return Colors.white;
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants