I have problem in using radio button "flutter" - flutter

I doubt they developed radio button that's why my code is not working I checked flutter.dev but I didn't see something new ,Then I realized that the error is in the dart data types I use string where is the problem??
lass _MyAppState extends State<MyApp> {
String feel;
#override
Widget build(BuildContext context) {
return MaterialApp(
radio button code :
Text('good'),
Radio(
value: "good",
groupValue: feel,
onChanged: (val) {
setState(() {
feel = val;
print(feel);
});
})
can someone help me find what's wrong??

You need define feel as nullable because the onChange function of Radio return a nullable value , try this:
String? feel;
and also define Radio type like this:
Radio<String>(//<--- add this
value: "good",
groupValue: feel,
onChanged: (val) {
setState(() {
feel = val;
print(feel);
});
}),

Related

How does the onPressed voidcallback change the boolean in my function?

Situation:
I have a checkbox in one place and i am sending the callback etc. up the widget tree to run a setState and run the function applyFilters().
The NeededChecked is also routed up to the checkbox-value.
Question:
What i am struggling to understand is why this works.
Specifically how the onPressed callback is able to set the value of the bool isNeededState to true/false?
Here is the code that is run. The only important part is the passing of the bool isNeededState to the neededCheked.
void neededFilterCalled(bool isNeededState) {
setState(() {
NeededChecked = isNeededState;
applyFilters();
});
}
And here is the checkbox widget:
Widget build(BuildContext context) {
return Checkbox(
value: isNeededChecked,
onChanged: neededFilterCalled,
);
}
Writing
onChanged: neededFilterCalled,
is shorthand for
onChanged: (value) => neededFilterCalled(value),
onChanged provide nullable bool, defined as
required void Function(bool?)? onChanged
You can accept null value and provide false on null case like
void neededFilterCalled(bool? isNeededState) {
setState(() {
isNeededChecked = isNeededState ?? false;
applyFilters();
});
}
return Checkbox(
value: isNeededChecked,
onChanged: neededFilterCalled,
);

How to access dropdownlist selected value in flutter and use it in another widget

This is my drop down list code
String dropdownValue = "a";
#override
Widget build(BuildContext context) {
return DropdownButton<String>(
value: dropdownValue
),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue!;
});
},
items: <String>[
'a','b','c'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
}
}
I want to use the selected value here
uploadDatatoFirebase() async {
*firebase connection code*
await FirebaseFirestore.instance
.collection(**selected value in drop down**) // the value from the box goes here
}
I can't seem to find a solution please help i want to create a database depending on the select box value any other techniques will also be welcomed
https://pub.dev/packages/get_it This is the package I use so I can can access a single instance of a class anywhere in the app, it is very popular and very well maintained. You could do this
setState(() {
dropdownValue = newValue!;
GetIt.I.get<ControllerWhatever>().selectedValueInDropDown = newValue!;
})
Just register the 'global controller instance' just as described in the get_it page and you will be able to get the value from any attribute you want anywhere in your app. It will make you life a lot easier

StateNotifierProvider: is it possible to control the value of different widgets in a form using a single StateNotifierProvider

class SearchFilterModel {
bool searchByDistance;
bool filterByStatusOpen;
String searchText;
...
}
class SearchFilterModelNotifier extends StateNotifier<SearchFilterModel> {
SearchFilterModelNotifier()
: super(SearchFilterModel(
searchByDistance: true,
filterByStatusOpen: false,
searchText: ''));
void updateSearchByDistance(bool searchByDistance) {
state.searchByDistance = searchByDistance;
}
void updateFilterByStatusOpen(bool filterByStatusOpen) {
state.filterByStatusOpen = filterByStatusOpen;
}
void updateSearchText(String searchText) {
state.searchText = searchText;
}
}
final searchFilterModelNotifierProvider = StateNotifierProvider<SearchFilterModelNotifier, SearchFilterModel>((ref) => SearchFilterModelNotifier());
class SearchForm extends ConsumerWidget {
#override
Widget build(context, watch) {
final searchFilterModelNotifier = watch(searchFilterModelNotifierProvider.notifier);
final searchFilterModel = watch(searchFilterModelNotifierProvider);
return Form(
child: Column(
children: [
SwitchListTile(
value: searchFilterModel.searchByDistance,
onChanged: (bool value) => searchFilterModelNotifier.updateSearchByDistance(value),
title: Text('Search by distance?')),
SwitchListTile(
value: searchFilterModel.filterByStatusOpen,
onChanged: (bool value) => searchFilterModelNotifier.updateFilterByStatusOpen(value),
title: Text('Filter by open status?')),
// not even sure how to do the text one if using a TextFormField
]
)
);
}
}
Okay with my project structured like this the values of the Switches aren't behaving as expected, their values aren't updating at all and I believe it's just that the widgets aren't rebuilding. I was able to make this work using a different state provider for each form field however when I have a lot of form fields it'd be way nicer to be able to have a single StateNotifierProvider that represents the entire form. Is this just something that's not possible?
The answer is to update the StateNotifier methods to something like
void updateSearchByDistance(bool searchByDistance) {
state = SearchFilterModel(... searchByDistance: searchByDistance);
}

How to set different value instead of items's value in DropdownButton in flutter?

I want to achieve different values behind the scene, for example, if the user selects "United State of America" behind the scene I want a value "USA" only. How can I achieve this?
Here is my button:
DropdownButton<String>(
isExpanded: true,
underline: SizedBox(),
icon: SvgPicture.asset("assets/icons/dropdown.svg"),
value: dropdownValue,
items: [
'Nepal',
'India',
'United States',
'Denmark',
'UK',
'World Wide'
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (newValue) {
setState(() {
dropdownValue = newValue;
});
},
),
What you can do is create a CountryOption class with a key('USA' in your example) and a fullName ('United States' in your example).
You then create a list of dropdown items of CountryOption instead of String, so you can store the currently selected CountryOption and you have both the key and fullName properties available for later use.
I would also recommend loading your list of items only once, not on each rebuild.
import 'package:flutter/material.dart';
class CountriesButton extends StatefulWidget {
const CountriesButton({Key key}) : super(key: key);
#override
_CountriesButtonState createState() => _CountriesButtonState();
}
class _CountriesButtonState extends State<CountriesButton> {
List<DropdownMenuItem<CountryOption>> _countryItems;
CountryOption _selectedCountry;
#override
void initState() {
// Get all countries
List<CountryOption> countries = CountryOption.allCountries;
// Initialise your items only once
_countryItems = countries.map<DropdownMenuItem<CountryOption>>(
(CountryOption countryOption) {
return DropdownMenuItem<CountryOption>(
value: countryOption,
child: Text(countryOption.fullName),
);
},
).toList();
// Initialiste your dropdown with the first country in the list
// (might be different in your specific scenario)
_selectedCountry = countries[0];
super.initState();
}
#override
Widget build(BuildContext context) {
return Container(
child: DropdownButton<CountryOption>(
isExpanded: true,
underline: SizedBox(),
icon: SvgPicture.asset("assets/icons/dropdown.svg"),
value: _selectedCountry,
items: _countryItems,
onChanged: (newValue) {
setState(() {
_selectedCountry = newValue;
});
},
),
);
}
}
class CountryOption {
final String key;
final String fullName;
CountryOption(this.key, this.fullName);
static List<CountryOption> get allCountries => [
CountryOption('nepal', 'Nepal'),
CountryOption('india', 'India'),
CountryOption('USA', 'United States'),
CountryOption('denmark', 'Denmark'),
CountryOption('uk', 'UK'),
CountryOption('world', 'World Wide'),
];
}
Let me know if something is not clear or if you have any questions.
Most simple method is to create a switch case and pass in the dropdownvalue.
Then define your cases and default option. You can make this with a function inside the class or in separate class.

How to update Inner Widget in Flutter

Hi I am having an issue with radio and checkbox on flutter. I have created my own widget on the purpose of having different return widget depending on what is needed as on the picture bellow since I selected change pin the return will be a checkbox but the return widget is a checkbox while the save button is stagnant or didn't change but I am having an issue when i used radio or check box even Visibility. When I try to update the setState() it is not updating or reloading the widget. The code below is just the first return of the widget which will be a check box. When i try to unchecked the button the value becomes falls but the UI is not unchecked.
sample Image
Widget getInputType(val){
if(val == 'Pin Lock'){
return CheckboxListTile(
title: Text('Pin Login'),
value: isSwitchPin,
onChanged: (value){
setState(() {
print(value);
this.isSwitchPin = value;
});
});
}
}
You can try:
1, Define a field
ValueChanged<bool> onEnventChange;
2, On initState()
onEnventChange = (value){
setState(() {
print(value);
this.isSwitchPin = value;
});
});
3, Define your function
Widget getInputType(val){
if(val == 'Pin Lock'){
return CheckboxListTile(
title: Text('Pin Login'),
value: isSwitchPin,
onChanged: onEnventChange);
}
}