How to select radio button when tapped on the container? - flutter

Basically, there is a row with two containers with the child as radio buttons. The containers change colour according to the selected radio button but I want the containers to change colours by tapping on them and not exactly by tapping the radio button.
Code:
Container(
height: 60,
width: 130,
decoration: BoxDecoration(
color: _radioBtnVal == 'manual' ? AMBER : WHITE,
borderRadius: BorderRadius.circular(15)),
child: Row(
children: [
Radio<String>(
//activeColor: WHITE,
activeColor: Colors.red,
value: "manual",
groupValue: _radioBtnVal,
onChanged: _handleChange),
Text(
"Manual",
style: TextStyle(
color: _radioBtnVal == 'manual' ? WHITE : BLACK,
fontWeight: FontWeight.bold),
),
],
),
),
SizedBox(width: 80),
Container(
height: 60,
width: 130,
decoration: BoxDecoration(
color: _radioBtnVal == 'video' ? AMBER : WHITE,
borderRadius: BorderRadius.circular(15)),
child: Row(
children: [
Radio<String>(
//activeColor: WHITE,
activeColor: Colors.red,
value: "video",
groupValue: _radioBtnVal,
onChanged: _handleChange),
Text(
"Video Call",
style: TextStyle(
color: _radioBtnVal == 'video' ? WHITE : BLACK,
fontWeight: FontWeight.bold),
),
],
),
),
Here is an image of what the output looks like.
Output:

You have to wrap the containers with gesture detectors and every time the container is tapped you have to pass a value to the function that handles the radio buttons and setstate. I have updated your code to get the desired output.
main.dart
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(debugShowCheckedModeBanner: false, home: RadioTest()));
}
class RadioTest extends StatefulWidget {
const RadioTest({
Key? key,
}) : super(key: key);
#override
State<RadioTest> createState() => _RadioTestState();
}
class _RadioTestState extends State<RadioTest> {
String _radioBtnVal = "";
_handleChange(String? value) {
setState(() {
_radioBtnVal = value.toString();
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Row(
children: [
GestureDetector(
onTap: () {
_handleChange('manual');
},
child: Container(
height: 60,
width: 130,
decoration: BoxDecoration(
color: _radioBtnVal == 'manual' ? Colors.amber : Colors.white,
borderRadius: BorderRadius.circular(15)),
child: Row(
children: [
Radio<String>(
//activeColor: WHITE,
activeColor: Colors.red,
value: "manual",
groupValue: _radioBtnVal,
onChanged: _handleChange),
Text(
"Manual",
style: TextStyle(
color: _radioBtnVal == 'manual'
? Colors.white
: Colors.black,
fontWeight: FontWeight.bold),
),
],
),
),
),
const SizedBox(width: 80),
GestureDetector(
onTap: () {
_handleChange('video');
},
child: Container(
height: 60,
width: 130,
decoration: BoxDecoration(
color: _radioBtnVal == 'video' ? Colors.amber : Colors.white,
borderRadius: BorderRadius.circular(15)),
child: Row(
children: [
Radio<String>(
//activeColor: WHITE,
activeColor: Colors.red,
value: "video",
groupValue: _radioBtnVal,
onChanged: _handleChange),
Text(
"Video Call",
style: TextStyle(
color: _radioBtnVal == 'video'
? Colors.white
: Colors.black,
fontWeight: FontWeight.bold),
),
],
),
),
),
],
),
);
}
}

Related

How to add text inside cupertino switch flutter

I want to add text inside cupertino switch like attached image.. Is there a way to do it?
As of today there is no way to customize the CupertinoSwitch in Flutter out of the box, but hey! there are a number of Plugins in pub.dev that could satisfy your needs, like flutter_switch.
With the following code you can achieve something like what you want:
FlutterSwitch(
showOnOff: true,
value: v,
activeIcon: Text("SELL"),
activeText: "BUY",
inactiveIcon: Text("BUY"),
inactiveText: "SELL",
inactiveColor: Colors.blue,
activeTextFontWeight: FontWeight.normal,
inactiveTextFontWeight: FontWeight.normal,
onToggle: (val) {
setState(() {
v = val;
});
},
)
which looks like this:
That is of course just a sample, you can further customize to get a more beautiful result.
I create custom widget but without cupertinoswitch animation in it. I hope this match your needs =))
GestureDetector(
onTap: () {
setState(() {
val = !val;
});
},
child: Container(
width: size.width * 0.35,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30),
color: kSecondaryColor),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Container(
width: 60,
height: 30,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(30),
color: val
? Colors.white
: kSecondaryColor),
child: Center(
child: Text(
'BUY',
style: TextStyle(
fontWeight: FontWeight.bold,
color: val
? Colors.black
: Colors.white),
)),
),
Container(
width: 60,
height: 30,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(30),
color: val
? kSecondaryColor
: Colors.white),
child: Center(
child: Text(
'SELL',
style: TextStyle(
fontWeight: FontWeight.bold,
color: val
? Colors.white
: Colors.black),
)),
),
],
),
),
),
),
add text inside cupertinoswitch flutter
customSwitcher: CupertinoSwitch(
value: model.enableBiometric == 'enable' ? true: false,
activeColor: Color.transparent,
trackColor: Color.transparent,
onChanged: (bool val) {
model.enableBiometric =
(model.enableBiometric == "disable"
? 'enable'
: 'disable');
},),
Container(
width: 70,
height: 36,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular((100)),
border: Border.all(
width: 1.0,
color: model?.enableBiometric == 'enable'
? Color.red
: Color.red,
),
color: Color.red),
child: Row(children: [
if (model?.enableBiometric == "enable")...[
Padding(
padding: EdgeInsets.only(left:model?.enableBiometric == "enable" ? 12 : 0),
child: AppText(
txt:model?.enableBiometric == "enable" ? "Yes" : "",
fontSize: 11,
fontWeight:800,
),
),
] else ...[
SizedBox(
width: 4.0.w,
),
],
Expanded(
child: Container(
// width: ch(12),
//color: AppColor.red,
child: Transform.scale(
transformHitTests: false,
scale: 1.0,
child: customSwitcher!,
),
)),
if (model?.enableBiometric == "disable") ...[
Padding(
padding: EdgeInsets.only(
right:
model?.enableBiometric == "disable" ? 12 : 0),
child: AppText(
txt:
model?.enableBiometric == "disable" ? "No" : "",
fontSize: 11,
fontWeight: 800,
),
),
] else ...[
SizedBox(
width: 4.0.w,
),
]
]),
),

How to change bottom navigation background color in Flutter

I download a project from Github and currently, it's using the Persistent bottom nav bar. I'm just wondering how can I change the navigation bar background color if a user select either Light mode or Dark mode from the Account setting screen. Any help or suggestion will be really appreciated.
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
body: PersistentTabView(
context,
controller: _controller,
confineInSafeArea: true,
backgroundColor: Color(0xFFE9E9E9), //I have tried removing this but still not working
handleAndroidBackButtonPress: true,
resizeToAvoidBottomInset: true,
stateManagement: true,
navBarStyle: NavBarStyle.simple,
items: _navBarsItems(),
screens: [
Container(
color: Colors.white,
height: MediaQuery.of(context).size.height,
child: Home()),
Container(
color: Colors.white,
height: MediaQuery.of(context).size.height,
child: Categories()),
Container(
color: Colors.white,
height: MediaQuery.of(context).size.height,
child: Search()),
Container(
color: Colors.white,
height: MediaQuery.of(context).size.height,
child: Bookmarks()),
Container(
color: Colors.white,
height: MediaQuery.of(context).size.height,
child: Account()),
],
),
);
}
Account.dart
class ColorModal extends HookWidget {
#override
Widget build(BuildContext context) {
final color = useProvider(colorProvider);
changeColor(String cl) async {
color.state = cl;
var box = await Hive.openBox('appBox');
box.put('color', cl);
Navigator.pop(context);
}
return SingleChildScrollView(
controller: ModalScrollController.of(context),
child: Container(
decoration: BoxDecoration(
color: color.state == 'dark' ? eachPostBgDark : Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5), topRight: Radius.circular(5))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.only(left: 30.0, top: 30, bottom: 20),
child: Text("Color Preference",
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.w500,
color: color.state == 'dark'
? Color(0xFFE9E9E9)
: Colors.black)),
),
Container(
margin: EdgeInsets.only(
bottom: 40,
left: 20,
right: 20,
),
decoration: BoxDecoration(
color: color.state == 'dark'
? Colors.black.withOpacity(0.4)
: Colors.black.withOpacity(0.06),
borderRadius: BorderRadius.circular(5)),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: new Icon(
color.state == 'light'
? Icons.check_circle
: Icons.radio_button_unchecked,
color: color.state == 'light'
? colorPrimary
: color.state == 'dark'
? Color(0xFFA7A9AC)
: Colors.black,
),
title: new Text("Light Mode",
style: TextStyle(
color: color.state == 'dark'
? Color(0xFFA7A9AC)
: Colors.black)),
onTap: () {
changeColor('light');
},
),
ListTile(
leading: new Icon(
color.state == 'dark'
? Icons.check_circle
: Icons.radio_button_unchecked,
color: color.state == 'dark'
? colorPrimary
: color.state == 'dark'
? Color(0xFFA7A9AC)
: Colors.black,
),
title: new Text("Dark Mode",
style: TextStyle(
color: color.state == 'dark'
? Color(0xFFA7A9AC)
: Colors.black)),
onTap: () {
changeColor('dark');
},
),
],
),
),
],
),
),
);
}
}
Assign here some another color like-
backgroundColor: Color(0xFFFFFFFF),
and check, if it works.

Flutter DropdownButton doesn't update in StatefulBuilder

I am using List items for DropdownButton widget in StatefulBuilder. When I select any item on DropdownButton its not updating at this stage. But when I close the showModalBottomSheet widget and open again its updating. How can I fix this? Thanks for help.
This is my menu code for items
List<DropdownMenuItem<String>> tur = [];
void kategoritur(){
tur = [];
tur.add(new DropdownMenuItem(
child: Row(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/g.png'),
fit: BoxFit.fitWidth,
),
),
height: 50, width: 50,
),
SizedBox(width: 30,),
new Text("Şırınga"),
],
),
value: "Siringa",)
);
tur.add(new DropdownMenuItem(
child: Row(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/t.png'),
fit: BoxFit.fitWidth,
),
),
height: 50, width: 50,
),
SizedBox(width: 30,),
new Text("Hap"),
],
),
value: "Hap",)
);
tur.add(new DropdownMenuItem(
child: Row(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/f.png'),
fit: BoxFit.fitWidth,
),
),
height: 50, width: 50,
),
SizedBox(width: 30,),
new Text("Şişe"),
],
),
value: "Sise",)
);
tur.add(new DropdownMenuItem(
child: Row(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/e.png'),
fit: BoxFit.fitWidth,
),
),
height: 50, width: 50,
),
SizedBox(width: 30,),
new Text("Damla"),
],
),
value: "Damla",)
);
}
This is my main code
showModalBottomSheet(
useRootNavigator: true,
context: context,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(24),
),
),
builder: (context) {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: StatefulBuilder(
builder: (context, setModalState) {
return Container(
padding: const EdgeInsets.all(32),
child: Column(
children: [
FlatButton(
onPressed: () async {
var selectedTime =
await showTimePicker(
builder: (BuildContext context, Widget child) {
return MediaQuery(
data: MediaQuery.of(context)
.copyWith(alwaysUse24HourFormat: false),
child: Theme(
data: ThemeData.dark().copyWith(
colorScheme: ColorScheme.dark(
primary: Color(0xFF52b788),
onPrimary: Color(0xFF52b788),
surface: Colors.white,
onSurface: Color(0xFF52b788),
),
dialogBackgroundColor:
Color(0xFF52b788),
),
child: child),
);
},
context: context,
initialTime:
TimeOfDay.now(),
);
if (selectedTime != null) {
final now = DateTime.now();
var selectedDateTime =
DateTime(
now.year,
now.month,
now.day,
selectedTime.hour,
selectedTime
.minute);
_alarmTime =
selectedDateTime;
setModalState(() {
_alarmTimeString =
DateFormat('HH:mm')
.format(
selectedDateTime);
});
}
},
child: Text(
_alarmTimeString,
style:
TextStyle(fontSize: 32),
),
),
Container(
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.black38,
borderRadius: BorderRadius.circular(18)
),
width: _width,
padding: EdgeInsets.fromLTRB(14, 0, 14, 0),
child: DropdownButton(
style: GoogleFonts.montserrat(
color: Colors.white,
fontSize: 16,
textStyle: TextStyle(fontWeight:
FontWeight.w300)),
dropdownColor: Colors.cyan[700],
underline: SizedBox.shrink(),
items: tur,
onChanged: (value){
turdata = value;
setState(() {
});
},
hint: new Text("İlaç Türü", textAlign:
TextAlign.center
,
style: GoogleFonts.montserrat(
color: Colors.white,
fontSize: 16,
textStyle: TextStyle(fontWeight:
FontWeight.w300)),
),
value: turdata,
),
),
SizedBox(height: 20,),
FloatingActionButton.extended(
backgroundColor: Color(0xFF52b788),
onPressed: onSaveAlarm,
icon: Icon(Icons.alarm),
label: Text('Save'),
),
],
),
);
},
),
);
},
);
i made a custom class of dropdown you can customize this in your own way
import 'package:flutter/material.dart';
import 'package:rio/Utils/utils.dart';
import 'package:rio/widgets/widgets.dart';
import 'package:rio/generated/l10n.dart';
import 'package:responsive_flutter/responsive_flutter.dart';
import 'color.dart';
// ignore: must_be_immutable
class DropDownClass extends StatelessWidget {
var _hint;
var _val;
double _fontSize;
List _list = new List();
bool _icon;
bool _border;
Color _underLineColor, _dropDownColor, _textColor;
List get list => _list;
dynamic Function(dynamic) _listener;
DropDownClass({List list,
var hint,
Color underLineColor,
Color dropDownColor,
Color textColor,
double fontSize,
bool icon,
var val,
int type,
bool border = true,
dynamic Function(dynamic) listener,})
: _list = list,
_hint = hint,
_underLineColor = underLineColor,
_dropDownColor = dropDownColor,
_textColor = textColor,
_icon = icon,
_val = val,
_fontSize = fontSize,
_border = border,
_listener = listener;
#override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
Utils.selectedValue = null;
},
child: DropdownButtonHideUnderline(
child: DropdownButtonFormField<String>(
value: _val,
dropdownColor: _dropDownColor ?? CustomColors.white,
decoration:_border == true? InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: _underLineColor ?? Theme
.of(context)
.hintColor,
width: 1.0,
),
)
):InputDecoration(
border: InputBorder.none,
fillColor: Theme.of(context).cardColor,
filled: true),
style: robotoStyle(context,
color: _textColor ??
Theme
.of(context)
.primaryTextTheme
.bodyText1
.color,
fontWeight: FontWeight.w400,
fontSize: _icon == null || _icon == false
? ResponsiveFlutter.of(context).fontSize(_fontSize??1.5)
: ResponsiveFlutter.of(context).fontSize(_fontSize??1.5)),
isExpanded: true,
icon: _icon == null || _icon == false
? null
: Icon(
Icons.keyboard_arrow_down,
color: CustomColors.appBarTextColor,
),
hint: Text(_hint,
style: robotoStyle(context,
color: _icon == null || _icon == false
? Theme
.of(context)
.hintColor
: Theme
.of(context)
.textSelectionColor,
fontWeight: FontWeight.w400,
fontSize: _icon == null || _icon == false
? ResponsiveFlutter.of(context)
.fontSize(_fontSize??1.75)
: ResponsiveFlutter.of(context).fontSize(_fontSize??1.5))),
items: list.map((item) {
return DropdownMenuItem<String>(
value: item,
child: new Text(item,
style: robotoStyle(context,
color: _textColor ?? CustomColors.dropDownTextColor,
fontWeight: FontWeight.w400,
fontSize: _icon == null || _icon == false
? ResponsiveFlutter.of(context)
.fontSize(_fontSize??1.75)
: ResponsiveFlutter.of(context).fontSize(_fontSize??1.5))),
);
}).toList(),
onChanged: (value) {
_val = value;
if (_listener != null) _listener.call(value);
if (_icon == true) {
Utils.selectedValue = value;
S.load(Locale(value));
Utils.preferences.setString("lang", value);
}
// return val;
},
),
),
);
}
}

How to change the color of text of a list of Cards dynamically in flutter?

I want to change the colors of those status texts(i.e. Pending, Cancel and Visited) as follows:
Pending --> as Lightgreen
Visited --> as Green
Cancel --> as Red
How should I do that ? Please help me doing this as I am new to flutter
Here is my code:
class AdminHomeContent extends StatefulWidget {
#override
_AdminHomeContentState createState() => _AdminHomeContentState();
}
class _AdminHomeContentState extends State<AdminHomeContent> {
final List<Patient> patients = [
Patient('Person A', 'https://images.unsplash.com/photo-1545996124-0501ebae84d0?ixid=MXwxMjA3fDB8MHxzZWFyY2h8OHx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80',
8, 2, 'Pending', '10-08-2015', true),
Patient('Person B', 'https://images.unsplash.com/photo-1544005313-94ddf0286df2?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MTF8fGh1bWFufGVufDB8fDB8&ixlib=rb-1.2.1&w=1000&q=80',
8, 5, 'Cancel', '23-12-2019', false),
Patient('Person C', 'https://images.unsplash.com/photo-1554151228-14d9def656e4?ixid=MXwxMjA3fDB8MHxzZWFyY2h8NHx8aHVtYW58ZW58MHx8MHw%3D&ixlib=rb-1.2.1&w=1000&q=80',
8, 7, 'Visited', '01-02-2019', false),
Patient('Person D', 'https://upload.wikimedia.org/wikipedia/commons/e/ec/Woman_7.jpg',
8, 4, 'Pending', '20-09-2018', true),
Patient('Person E', 'https://cdn.pixabay.com/photo/2017/08/07/14/15/portrait-2604283__340.jpg',
8, 6, 'Visited', '28-04-2017', false)
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: aapBarSection('Today\'s Appointments' , Colors.blueAccent[700], context),
body: ListView.builder(
itemCount: patients.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(9.0),
child: SizedBox(
height: 120,
child: Card(
elevation: 5.0,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: Row(
children: [
Expanded(
flex: 3,
child: Container(
child: CircleAvatar(
backgroundImage: NetworkImage(patients[index].imgPath),
radius: 50.0,
),
),
),
Expanded(
flex: 5,
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(patients[index].name, style: TextStyle(
fontSize: 23.0,
fontWeight: FontWeight.bold,
color: Colors.black87
),),
SizedBox(
height: 20,
),
Text(patients[index].completedSession.toString() +'/'+ patients[index].totalSession.toString(),
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
color: Colors.black54
),),
],
),
),
),
Expanded(
flex: 2,
child: Container(
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Container(
height: 10,
width: 10,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.lightGreen,
),
),
SizedBox(
width: 8.0,
),
Text(patients[index].status,
style: TextStyle(
fontSize: 10.0,
fontWeight: FontWeight.bold,
color: Colors.lightGreen
),
),
],
),
),
),
],
),
),
),
),
),
);
},
),
);
}
}
I want to change the colors of those texts as well as those dots(i.e. both dots and texts must be of same colors)
I have taken container to create those dots.
And here is my model class from which the fields are coming:
patient.dart:-
class Patient {
String name ;
String imgPath ;
int totalSession ;
int completedSession ;
String status ;
String dob ;
bool isActive ;
Patient(this.name, this.imgPath,this.totalSession,this.completedSession,this.status,this.dob,this.isActive);
}
Create a function that returns the color based on the status. Thus making the code concise. and it is always a good practice to write functions and reuse it
Use this function.
Color getDynamicColor(String status) {
if (status == "Pending") {
return Colors.lightGreen;
}
if (status == "Visited") {
return Colors.green;
}
if (status == "Cancel") {
return Colors.red;
}
return Colors.black; //default color.
}
and use this in your text widget as such.
Text(patients[index].status,
style: TextStyle(
fontSize: 10.0,
fontWeight: FontWeight.bold,
color: getDynamicColor(patients[index].status),
),
Container(
height: 10,
width: 10,
decoration: BoxDecoration(
shape: BoxShape.circle,
color:getDynamicColor(patients[index].status),
),
),
You can add a method to your class and use it as you wish:
class Patient {
....
Color get color {
return status == 'Pending'
? Colors.lightGreen
: status == 'Visited'
? Colors.green
: Colors.red;
}
}
You can use ternary operator.
Text(patients[index].status,
style: TextStyle(fontSize: 10.0,
fontWeight: FontWeight.bold, color: patients[index].status == "Pending" ?
Colors.lightGreen ? patients[index].status == "Visited" ? Colors.green : Colors.red))
Apply the same changes for dot too.
color: patients[index].status == "Pending"
? Colors.lightGreen
: patients[index].status == "Visited"
? Colors.green
: Colors.red,
color: patients[index].status == "Pending"
? Colors.lightGreen // if pending is true
: patients[index].status == "Visited" //if pending is false
? Colors.green // if visited is true
: Colors.red, // if visited is false, and default

How to get value from a list of values in flutter

I have three containers with different information(Strings and Icon) in each and that I want to put in a column.
I want to get the value of the clicked one and want to change the color of the container when clicked, how can I do that, please?
Also, I want to get the value of the strings selected
I think it's simple but I seem not to find a way to do that.
This is my Container widget:
class VisitTypeFees extends StatelessWidget {
const VisitTypeFees({
Key key,
this.onSelected = false,
this.title,
this.subTitle,
this.amount,
this.icon,
}) : super(key: key);
final bool onSelected;
final String title, subTitle, amount;
final IconData icon;
#override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
height: getProportionateScreenHeight(65),
width: SizeConfig.screenWidth - 20,
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(10),
vertical: getProportionateScreenWidth(7.5),
),
decoration: BoxDecoration(
color: onSelected ? kSecondaryColor : Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [kDefaultShadow]
),
child: Row(
children: [
Container(
height: getProportionateScreenHeight(50),
width: getProportionateScreenHeight(50),
decoration: BoxDecoration(
color: onSelected
? Colors.white
: kPrimaryLightColor.withOpacity(0.6),
borderRadius: BorderRadius.circular(10)),
child: Icon(
icon,
color: onSelected ? kSecondaryColor : kPrimaryColor,
),
),
SizedBox(
width: getProportionateScreenWidth(7.5),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
title,
style: TextStyle(
color: onSelected ? Colors.white : Colors.black,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.w600),
),
Text(
subTitle,
style: TextStyle(
color: onSelected ? Colors.white : kTextColor,
fontSize: getProportionateScreenWidth(12),
fontWeight: FontWeight.w500),
),
],
),
Spacer(),
Text(
amount,
style: TextStyle(
color: onSelected ? Colors.white : kPrimaryColor,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.bold),
),
],
),
),
);
}
}
At first, you need to convert your widget to a StatefulWidget. Then, you need to wrap with InkWell widget for each Text and Icon widget. And they will all have isSelected properties themselves.
It will look like this:
class VisitTypeFees extends StatefulWidget {
const VisitTypeFees({
Key key,
this.onSelected = false,
this.title,
this.subTitle,
this.amount,
this.icon,
}) : super(key: key);
final bool onSelected;
final String title, subTitle, amount;
final IconData icon;
#override
_VisitTypeFeesState createState() => _VisitTypeFeesState();
}
class _VisitTypeFeesState extends State<VisitTypeFees> {
bool iconSelected;
bool firstTextSelected;
bool seccondTextSelected;
#override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
height: getProportionateScreenHeight(65),
width: SizeConfig.screenWidth - 20,
padding: EdgeInsets.symmetric(
horizontal: getProportionateScreenWidth(10),
vertical: getProportionateScreenWidth(7.5),
),
decoration: BoxDecoration(
color: widget.onSelected ? kSecondaryColor : Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [kDefaultShadow]),
child: Row(
children: [
InkWell(
onTap: () {
// icon selected!
setState(() {
iconSelected = true;
firstTextSelected = false;
seccondTextSelected = false;
});
},
child: Container(
height: getProportionateScreenHeight(50),
width: getProportionateScreenHeight(50),
decoration: BoxDecoration(
color: widget.onSelected
? Colors.white
: kPrimaryLightColor.withOpacity(0.6),
borderRadius: BorderRadius.circular(10)),
child: Icon(
widget.icon,
color: widget.onSelected ? kSecondaryColor : kPrimaryColor,
),
),
),
SizedBox(
width: getProportionateScreenWidth(7.5),
),
InkWell(
onTap: () {
// first text - title and subtitle selected!
setState(() {
iconSelected = false;
firstTextSelected = true;
seccondTextSelected = false;
});
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
widget.title,
style: TextStyle(
color: widget.onSelected ? Colors.white : Colors.black,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.w600),
),
Text(
widget.subTitle,
style: TextStyle(
color: widget.onSelected ? Colors.white : kTextColor,
fontSize: getProportionateScreenWidth(12),
fontWeight: FontWeight.w500),
),
],
),
),
Spacer(),
InkWell(
onTap: () {
// second text - amount selected.
setState(() {
iconSelected = false;
firstTextSelected = false;
seccondTextSelected = true;
});
},
child: Text(
widget.amount,
style: TextStyle(
color: widget.onSelected ? Colors.white : kPrimaryColor,
fontSize: getProportionateScreenWidth(15),
fontWeight: FontWeight.bold),
),
),
],
),
),
);
}
}