在Flutter中获取TextField中的DropDown的值,可以通过以下步骤实现:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
flutter_dropdown: ^0.4.0
import 'package:flutter/material.dart';
import 'package:flutter_dropdown/flutter_dropdown.dart';
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
String selectedValue = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('TextField with DropDown'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
decoration: InputDecoration(
labelText: 'Enter text',
),
),
SizedBox(height: 20),
DropDown<String>(
items: ['Option 1', 'Option 2', 'Option 3'],
hint: Text('Select an option'),
onChanged: (value) {
setState(() {
selectedValue = value;
});
},
),
SizedBox(height: 20),
Text('Selected value: $selectedValue'),
],
),
),
);
}
}
class _MyPageState extends State<MyPage> {
TextEditingController textFieldController = TextEditingController();
@override
void dispose() {
textFieldController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
// ...
body: Center(
child: Column(
// ...
children: [
TextField(
controller: textFieldController,
decoration: InputDecoration(
labelText: 'Enter text',
),
),
// ...
],
),
),
);
}
}
这是在Flutter中获取TextField中的DropDown的值的基本步骤。根据具体的应用场景和需求,你可以进一步扩展和优化代码。如果你想了解更多关于Flutter的信息,可以访问腾讯云的Flutter产品介绍页面:Flutter产品介绍。
领取专属 10元无门槛券
手把手带您无忧上云