Skip to content

Instantly share code, notes, and snippets.

@MeshkaniMohammad
Last active March 10, 2020 21:29
Show Gist options
  • Save MeshkaniMohammad/e3979830594c13867574c2cb83a299d1 to your computer and use it in GitHub Desktop.
Save MeshkaniMohammad/e3979830594c13867574c2cb83a299d1 to your computer and use it in GitHub Desktop.
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: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>{
int selectedOption = 0;
@override
Widget build(BuildContext context) {
final List<Item> items = [
Item(title: "fruits",isSelected: selectedOption == 0 ? true : false,onTap: (){
setState(
(){
selectedOption = 0;
});
}),
Item(title: "water",isSelected: selectedOption == 1 ? true : false,onTap: (){
setState(
(){
selectedOption = 1;
});
}),
Item(title: "foods",isSelected: selectedOption == 2 ? true : false,onTap: (){
setState(
(){
selectedOption = 2;
});
}),
Item(title: "cakes",isSelected: selectedOption == 3 ? true : false,onTap: (){
setState(
(){
selectedOption = 3;
});
}),
];
return Scaffold(
body:Column(
children: [
Row(
children: <Widget>[
...items.map((item){
return Padding(
padding: EdgeInsets.all(10),
child: item);
})
]
),
if(selectedOption == 0)
Text("0"),
if(selectedOption == 1)
Text("1"),
if(selectedOption == 2)
Text("2"),
if(selectedOption == 3)
Text("3")
]));
}
}
class Item extends StatelessWidget {
final bool isSelected;
final VoidCallback onTap;
final String title;
const Item({Key key, this.isSelected, this.onTap,this.title}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
InkWell(
borderRadius: BorderRadius.all(Radius.circular(10)),
onTap:onTap,radius: 32,
child:Container(
height: 50,width: 50,
decoration:BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10)),
shape: BoxShape.rectangle,
color:isSelected ? Colors.amberAccent : Colors.red,
))
),
Text(title),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment