admin管理员组文章数量:1291127
I have a large list that I want to let the user filter by typing into a field. It is working well but the keyboard is giving some unwanted suggestions. I cannot find a proper way to customize the keyboard. I have tried to change the keyboardType, but with no results.
Below is some of my code using DropDownItem
import 'package:flutter/material.dart';
import 'selector_theme.dart';
abstract class DropDownItem {
String get name;
String? get code;
}
class DropDownSelector extends StatefulWidget {
const DropDownSelector({
super.key,
required this.itemsList,
required this.hintText,
this.selectedItem,
this.onSelected,
});
final List<DropDownItem> itemsList;
final DropDownItem? selectedItem;
final void Function(DropDownItem?)? onSelected;
final String hintText;
@override
State<DropDownSelector> createState() => _DropDownSelectorState();
}
class _DropDownSelectorState extends State<DropDownSelector> {
DropDownItem? selectedItem;
late final List<DropDownItem> itemsList;
late final TextEditingController controller;
@override
void initState() {
super.initState();
controller = TextEditingController();
itemsList = widget.itemsList;
selectedItem = widget.selectedItem;
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: DropdownMenu<DropDownItem>(
controller: controller,
width: double.infinity,
leadingIcon: const Icon(Icons.search),
hintText: widget.hintText,
requestFocusOnTap: true,
keyboardType: TextInputType.visiblePassword,
enableFilter: true,
inputDecorationTheme: selectorDecorationTheme(context),
onSelected: (DropDownItem? anItem) {
FocusScope.of(context).unfocus();
setState(() {
selectedItem = anItem;
widget.onSelected?.call(anItem);
});
},
initialSelection: selectedItem,
menuStyle: const MenuStyle(
shape: WidgetStatePropertyAll(RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)))),
),
dropdownMenuEntries: itemsList
.map<DropdownMenuEntry<DropDownItem>>((DropDownItem anItem) {
return DropdownMenuEntry<DropDownItem>(
value: anItem,
label: anItem.name,
);
}).toList(growable: false),
),
);
}
}
I have a large list that I want to let the user filter by typing into a field. It is working well but the keyboard is giving some unwanted suggestions. I cannot find a proper way to customize the keyboard. I have tried to change the keyboardType, but with no results.
Below is some of my code using DropDownItem
import 'package:flutter/material.dart';
import 'selector_theme.dart';
abstract class DropDownItem {
String get name;
String? get code;
}
class DropDownSelector extends StatefulWidget {
const DropDownSelector({
super.key,
required this.itemsList,
required this.hintText,
this.selectedItem,
this.onSelected,
});
final List<DropDownItem> itemsList;
final DropDownItem? selectedItem;
final void Function(DropDownItem?)? onSelected;
final String hintText;
@override
State<DropDownSelector> createState() => _DropDownSelectorState();
}
class _DropDownSelectorState extends State<DropDownSelector> {
DropDownItem? selectedItem;
late final List<DropDownItem> itemsList;
late final TextEditingController controller;
@override
void initState() {
super.initState();
controller = TextEditingController();
itemsList = widget.itemsList;
selectedItem = widget.selectedItem;
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: DropdownMenu<DropDownItem>(
controller: controller,
width: double.infinity,
leadingIcon: const Icon(Icons.search),
hintText: widget.hintText,
requestFocusOnTap: true,
keyboardType: TextInputType.visiblePassword,
enableFilter: true,
inputDecorationTheme: selectorDecorationTheme(context),
onSelected: (DropDownItem? anItem) {
FocusScope.of(context).unfocus();
setState(() {
selectedItem = anItem;
widget.onSelected?.call(anItem);
});
},
initialSelection: selectedItem,
menuStyle: const MenuStyle(
shape: WidgetStatePropertyAll(RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)))),
),
dropdownMenuEntries: itemsList
.map<DropdownMenuEntry<DropDownItem>>((DropDownItem anItem) {
return DropdownMenuEntry<DropDownItem>(
value: anItem,
label: anItem.name,
);
}).toList(growable: false),
),
);
}
}
Share
Improve this question
asked Feb 13 at 15:04
Denis GesbertDenis Gesbert
11 bronze badge
1 Answer
Reset to default 0Use a Custom TextField
Instead
TheDropdownMenu
does not provide full control over the keyboard, consider using a custom TextField to filter the list instead
import 'package:flutter/material.dart';
void main() {
runApp(const MainApp());
}
// DropDownItem Interface
abstract class DropDownItem {
String get name;
String? get code;
}
// Example DropDownItem Implementation
class CountryItem implements DropDownItem {
final String countryName;
final String countryCode;
CountryItem(this.countryName, this.countryCode);
@override
String get name => countryName;
@override
String? get code => countryCode;
}
// MainApp Widget
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Dropdown Selector Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomeScreen(),
);
}
}
// HomeScreen Widget
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
DropDownItem? selectedItem;
// Sample Data
final List<DropDownItem> countries = [
CountryItem('United States', 'US'),
CountryItem('Canada', 'CA'),
CountryItem('United Kingdom', 'UK'),
CountryItem('Germany', 'DE'),
CountryItem('France', 'FR'),
CountryItem('Nigeria', 'NG'),
CountryItem('India', 'IN'),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Dropdown Selector")),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DropDownSelector(
itemsList: countries,
hintText: "Select a country",
selectedItem: selectedItem,
onSelected: (DropDownItem? item) {
setState(() {
selectedItem = item;
});
},
),
const SizedBox(height: 20),
Text(
selectedItem != null
? "Selected: ${selectedItem!.name} (${selectedItem!.code})"
: "No selection",
style: const TextStyle(fontSize: 18),
),
],
),
),
);
}
}
// DropDownSelector Widget
class DropDownSelector extends StatefulWidget {
const DropDownSelector({
super.key,
required this.itemsList,
required this.hintText,
this.selectedItem,
this.onSelected,
});
final List<DropDownItem> itemsList;
final DropDownItem? selectedItem;
final void Function(DropDownItem?)? onSelected;
final String hintText;
@override
State<DropDownSelector> createState() => _DropDownSelectorState();
}
class _DropDownSelectorState extends State<DropDownSelector> {
DropDownItem? selectedItem;
late List<DropDownItem> itemsList;
late final TextEditingController controller;
@override
void initState() {
super.initState();
controller = TextEditingController();
itemsList = widget.itemsList;
selectedItem = widget.selectedItem;
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
TextField(
controller: controller,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
hintText: widget.hintText,
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
),
keyboardType: TextInputType.text,
autocorrect: false,
enableSuggestions: false,
textCapitalization: TextCapitalization.none,
onChanged: (value) {
setState(() {
itemsList = widget.itemsList
.where((item) =>
item.name.toLowerCase().contains(value.toLowerCase()))
.toList();
});
},
),
const SizedBox(height: 10),
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 5)],
),
child: Column(
children: itemsList
.map(
(item) => ListTile(
title: Text(item.name),
onTap: () {
setState(() {
selectedItem = item;
controller.text = item.name;
widget.onSelected?.call(item);
});
},
),
)
.toList(),
),
),
],
);
}
}
本文标签: flutterHow to prevent keyboard autocorrection and suggestion when using DropDownItemStack Overflow
版权声明:本文标题:flutter - How to prevent keyboard autocorrection and suggestion when using DropDownItem - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741524824a2383403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论