使用typeahead软件包时,我没有得到期望的结果。
当前的不良行为:
期望的结果:
我的相关代码:
import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import '../providers/analysis_route_provider.dart';
class AutoCompleteTextfieldTwo extends StatefulWidget {
@override
_AutoCompleteTextfieldTwoState createState() =>
_AutoCompleteTextfieldTwoState();
}
class _AutoCompleteTextfieldTwoState extends State<AutoCompleteTextfieldTwo> {
final TextEditingController _controller = TextEditingController();
@override
Widget build(BuildContext context) {
return TypeAheadField(
hideOnEmpty: true,
textFieldConfiguration: TextFieldConfiguration(
style: TextStyle(
color: Colors.white,
),
autofocus: false,
controller: this._controller,
keyboardType: TextInputType.text,
enabled: true,
focusNode: FocusNode(),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide(
width: 2,
color: Colors.blue,
),
),
hintText: 'Type in company name or ticker symbol',
hintStyle: TextStyle(
color: Colors.grey,
),
),
),
getImmediateSuggestions: true,
hideOnError: true,
suggestionsCallback: (pattern) async {
return await AnalysisRouteProvider.getCompaniesForTextfield2(pattern);
},
itemBuilder: (context, itemData) {
return ListTile(
title: Text(itemData['name'].toString()),
subtitle: Text(itemData['symbol'].toString()),
);
},
onSuggestionSelected: (suggestion) {
print('selected');
FocusNode().unfocus();
this._controller.text = suggestion['name'].toString();
_controller.clear();
},
);
}
}
Http请求: static Future getCompaniesForTextfield2(String query) async {
var url = *url with api key here*;
http.Response response = await http.get(url, headers: {
'Content-Type': 'application/json',
});
var jsonData = json.decode(response.body);
return jsonData;
}
JSON代码段(从API实际返回的对象超过26,000个):[ {
"symbol" : "SPY",
"name" : "SPDR S&P 500",
"price" : 326.7,
"exchange" : "NYSE Arca"
}, {
"symbol" : "CMCSA",
"name" : "Comcast Corp",
"price" : 41.98,
"exchange" : "Nasdaq Global Select"
}, {
"symbol" : "KMI",
"name" : "Kinder Morgan Inc",
"price" : 11.83,
"exchange" : "New York Stock Exchange"
}]
我也确定这部分信息是相关的。当前,当我调试并点击“预输入”文本字段时,在调试控制台中得到以下信息:W/IInputConnectionWrapper(13704): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(13704): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(13704): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(13704): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(13704): endBatchEdit on inactive InputConnection
最佳答案
我认为您应该在getImadiateSugetions
上设置false
以禁用onTap上显示建议列表。
关于第二个问题,您不会得到建议,我认为问题出在json中,
您正在使用json.decode,它将解码[]
中要列出的任何json,如果是{}
则将其解码到map中,因此我认为您应该做的是:
suggestionsCallback: (pattern) async {
var list = await AnalysisRouteProvider.getCompaniesForTextfield2(pattern);
return list[0];
},
关于flutter - 提前振颤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64583784/