I'm working in small project. For Login, I need to save URL in shared preferences so that next time user can select it in dropdown. I need to keep delete option in that dropdown to delete old URL. How to do?
code:
if (getALLURL.isNotEmpty) ...[
DropdownButton<String>(
value: baseurl.isNotEmpty ? baseurl : null,
borderRadius: BorderRadius.circular(2.0),
underline: SizedBox(),
focusColor: AppColors().linenDark,
elevation: 1,
enableFeedback: true,
isExpanded: true,
style: GoogleFonts.montserrat(
fontSize: AppSizes().paraLarge16,
color: AppColors().linenDark,
fontWeight: FontWeight.w400),
icon: Icon(
Icons.keyboard_arrow_down_rounded,
color: AppColors().linenMedium,
),
items: getALLURL.map((String item) {
return DropdownMenuItem<String>(
child: Text(item),
value: item,
);
}).toList(),
onChanged: (value) async {
setState(() {
baseurl = value!;
print(baseurl);
});
await Prefs().setBaseURLLogin(baseurl);
},
hint: Text("Select item"),
),
//IconButton(onPressed: onDelete, icon: Icon(Icons.delete))
],
Now I'm only used text in menuitems, how to add delete in it. Can I delete particular value in shared preferences
Can I delete particular value in shared preferences
Yes, you can write new value over the old one with same key.
Now I'm only used text in menuitems, how to add delete in it
i don't think it could support that directly . Even if it , i recommended you to use instead way for delete option such as any position of the screen.
Related
I'm using firebase real-time database and I have a form where the user adds the information to the database using it, but I'm stuck with the DropdownButtonFormField, how can I store the value in my database when the form button is pressed?
I used to use the controller when it was TextFormField, but now, I don't know what to do
DropdownButtonFormField(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(20.0)),
borderSide:
BorderSide(width: 2, color: appColor),
),
labelText: 'Location',
labelStyle: TextStyle(
fontSize: 20, color: Colors.black)),
isExpanded: true,
// Initial Value
value: selectedLocation,
icon: const Icon(
Icons.keyboard_arrow_down,
color: appColor,
),
// Array list of items
items: Locations.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newLocation) {
setState(() {
selectedLocation = newLocation!;
});
},
),
you get the selectedLocation value and pass that to firebase method. print selectedLocation in the onChange method then when ever you click item in dropdown, the selectedLocation will change.
DatabaseReference ref = FirebaseDatabase.instance.ref("users/123");
await ref.set({
"selectedLocation": selectedLocation ,
});
I am using pin_code_text_field package to create pin code textfields. But when I was updating the bool value used to obscure text. Bool value is not updating in pin code fields until I click on textfields.
Code was added below:
bool pinWasObscured = true;
Row(
children: [
PinCodeTextField(
maxLength: 4,
hideCharacter: pinWasObscured,
highlight: true,
highlightAnimation: true,
highlightAnimationBeginColor: Colors.black,
highlightAnimationEndColor: Colors.white,
highlightAnimationDuration: Duration(seconds: 5),
highlightColor: Color(0xFFF37021),
pinBoxDecoration: ProvidedPinBoxDecoration.underlinedPinBoxDecoration,
maskCharacter: "*",
pinTextStyle: TextStyle(
fontSize: 15.sp,
fontWeight: FontWeight.bold,
),
pinBoxWidth: SizeConfig.blockSizeHorizontal! * 12,
pinBoxHeight: SizeConfig.blockSizeHorizontal! * 10,
autofocus: false,
controller: pinController,
defaultBorderColor: Colors.black26,
),
SizedBox(width: 10),
IconButton(
icon: pinWasObscured
? Icon(Icons.visibility_off_outlined)
: Icon(Icons.visibility_outlined),
onPressed: () {
setState(() {
pinWasObscured = !pinWasObscured;
});
},
),
],
),
the issue is that you are also updating the value of the "hideCharacter" on the click of item. you dont have to update the hideCharacter value everytime.
instead you have to set the value to true if you want to hide the character and false to show.
I have switch button and when I go to some other tab and come again it turns on itself. I want it to stay On or Off depending on what last selected. Though the value are getting saved in data base correctly but when go to some other tab it is back to On. This is code below.
bool _hideProfile = false;
ListTile(
title: Card(
color: themeProvider.isDarkMode? black :white,
child: Padding(
padding: EdgeInsets.only(left:20,right: 20),
child: SwitchListTile(
title: _hideProfile? Text("Profile hidden",style: TextStyle(fontSize: 18),)
:Text(" Profile visible",style: TextStyle(fontSize: 18)),
secondary: _hideProfile?
Icon(Icons.visibility_off_outlined,color: lRed ):
Icon(Icons.visibility_outlined,color: Colors.green),
activeColor: Colors.blueGrey,
inactiveThumbColor: mRed,
inactiveTrackColor: dRed,
value: _hideProfile,
onChanged:(value){
setState(() {
_hideProfile = value;
});
String userStatus = 'active';
if (value) {
userStatus = 'hidden';
}
CreateAccountData(userStatus: userStatus ,);
_reference.doc(auth.currentUser.uid).update(
{
"userStatus" : userStatus,
}).then((_){
print('Profile hidden: $value');
});
}
),
),
),
),
You are using setState() for the state management so when you will go to some other page then the variable will loose it's state means it will get disposed and when you will come back it will start what ever you had set as default.
Now to over come this use state management package like provider, mobx, bloc etc.
has to be something simple I'm not getting but have been struggling on this for way too long. If anybody can help me out I would be appreciative. I believe it has to do with some kind of type issues as when I make selections I have a print statement that then prints out what I'm getting back:
[Instance of 'MultiSelectItem', Instance of 'Analysis', Instance of 'Analysis']
Not sure how to convert the above to a List of Analysis and not able to carry out per the example for the package:
MultiSelectDialogField(
items: _animals.map((e) => MultiSelectItem(e, e.name)).toList(),
listType: MultiSelectListType.CHIP,
onConfirm: (values) {
_selectedAnimals = values;
},
),
where I also have to use a 'cast' in my code. My attempt:
MultiSelectDialogField(
items: _analyses.map((analysis) => MultiSelectItem<Analysis>(analysis, analysis.name)).toList(),
initialValue: outwardsData.outwards[widget.index].lossFeed
.map((analysis) => MultiSelectItem<Analysis>(analysis, analysis.name))
.toList(),
title: Text(
"Loss Feeds:",
style: TextStyle(color: Colors.black),
),
selectedColor: kConvexGreen,
unselectedColor: kConvexLightGreen,
backgroundColor: kConvexGreen,
decoration: BoxDecoration(
color: kConvexGreen.withOpacity(0.1),
borderRadius: BorderRadius.all(Radius.circular(5)),
border: Border.all(
color: kConvexGreen,
width: 2,
),
),
confirmText: Text(
'Submit',
style: TextStyle(color: Colors.white),
),
cancelText: Text(
'Cancel',
style: TextStyle(color: Colors.white),
),
searchable: true,
buttonText: Text("Loss Feeds"),
onSelectionChanged: (results) {
**print(results)**;
},
onConfirm: (results) {
// this is different than what example shows; without cast it errors
// "A value of type 'List<Object?>' can't be assigned to a variable of type 'List<Analysis>'". (Documentation)
_selectedAnalyses = results.cast();
},
chipDisplay: MultiSelectChipDisplay(
chipColor: kConvexGreen,
textStyle: TextStyle(color: Colors.white),
onTap: (value) {
setState(() {
_selectedAnalyses.remove(value);
// crashes here even though both are of same type List<Analyses> (theoretically)
// this is just here for testing purposes as am through many iterations trying to get
// data into lossFeed.
outwardsData.outwards[widget.index].lossFeed =
_selectedAnalyses;
});
},
),
),
I'm sure a more experienced flutter person will understand this (maybe) but I found that I couldn't just have the initial values come from the same list type used for the item (List< Analysis> _analyses) but I actually had to map it to use the same items data (_analyses).
initialValue: outwardsData.outwards[widget.index].lossFeed.map((analysis) => _analyses[analysis.id - 1]).toList()
Had other problems with this widget such as I found I couldn't map the results from onConfirm directly to List but instead had to do the following:
_rData.lossFeed = results.toList().cast<Analysis>()
as the data came back as List< dynamic>.
Couldn't find great examples online and trust experience will be the cure-all at some point.
How can I remove the underline from Searchable Drop down Button , I have tried various combinations of options with Input Decoration couldn't find any way.
child: DropdownButtonHideUnderline(
child: SearchableDropdown.single(
items: this.searchableItems,
value: this._selectSubordinate,
hint: "Subordinate",
searchHint: "Enter yoursubordinate name or Business code",
onChanged: (value) {
setState(() {
this._selectSubordinate = value;
});
},
isExpanded: true,
),
),
used underline properties :)
SearchableDropdown.single(
underline: Padding(
padding: EdgeInsets.all(5),
),
items: _serviceItems,
value: selectedServiceVal,
onChanged: (value) => setState(() {
selectedServiceVal = value;
_subjectTextController.text =
'${selectedServiceVal ?? ''} - ${selectedCustomerVal ?? ''}';
}),
isExpanded: true,
);
cf. flutter searchable dropdown version
https://pub.dev/packages/searchable_dropdown
# searchable dropdown
searchable_dropdown: ^1.1.3
You can use
underline: DropdownButtonHideUnderline(child: Container()),