How to add text inside cupertino switch flutter - 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,
),
]
]),
),

Related

why animatedAlign's animation does not perform when i change my valueNotifier from a alert dialog? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 days ago.
This post was edited and submitted for review 6 days ago.
Improve this question
can someone help me with my problem?
I have a animatedAlign that works like a switch
ValueListenableBuilder(
valueListenable: _authenticatorSwitch,
builder: (context, value, _) {
return Ink(
height: 20,
width: 42,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(35),
color: (_authenticatorSwitch.value) ? primary3 : Theme.of(context).colorScheme.neutral1,
border: Border.all(color: (_authenticatorSwitch.value) ? primary1 : Theme.of(context).colorScheme.neutral4, width: 0.5),
),
padding: const EdgeInsets.all(3.0),
child: AnimatedAlign(
duration: const Duration(milliseconds: 200),
alignment: (_authenticatorSwitch.value) ? Alignment.centerLeft : Alignment.centerRight,
child: Ink(
height: 14,
width: 14,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: (_authenticatorSwitch.value) ? primary1 : Theme.of(context).colorScheme.neutral4,
),
),
),
);
},
),
and this is my alert dialog
InkWell(
splashColor: Colors.transparent,
onTap: () {
Sheet.modalSheet(
context,
Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
SvgPicture.asset(
'assets/icons/bulk/info_icon.svg',
color: primary1,
width: 65,
),
const SizedBox(height: 30),
Text(
'مطمئن هستید که می‌خواهید ورود دو مرحله‌ای را غیر فعال کنید؟',
style: Theme.of(context).textTheme.titleLarge?.copyWith(
color: Theme.of(context).colorScheme.textColor,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
Text(
'کاربر عزیز، برای حفظ امنیت بیشتر دارایی شما پیشنهاد می‌کنیم از غیر فعال کردن ورود دو مرحله‌ای صرف نظر کنید.',
style: Theme.of(context).textTheme.titleSmall?.copyWith(
color: Theme.of(context).colorScheme.neutral4,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
Row(
children: [
Expanded(
flex: 2,
child: Button(
onPressed: () {
Navigator.of(context).pop();
},
size: 'medium',
mode: 'neutral',
title: 'انصراف',
isExpanded: false,
isLoading: false,
),
),
const SizedBox(width: 8.0),
Expanded(
flex: 3,
child: Button(
onPressed: () {
Navigator.of(context).pop();
_authenticatorSwitch.value = !_authenticatorSwitch.value;
BlocProvider.of<AuthenticatorBloc>(context).add(SendEmail(widget.isActive));
},
size: 'medium',
mode: 'primary',
title: 'تایید',
isExpanded: true,
isLoading: false,
),
),
],
)
],
),
);
},
child: Row(
children: [
Text('سرویس احراز هویت گوگل', style: Theme.of(context).textTheme.titleSmall?.copyWith(color: Theme.of(context).colorScheme.textColor)),
const Spacer(),
ValueListenableBuilder(
valueListenable: _authenticatorSwitch,
builder: (context, value, _) {
return Ink(
height: 20,
width: 42,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(35),
color: (_authenticatorSwitch.value) ? primary3 : Theme.of(context).colorScheme.neutral1,
border: Border.all(color: (_authenticatorSwitch.value) ? primary1 : Theme.of(context).colorScheme.neutral4, width: 0.5),
),
padding: const EdgeInsets.all(3.0),
child: AnimatedAlign(
duration: const Duration(milliseconds: 200),
alignment: (_authenticatorSwitch.value) ? Alignment.centerLeft : Alignment.centerRight,
child: Ink(
height: 14,
width: 14,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: (_authenticatorSwitch.value) ? primary1 : Theme.of(context).colorScheme.neutral4,
),
),
),
);
},
),
],
),
)
The problem is when I open alert dialog and change the value of my notifier inside my alert dialog value changes but animation does not perform.
Consider when I change my value of my notifier outside of alert dialog animatedAlign perform perfectly.

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.

How to select radio button when tapped on the container?

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),
),
],
),
),
),
],
),
);
}
}

how to reduce the gap between this widgets? I tried tweaking padding values but no luck so far. Please take a look

This is the code
I tweaked the paading but no luck. I am new to flutter manage to build this but stuck, any suggestions/help is much appreciated!
import 'package:bmi_app_one/utils/TextStyle_Decorations.dart';
import 'package:bmi_app_one/utils/hexcolor.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class home_screen extends StatefulWidget {
#override
_home_screenState createState() => _home_screenState();
}
class _home_screenState extends State<home_screen> {
Color _appBar_bg_color = HexColor('#1C1C1E');
Color _app_bg_color = HexColor('#2c2c2e');
bool changeButtonColorMale = false;
bool changeButtonColorFemale = false;
int age = 20;
int weight = 30;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: homeScreenAppBarText,
backgroundColor: _appBar_bg_color,
centerTitle: true,
),
backgroundColor: _app_bg_color,
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
width: ScreenUtil().screenWidth,
height: 0.6.sh,
child: Row(
/* Entry Point of 1st set */
children: [
/* This contains both Male and female gender button */
Column(
children: [
Row(
children: [
/* Male Button */
Padding(
padding: const EdgeInsets.fromLTRB(8, 8, 4, 8),
child: InkWell(
onTap: (){
debugPrint("Gender: Male pressed");
setState(() {
if(changeButtonColorMale == false){
changeButtonColorMale = true;
changeButtonColorFemale = false;
}
else{
changeButtonColorMale = false;
}
});
},
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
child: Container(
width: 0.4.sw,
height: 0.2.sh,
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.1),
borderRadius: BorderRadius.circular(8.0)
),
margin: EdgeInsets.all(10),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(12.0),
child: Icon(Ionicons.ios_male,size: 88.ssp,
color: changeButtonColorMale ? Colors.yellow.withOpacity(0.7) : Colors.white),
),
Text(
"Male", style: TextStyle(
fontSize: 24.ssp,
fontFamily: 'San francisco',
color: changeButtonColorMale ? Colors.yellow.withOpacity(0.7) : Colors.white
),
),
],
),
),
),
),
/* Age */
Padding(
padding: const EdgeInsets.fromLTRB(0, 8, 8, 8),
child: Container(
width: 0.43.sw,
height: 0.2.sh,
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.1),
borderRadius: BorderRadius.circular(8.0)
),
child: Column(
children: [
/* Age Text */
Padding(
padding: const EdgeInsets.fromLTRB(8, 36, 8, 8),
child: Text("AGE",
style: TextStyle(
fontSize: 24.ssp,
fontFamily: 'San francisco',
color: Colors.white
),
),
),
Row(
children: [
/* Age decrease icon */
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: IconButton(
icon: new Icon(
MaterialCommunityIcons.minus_circle_outline,
size: 42.ssp,
color: Colors.white,),
onPressed: (){
debugPrint("Age decreased by 1");
setState(() {
if(age <= 5){
}
else{
age --;
}
});
},
splashRadius: 0.1,
),
),
/* Display Age in numbers */
Padding(
padding: const EdgeInsets.fromLTRB(15, 8 , 0, 0),
child: Text(age.toString(),
style: TextStyle(
color: Colors.white,
fontSize: 26.ssp,
fontFamily: 'San francisco',
),
),
),
/* Age increase icon */
Padding(
padding: const EdgeInsets.fromLTRB(3, 0, 0, 0),
child: IconButton(
icon: new Icon(
MaterialCommunityIcons.plus_circle_outline,
size: 42.ssp,
color: Colors.white,),
onPressed: (){
debugPrint("Age increased by 1");
setState(() {
if(age >= 90){
}
else{
age ++;
}
});
},
splashRadius: 0.1,
),
)
],
)
],
),
),
)
],
),
Row(
children: [
/* Female Button */
Padding(
padding: const EdgeInsets.fromLTRB(8, 0, 4, 0),
child: new InkWell(
onTap: (){
debugPrint("Gender: Female pressed");
setState(() {
if(changeButtonColorFemale == false){
changeButtonColorFemale = true;
changeButtonColorMale = false;
}
else{
changeButtonColorFemale = false;
}
});
},
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
child: Container(
width: 0.4.sw,
height: 0.2.sh,
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.1),
borderRadius: BorderRadius.circular(8.0)
),
margin: EdgeInsets.all(10.0),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(12.0),
child: Icon(Ionicons.ios_female,size: 88.ssp,
color: changeButtonColorFemale ? Colors.yellow.withOpacity(0.7) : Colors.white),
),
Text(
"Female", style: TextStyle(
fontSize: 24.ssp,
fontFamily: 'San francisco',
color: changeButtonColorFemale ? Colors.yellow.withOpacity(0.7) : Colors.white
),
),
],
),
),
),
),
/* Weight */
Padding(
padding: const EdgeInsets.fromLTRB(0, 8, 8, 8),
child: Container(
width: 0.43.sw,
height: 0.2.sh,
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.1),
borderRadius: BorderRadius.circular(8.0)
),
child: Column(
children: [
/* Weight Text */
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(24, 36, 3, 8),
child: Text("WEIGHT",
style: TextStyle(
fontSize: 24.ssp,
fontFamily: 'San francisco',
color: Colors.white
),
),
),
Text("(Kg)",
style: TextStyle(
color: Colors.white.withOpacity(0.7),
fontSize: 11.ssp,
fontFamily: 'San francisco',
),
),
],
),
Row(
children: [
/* Weight decrease icon */
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: IconButton(
icon: new Icon(
MaterialCommunityIcons.minus_circle_outline,
size: 42.ssp,
color: Colors.white,),
onPressed: (){
debugPrint("Weight decreased by 1");
setState(() {
if(weight <= 5){
}
else{
weight --;
}
});
},
splashRadius: 0.1,
),
),
/* Display Weight in numbers */
Padding(
padding: const EdgeInsets.fromLTRB(15, 8 , 0, 0),
child: Text(weight.toString(),
style: TextStyle(
color: Colors.white,
fontSize: 26.ssp,
fontFamily: 'San francisco',
),
),
),
/* Weight increase icon */
Padding(
padding: const EdgeInsets.fromLTRB(3, 0, 0, 0),
child: IconButton(
icon: new Icon(
MaterialCommunityIcons.plus_circle_outline,
size: 42.ssp,
color: Colors.white,),
onPressed: (){
debugPrint("Weight increased by 1");
setState(() {
if(weight >= 625){
}
else{
weight ++;
}
});
},
splashRadius: 0.1,
),
)
],
)
],
),
),
),
],
)
],
),
],
),
),
/* Height Slider 2nd set */
Container(
width: ScreenUtil().screenWidth,
height: 0.15.sh,
decoration: BoxDecoration(
color: Colors.white
),
child: Column(
children: [],
),
)
],
),
)
);
}
}
This is the screenshot at present
tweaked the paading but no luck. I am new to flutter manage to build this but stuck, any suggestions/help is much appreciated!
You're creating that extra space with how you're setting the height of the 2nd Container at the top of your build method. I assume that the sh in height: 0.6.sh, is ScreenHeight * 0.6?
It doesn't appear that either of those first 2 Container's are adding anything of value. You can just remove them both and your white Container at the bottom should jump up closer to everything else.

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