How to show icon on specific area after click on button? - flutter

My Goal:
When user click on RaisedButton i would like to show a icon.
My problem:
I don't know how to show icon on specific area
My code:
This code generate some responses buttons (Europe, Asia, Africa, ..)
for (var i = 0; i < NumberChoice; i++)
RaisedButton(
color: _colorButton,
child: new Text(
_choice[i],
style: TextStyle(
fontSize: 20.0, color: Colors.white),
),
onPressed: () {
setState(() {
if (_choice[i] == answer) {
print("--------------- Corect ----------------");
//--HERE HOW TO SHOW ICON IF USER CLICK ON BUTTON--
Icon(
Icons.check,
color: Colors.green,
size: 24.0,
);
scoreResult = 1;
} else {
print("--------------- False ------------------------");
//--HERE HOW TO SHOW ICON IF USER CLICK ON BUTTON--
Icon(
Icons.close,
color: Colors.red,
size: 24.0,
);
scoreResult = 0;
}
});
},
)
Example
If user click on "Europe" , then "Icons.check" displayed where i added "Button here" .

From what i understand
create a string like this : -
String choice;
change it to true whenever u click on raised button
choice == answer
? // show widget if correct
: SizedBox()

Related

Pie chart issues in flutter

I'm using the fl_chart package to create an interactive Pie Chart for my application. Although the design came out fine, I'm facing the following issues with the functionality:
Tapping on a particular PieChartSectionData doesn't always select it and even tapping repeatedly doesn't help. It's always two out of the three that are tappable. Please note that the functionality works as it should for all the sections but there is no indication suggesting that the section tapped on was clicked. As defined in my code, every tap on a section should increase the radius and it's font size to make it stand out from the rest.
Every time a PieChartSectionData is clicked, it is supposed to navigate to a new page after a delay of 500 milliseconds. Although this works as expected, I cannot seem to come back to the previous page that has the Pie Chart on it using the phone's back button on the very first click. It is only after repeated clicks that it takes me back to the original page.
The chart seems to be clickable even beyond the PieChartSectionData. So like let's say if I tap an inch away from one of the sections, it tends to take me to that other page I need to navigate to. This shouldn't be the case.
Here is my code:
class ToDoListPieState extends State<ToDoListPie> {
int? index;
double? percent;
int? taskCount;
#override
Widget build(BuildContext context) {
final provider = Provider.of<TodaysTaskList>(context).getToDoList;
// TODO: implement build
return Stack(
children: [
PieChart(PieChartData(
borderData: FlBorderData(show: false),
pieTouchData: PieTouchData(
touchCallback: (FlTouchEvent event, pieTouchResponse) {
setState(() {
if (pieTouchResponse == null ||
pieTouchResponse.touchedSection == null) {
index = -1;
}
index = pieTouchResponse!.touchedSection!.touchedSectionIndex;
});
Future.delayed(const Duration(milliseconds: 500), () {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => TaskList())); //This here is the event where clicking on a section should take us to the next page
});
print('Index: $index'); //This prints even if we click away from a section
},
),
sectionsSpace: 5,
centerSpaceRadius: 80,
sections: [
PieChartSectionData(
color: Colors.blue,
title: provider['upcoming'].length.toString(),
value: double.parse(provider['upcoming'].length.toString()),
radius: index == 0 ? 40 : 30,
titleStyle: TextStyle(fontSize: index == 0 ? 25 : 16)),
PieChartSectionData(
color: Colors.amber,
title: provider['today'].length.toString(),
value: double.parse(provider['today'].length.toString()),
radius: index == -1 ? 40 : 30,
titleStyle: TextStyle(fontSize: index == -1 ? 25 : 16)),
PieChartSectionData(
color: Colors.red,
title: provider['previous'].length.toString(),
value: double.parse(provider['previous'].length.toString()),
radius: index == 1 ? 40 : 30,
titleStyle: TextStyle(fontSize: index == 1 ? 25 : 16))
])),
Positioned(
left: 0,
top: SizeVariables.getHeight(context) * 0.18,
right: 0,
bottom: 0,
child: Column(
children: [
FittedBox(
fit: BoxFit.contain,
child: Text(
'${provider['upcoming'].length + provider['today'].length + provider['previous'].length}',
style: Theme.of(context)
.textTheme
.bodyText1!
.copyWith(fontSize: 25),
),
),
Text(
'Tasks',
style: Theme.of(context)
.textTheme
.bodyText1!
.copyWith(fontSize: 18),
)
],
),
)
],
);
}
}
Any help will be appreciated!

How to make a container as a radio button in Flutter and make sure it doesn't deselect

I have used a package group_button 4.2.1 but once i select the textfields the radio buttons deselect and i have to select again, i have tried using the controller property of the Widget but i didn't get it to work.
I was thinking if i can make a container from scratch that is a radio button and can retain the value once i finish filling the form to be submitted to my firestore database.
You can use List of button text and keep tract of selectedIndex.
Run on dartPad
int? _selectedValueIndex;
List<String> buttonText = ["ForSale", "For rent"];
Widget button({required String text, required int index}) {
return InkWell(
splashColor: Colors.cyanAccent,
onTap: () {
setState(() {
_selectedValueIndex = index;
});
},
child: Container(
padding: const EdgeInsets.all(12),
color: index == _selectedValueIndex ? Colors.blue : Colors.white,
child: Text(
text,
style: TextStyle(
color: index == _selectedValueIndex ? Colors.white : Colors.black,
),
),
),
);
}
Inside build method to use this,
Row(
children: [
...List.generate(
buttonText.length,
(index) => button(
index: index,
text: buttonText[index],
),
)
],
),

How can I show the text of Text Button when I make it's onPressed function return Null ?(in dark mode)

I have been trying to create an Onboarding view with the next and previous Text Button() where when the user is in the first index of the Onboarding the previous button should be unclickable.
I already did it by making onPressed: function return Null when the onboarding current index = 0 but the problem is the previous's text doesn't appear when I use dark mode. I hope someone can help me.
Previou's TextButton code:
TextButton(
onPressed: cubit.currentIndex != 0
? () {
cubit.decrement();
boardController.previousPage(
duration:
const Duration(milliseconds: 300),
curve: Curves.fastLinearToSlowEaseIn,
);
}
: null,
child: const Text(
'Previous',
style: TextStyle(
fontSize: 18,
)),
Here in the light mode everything is okay I wanna show this in dark mode
use Brigthness for color
style: TextStyle(
fontSize: 18,
color: Theme.of(context).brightness == Brightness.dark
? Colors.green
: null,
)),
if you don't want to use Previous text on first onboarding screen then use following structure :
child: FlatButton(
child: Text(
currentIndex == slides.length - 1 ? "": "Previous"),)
Or if you don't want to use Previous Button on first onboarding screen then use following structure :
child:
currentIndex == slides.length - 1 ? null :
FlatButton(
child: Text(
"Previous"),)

Change state of clicked button inside StreamBuilder list in Flutter

I have a StreamBuilder which returns me a ListView of items.
For each item I also have ElevatedButton button.
ElevatedButton(
onPressed: () {
setState(() {
pressGeoON = !pressGeoON;
if (pressGeoON == true) {
favoriteDataList.add(snapshot
.data?.docs[index].id);
} else {
favoriteDataList.removeWhere(
(item) =>
item ==
snapshot.data
?.docs[index].id);
}
});
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all<
Color>(
Colors.deepPurple,
),
),
child: Icon(
Icons.favorite,
color: pressGeoON
? Colors.blue
: Colors.red,
),
)
I'm trying to change state of only clicked button, but so far with my code it always changes state of all buttons inside of list.
How can I change state only of clicked button and not all of them?

Change Material button color onPress and back to default onLongPress

Hi I am still new to flutter but was trying to make a pretty simple app I thought.
This app consists of 30 buttons each in their own container. Code to follow. All I am trying to do is if a button is pressed then it will turn orange and then if it is longPressed that it goes back to its default color of white. Can someone explain how to do this. Here is an example of just 1 button.
Container(
width: 65,
height: 65,
child: MaterialButton(
shape: CircleBorder(
side: BorderSide(
width: 5,
color: Colors.blue[900],
style: BorderStyle.solid)),
child: Text(
"1",
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
color: Colors.white,
textColor: Colors.black,
onPressed: () {
setState(() {
color:
Colors.orange;
});
},
onLongPress: (){
setState(() {
color:
Colors.white;
});
},
),
),
Thanks in Advance
//declare color variable
Color myColor=Colors.white;
//set myColor to material button
color:myColor;
//in setstate use it like this
setState({
myColor=Colors.white; or myColor=Colors.orange;
})
first define a Color
Color buttonColor = Colors.white;
then pass this color to your button
MaterialButton(color: buttonColor)
after this inside your onPressed function
onPressed: () {
setState(() {
buttonColor =
Colors.orange;
});
},