Comments (3)
So mabye the dropdown menu claborated with the d.f file so it gdlitched?
from flutter.
The
DropdownMenu
should display the item that is selected when theDropdownMenu
text field has no text and loses focus.
@andrew-moreno You mean to say if I had selected cat
, deleted it manually and unfocused from the field and then try to validate, it should show cat
again ?
If so, do you know if the expected behavior matches with any native platform ?
from flutter.
@andrew-moreno hello i think you should try this.
To achieve the expected behavior, you need to make a couple of adjustments to your code:
1.Display the selected value when the DropdownMenu text field has no text and loses focus.
2.Set a breakpoint in the validation function and validate the DropdownMenu selection.
Here is the modified code hope it will help you
SAMPLE CODE:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@OverRide
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@OverRide
State createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
final _formKey = GlobalKey();
static const List animals = ["Cat", "Dog", "Raccoon", "Eagle"];
String? _selectedAnimal;
@OverRide
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Form(
key: _formKey,
child: FormField(
validator: (value) {
if (value == null || value.isEmpty) {
return "Please select an animal";
}
return null;
},
builder: (state) {
return DropdownButtonFormField(
value: _selectedAnimal,
items: animals
.map((animal) =>
DropdownMenuItem(value: animal, child: Text(animal)))
.toList(),
onChanged: (value) {
setState(() {
_selectedAnimal = value;
});
state.didChange(value);
},
onSaved: (value) {
_selectedAnimal = value;
},
validator: (value) {
if (value == null || value.isEmpty) {
return "Please select an animal";
}
return null;
},
decoration: InputDecoration(
labelText: "Select an animal",
border: OutlineInputBorder(),
),
);
},
),
),
TextButton(
onPressed: () {
_formKey.currentState!.validate();
// Set a breakpoint here to validate the DropdownMenu selection
},
child: const Text("Validate"),
),
],
),
),
);
}
}
Explanation of the provided code:
1.I replaced DropdownMenu with DropdownButtonFormField for a simpler and more standard dropdown implementation in Flutter.
2.I added a _selectedAnimal variable to keep track of the selected animal.
3.Modified the DropdownButtonFormField to use _selectedAnimal as the value and updated it when the selection changes.
4.Added validation for the dropdown to ensure an animal is selected before form submission.
5.Set a breakpoint in the validation function where indicated. This breakpoint will be triggered when you press the "Validate" button.
from flutter.
Related Issues (20)
- [iPad] Apple pencil input is not properly captured by the textfield toolbar HOT 2
- Exception has occurred. SocketException (SocketException: Failed host lookup: 'beta.aegis.com.np' (OS Error: The requested name is valid, but no data of the requested type was found. , errno = 11004)) HOT 1
- issue with local auth in flutter HOT 3
- 'Adding WebView to your Flutter app' https://api.ipify.org/?format=json don't work HOT 3
- [in_app_purchase][android] currencySymbol is missing HOT 8
- [Android] Soft keyboard animation is not same as native Android app HOT 3
- [tool_crash] FileSystemException: Cannot open file, OS Error: The system cannot find the file specified., errno = 2 HOT 1
- flutter layout exception HOT 2
- Deprecated imperative apply of Flutter's Gradle plugins HOT 1
- dont able to run flutter in android studio HOT 2
- DropdownMenu navigation is incorrect HOT 2
- AlertDialog content semantics merged HOT 1
- Incorrect "Set up universal links for iOS" guide, causes iOS Universal links not to work. Fix included:
- [Impeller] Unwanted gaps between Arabic characters HOT 4
- [CP] Workaround HardwareRenderer breakage in Android 14 HOT 2
- mac-23 & mac-24, both lost external connection phone device. HOT 1
- mac-8 is low on disk space on machine. HOT 2
- mac-6 & mac-25, both lost external connection phone device. HOT 1
- `ObjcOptions.headerIncludePath` not used HOT 3
- Codelabs - Building an animated responsive app layout with Material 3 - Changing the language changes the code HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter.