问题描述
我正在制作一个词汇应用程序,并且在如下数据中包含List<Map<String,String>>
。
english.dart
class Service {
static List<Map<String,String>> getSuggestions(String query) {
List<Map<String,String>> matches = List<Map<String,String>>();
matches.addAll(words);
matches.retainWhere(
(s) => s['word'].toLowerCase().startsWith(query.toLowerCase()));
return matches;
}
static final List<Map<String,String>> words = [
{'word': "aardvark",'meaning': "땅돼지"},...
];
}
home.dart
import 'package:Flutter_typeahead/Flutter_typeahead.dart';
class _HomeState extends State<Home> {
SuggestionsBoxController suggestionsBoxController =
SuggestionsBoxController();
final TextEditingController _controller = TextEditingController();
var _controller2 = TextEditingController();
void displayBottomSheet(BuildContext context) {
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(40),),context: context,isScrollControlled: true,backgroundColor: Colors.white,isdismissible: true,builder: (context) {
return StatefulBuilder(
builder: (BuildContext context,StateSetter state) {
return Form(
key: this._formKey,child: Container(
height: MediaQuery.of(context).size.height -
190.0,child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 10,left: 280),child: FlatButton(
child: Text('save',style: TextStyle(
fontSize: 20,fontWeight: FontWeight.w800)),onpressed: () {
setState(() {
saveDB();
_controller.clear();
_controller2.clear();
FocusScope.of(context)
.requestFocus(focusNode1);
});
})),Container(
margin: EdgeInsets.only(left: 20,right: 20),child: Column(children: <Widget>[
Focus(
child: TypeAheadFormField(
suggestionsBoxController:
suggestionsBoxController,hideOnEmpty: true,textFieldConfiguration:
TextFieldConfiguration(
maxLines: null,onChanged: (text) {
setState(() {
this.word = this._controller.text;
});
},focusNode: focusNode1,autofocus: true,controller: this._controller,style: TextStyle(
fontSize: 30,fontWeight: FontWeight.w800,color: Colors.black),decoration: Inputdecoration(
isDense: true,suffix: Padding(
padding:
const EdgeInsetsDirectional
.only(),child: IconButton(
onpressed: () =>
_controller.clear(),icon: Icon(Icons.clear),iconSize: 25,color: Colors.black
.withOpacity(0.5),)),contentPadding: EdgeInsets.only(
left: 11,bottom: 3),enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black)),focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.black),hintText: "word",hintStyle: TextStyle(
fontSize: 29.0,color: Colors.black
.withOpacity(0.5)),suggestionsBoxVerticalOffset: 0,suggestionsBoxdecoration:
SuggestionsBoxdecoration(
constraints:
BoxConstraints(maxHeight: 124),offsetX: 0,elevation: 10,color: Colors.white,borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(30),bottomright:
Radius.circular(30))),suggestionsCallback: (pattern) {
if (pattern.length > 0) {
return Service.getSuggestions(pattern);
}
},itemBuilder: (context,suggestion) {
return Container(
decoration: new Boxdecoration(),child: ListTile(
dense: true,contentPadding:
EdgeInsets.only(left: 20),title: Row(children: <Widget>[
Text(suggestion['word'],style: TextStyle(
fontSize: 18,fontWeight:
FontWeight.w500)),SizedBox(width: 7),Text(suggestion['meaning'],fontWeight:
FontWeight.w300))
])));
},transitionBuilder:
(context,suggestionsBox,controller) {
return suggestionsBox;
},onSuggestionSelected: (suggestion) {
this._controller.text = suggestion['word'];
this.word = this._controller.text;
this._controller2.text =
suggestion['meaning'];
FocusScope.of(context)
.requestFocus(focusNode2);
},onSaved: (value) =>
this._selectedWord = value,TextField(
maxLines: null,controller: _controller2,focusNode: focusNode2,textInputAction: TextInputAction.done,onChanged: (String meaning) {
this.meaning = meaning;
},style: TextStyle(
fontSize: 20,fontWeight: FontWeight.w500,decoration: new Inputdecoration(
isDense: true,suffix: Padding(
padding:
const EdgeInsets.only(left: 20),child: IconButton(
onpressed: () =>
_controller2.clear(),color:
Colors.black.withOpacity(0.5),contentPadding:
EdgeInsets.only(left: 13,bottom: 20),border: InputBorder.none,focusedBorder: InputBorder.none,enabledBorder: InputBorder.none,hintText: "meaning",hintStyle: TextStyle(
color:
Colors.black.withOpacity(0.5)))),]))
],)));
});
});
}
我在文本字段中使用了typeahead plugin,但是没有用。但是在VSCode中,我的代码没有错误。我该怎么解决?
解决方法
我做了一点测试,它对我有用。这是我的代码:
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),),body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
TypeAheadField(
textFieldConfiguration: TextFieldConfiguration(
autofocus: true,style: DefaultTextStyle.of(context).style.copyWith(
fontStyle: FontStyle.italic
),decoration: InputDecoration(
border: OutlineInputBorder()
)
),suggestionsCallback: (pattern) {
return Service.getSuggestions(pattern);
},itemBuilder: (context,suggestion) {
return ListTile(
title: Text(suggestion['word']),subtitle: Text('\$${suggestion['meaning']}'),);
},onSuggestionSelected: (suggestion) {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => null
));
},],floatingActionButton: FloatingActionButton(
onPressed: null,tooltip: 'fd',child: Icon(Icons.add),// This trailing comma makes auto-formatting nicer for build methods.
);
}
这是课程服务:
class Service {
static List<Map<String,String>> getSuggestions(String query) {
List<Map<String,String>> matches = List<Map<String,String>>();
matches.addAll(words);
matches.retainWhere(
(s) => s['word'].toLowerCase().startsWith(query.toLowerCase()));
return matches;
}
static final List<Map<String,String>> words = [
{'word': "aardvark",'meaning': "땅돼지"},{'word': "bbdaad",'meaning': "땅돼지지"},];
}
希望它可以为您提供帮助