Comments (4)
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.
Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now.
If you find this problem please file a new issue with the same description, what happens, logs and the output of 'flutter doctor -v'. All system setups can be slightly different so it's always better to open new issues and reference the related ones.
Thanks for your contribution.
from flutter.
Related Issues (20)
- [tool_crash] FileSystemException: Failed to decode data using encoding 'utf-8', null HOT 1
- On iOS, the cursor does not move even if you enter a full-width space in the TextField (3.22.0) HOT 4
- ccache support is gone in the latest version of Buildroot. HOT 2
- [web] JS formatter breaks the templates in the new load API. HOT 3
- [add2app] Android cannot register flutter plugin HOT 6
- Tool Crash - _TypeError: type 'String' is not a subtype of type 'int?' in type cast HOT 5
- [tool_crash] OutOfMemoryError: (null) HOT 1
- Can't interact with app build for integration testing HOT 5
- [google_maps_flutter] setMapStyle should not be deprecated HOT 2
- [two_dimensional_scrollables] TapGestureRecognizer of TableSpan not working after setState() HOT 8
- [Impeller] Latest Vulkan SDK introduces validation failures.
- List/Detail template crashes on horizontal orientation of foldable android emulators HOT 2
- SliverAppBar should allow for a custom scroll physics or some control over collapse/expand speed, or should at least give an option to mirror native iOS behavior HOT 3
- please add menu mode to FilterChip HOT 1
- Add display dpi information to MediaQuery to calculate display size HOT 4
- adb devices does find my phone.flutter devices does not find my phone. HOT 2
- 3.22.1 does not support win7 HOT 2
- How do I use the same name and Bundle ID for my iOS and MacOS app? Can anyone help? HOT 1
- Failed to launches my Flutter WASM compiled web app (WebAssembly.compileStreaming() Compiling function failed HOT 4
- Keyboard Flickering Issue on Android Devices HOT 1
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.