Last active
February 16, 2020 17:08
-
-
Save maheshj01/132db523c9369a265d2614f8908693bb to your computer and use it in GitHub Desktop.
change color of a box in list of boxes on tap flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: Tabbed() | |
); | |
} | |
} | |
class Tabbed extends StatefulWidget { | |
@override | |
_TabbedState createState() => _TabbedState(); | |
} | |
class _TabbedState extends State<Tabbed> { | |
Color color = Colors.green; | |
@override | |
void initState() { | |
for(int i=0;i<listLength;i++){ | |
list1.add( | |
TabModel(isTapped: false)); // assigns initial value to false | |
} | |
for(int i=0;i<listLength;i++){ | |
list2.add( | |
TabModel(isTapped: false)); // assigns initial value to false | |
} | |
} | |
Widget column1(){ | |
return Column( | |
children: List.generate( | |
listLength, | |
(index) =>GestureDetector( | |
onTap: (){ | |
// this selects only one out of many and disables rest | |
for(int i=0;i<list1.length;i++){ | |
if(i!=index){ | |
list1[i].isTapped=false; | |
} | |
}; | |
setState((){ | |
list1[index].isTapped = true; | |
}); | |
}, | |
child:Container( | |
margin:EdgeInsets.all(5), | |
color: list1[index].isTapped? Colors.red:color, | |
height:100, | |
width:100 | |
)) | |
)); | |
} | |
Widget column2(){ | |
return Column( | |
children: List.generate( | |
listLength, | |
(index) =>GestureDetector( | |
onTap: (){ | |
setState((){ | |
list2[index].isTapped = !list2[index].isTapped; | |
}); | |
}, | |
child:Container( | |
margin:EdgeInsets.all(5), | |
color: list2[index].isTapped? Colors.red:color, | |
height:100, | |
width:100 | |
)) | |
)); | |
} | |
List<TabModel> list1 =[]; | |
List<TabModel> list2 =[]; | |
int listLength=5; | |
@override | |
Widget build(BuildContext context) { | |
return Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
column1(), | |
column2() | |
],); | |
} | |
} | |
class TabModel{ | |
bool isTapped = false; | |
TabModel({this.isTapped}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment