Flutter 如何实时查看下拉按钮
我的问题是我不能实时看到我选择的实时字符串。 例如,如果我从Drop溺水项目中选择一个字符串,那么我就看不到它。如果我想看的话,首先我得回去再打开表格。我怎样才能解决这个问题? (顺便说一句,它是有效的,提交按钮没有问题。我只是想看看用户选择了什么 我的问题是: 我的代码:Flutter 如何实时查看下拉按钮,flutter,provider,state-management,Flutter,Provider,State Management,我的问题是我不能实时看到我选择的实时字符串。 例如,如果我从Drop溺水项目中选择一个字符串,那么我就看不到它。如果我想看的话,首先我得回去再打开表格。我怎样才能解决这个问题? (顺便说一句,它是有效的,提交按钮没有问题。我只是想看看用户选择了什么 我的问题是: 我的代码: Widget dropdownButton(BuildContext context) { String constantValue = "League Of Legends"; ret
Widget dropdownButton(BuildContext context) {
String constantValue = "League Of Legends";
return DropdownButton(
value: context.read<PostProvider>().postCategory ?? constantValue,
onChanged: (newValue) {
context.read<PostProvider>().postCategory = newValue;
},
items: <String>["League Of Legends", "Steam", "Csgo"]
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
onTap: () => context.read<PostProvider>().postCategory,
value: value,
child: Text(value),
);
}).toList());
}
String get postCategory => _postCategory;
set postCategory(String value) {
_postCategory = value;
notifyListeners();
}
主页:
Scaffold(
floatingActionButton: CreatePostButton(),
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Home Page"),
IconButton(
onPressed: () {
provider.logout();
},
icon: FaIcon(FontAwesomeIcons.poo))
],
),
),
body: HomePageWidget())
创建帖子按钮:
class CreatePostButton extends StatelessWidget {
static final _formKey = GlobalKey<FormState>(debugLabel: '_formKey');
static final _scaffoldKey = GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) => Padding(
padding: const EdgeInsets.only(right: 13.0, bottom: 13.0),
child: FloatingActionButton(
child: FaIcon(FontAwesomeIcons.plus),
onPressed: () {
showDialog(context: context, child: buildAlertDialog(context));
}),
);
Widget buildAlertDialog(BuildContext context) {
final provider = Provider.of<PostProvider>(context, listen: false);
return AlertDialog(
content: Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
autocorrect: true,
textCapitalization: TextCapitalization.words,
enableSuggestions: false,
validator: (value) {
if (value.isEmpty || value.length <= 4) {
return 'Please enter at least 4 characters';
} else {
return null;
}
},
decoration: InputDecoration(labelText: 'Post Title'),
onChanged: (postTitle) {
provider.postTitle = postTitle;
})),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
autocorrect: true,
textCapitalization: TextCapitalization.words,
enableSuggestions: false,
validator: (value) {
if (value.isEmpty || value.length <= 25) {
return 'Please enter at least 25 characters';
} else {
return null;
}
},
decoration:
InputDecoration(labelText: 'Write a post details'),
onChanged: (postDetails) {
provider.postDetails = postDetails;
})),
Padding(
padding: EdgeInsets.all(8.0),
child: TextFormField(
enableSuggestions: false,
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],
keyboardType: TextInputType.number,
validator: (value) {
if (value.isEmpty || value.length >= 4) {
return 'Please enter a valid value';
} else {
return null;
}
},
decoration: InputDecoration(labelText: 'Enter the Price'),
onChanged: (gamePrice) {
provider.gamePrice = gamePrice;
})),
dropdownButton(context),
Padding(
padding: const EdgeInsets.all(8.0),
child: RaisedButton(
child: Text("Submit"),
onPressed: () => submitNewPost(context)),
),
],
),
),
),
);
}
Future submitNewPost(BuildContext context) async {
final provider = Provider.of<PostProvider>(context, listen: false);
final isValid = _formKey.currentState.validate();
FocusScope.of(context).unfocus();
if (isValid) {
_formKey.currentState.save();
final isSuccess = await provider.createNewPost();
if (isSuccess) {
Navigator.of(context).pop();
} else {
final message = 'An error occurred, please check your inputs!';
Center(child: Text(message),);
}
}
}
Widget dropdownButton(BuildContext context) {
String constantValue = "League Of Legends";
return DropdownButton(
value: context.read<PostProvider>().postCategory ?? constantValue,
onChanged: (newValue) {
context.read<PostProvider>().postCategory = newValue;
},
items: <String>["League Of Legends", "Steam", "Csgo"]
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
onTap: () => context.read<PostProvider>().postCategory,
value: value,
child: Text(value),
);
}).toList());
}
}
class CreatePostButton扩展了无状态小部件{
静态final _formKey=GlobalKey(debugLabel:'u formKey');
静态最终_scaffoldKey=GlobalKey();
@凌驾
小部件构建(BuildContext上下文)=>填充(
填充:仅限常量边集(右侧:13.0,底部:13.0),
子:浮动操作按钮(
子项:FaIcon(FontAwesomeIcons.plus),
已按下:(){
showDialog(上下文:上下文,子级:buildAlertDialog(上下文));
}),
);
小部件buildAlertDialog(BuildContext上下文){
final provider=provider.of(上下文,listen:false);
返回警报对话框(
内容:表格(
键:_formKey,
子:SingleChildScrollView(
子:列(
mainAxisSize:mainAxisSize.min,
儿童:[
填充物(
填充:边缘设置。全部(8.0),
子项:TextFormField(
自动更正:正确,
textcapitalize:textcapitalize.words,
使能建议:错误,
验证器:(值){
如果(value.isEmpty | | value.length submitNewPost(上下文)),
),
],
),
),
),
);
}
未来submitNewPost(构建上下文)异步{
final provider=provider.of(上下文,listen:false);
final isValid=_formKey.currentState.validate();
(上下文)的焦点范围。取消焦点();
如果(有效){
_formKey.currentState.save();
final isSuccess=等待提供程序。createNewPost();
如果(isSuccess){
Navigator.of(context.pop();
}否则{
最终消息='发生错误,请检查您的输入!';
中心(子:文本(消息),);
}
}
}
小部件下拉按钮(BuildContext上下文){
字符串constantValue=“传奇联盟”;
返回下拉按钮(
值:context.read().postCategory??constantValue,
一旦更改:(newValue){
context.read().postCategory=newValue;
},
物品:[“传奇联盟”、“蒸汽”、“Csgo”]
.map((字符串值){
返回下拉菜单项(
onTap:()=>context.read().postCategory,
价值:价值,
子项:文本(值),
);
}).toList());
}
}
用消费者包装下拉按钮以更新小部件:
Widget dropdownButton(BuildContext context) {
String constantValue = "League Of Legends";
return Consumer<PostProvider>(builder: (_context, _postprovider, _widget) {
return DropdownButton(
value: _postprovider.postCategory ?? constantValue,
onChanged: (newValue) {
_postprovider.postCategory = newValue;
},
items: <String>["League Of Legends", "Steam", "Csgo"]
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
onTap: () => context.read<PostProvider>().postCategory,
value: value ?? constantValue,
child: Text(value ?? constantValue),
);
}).toList());
});
}
Widget下拉按钮(BuildContext上下文){
字符串constantValue=“传奇联盟”;
返回使用者(生成器:(\u上下文,\u后提供程序,\u小部件){
返回下拉按钮(
值:_postprovider.postCategory??常量值,
一旦更改:(newValue){
_postprovider.postCategory=新值;
},
物品:[“传奇联盟”、“蒸汽”、“Csgo”]
.map((字符串值){
返回下拉菜单项(
onTap:()=>context.read().postCategory,
值:值??恒定值,
子项:文本(值??常量值),
);
}).toList());
});
}
在此添加以下内容:
Future submitNewPost(BuildContext context) async {
final provider = Provider.of<PostProvider>(context, listen: false);
final isValid = _formKey.currentState.validate();
FocusScope.of(context).unfocus();
if (isValid) {
if( provider.postCategory == null || provider.postCategory.isEmpty){
provider.postCategory = "League Of Legends";
}
_formKey.currentState.save();
final isSuccess = await provider.createNewPost();
if (isSuccess) {
Navigator.of(context).pop();
} else {
final message = 'An error occurred, please check your inputs!';
Center(child: Text(message),);
}
}
}
Future submitNewPost(BuildContext上下文)异步{
final provider=provider.of(上下文,listen:false);
final isValid=_formKey.currentState.validate();
(上下文)的焦点范围。取消焦点();
如果(有效){
if(provider.postCategory==null | | provider.postCategory.isEmpty){
provider.postCategory=“传奇联盟”;
}
_formKey.currentState.save();
final isSuccess=等待提供程序。createNewPost();
如果(isSuccess){
Navigator.of(context.pop();
}否则{
最终消息='发生错误,请检查您的输入!';
中心(子:文本(消息),);
}
}
}
这可能是因为您仅在单击“提交”按钮时更新屏幕,而不是在关闭对话框时更新屏幕。您可以等待对话框弹出并重新加载页面状态。@javachipper,但我的代码(见上文)已经有了。看看下拉菜单项onTap函数。我应该记录什么?你可以发布你主页的代码吗?@javachipper当然。我编辑了。你好。我解决了这个问题,但仍然有一个问题。你写了值:_postprovider??constantValue,我把它改为值:_postprovider.postCategory??constantValue,它是OKy、 但constantvalue不起作用。如果我不选择任何值,它会给出这样的结果:必须向文本小部件提供一个非空字符串。“package:flatter/src/widgets/Text.dart”:失败的断言:第370行pos 10:“data!=null”我该怎么办?是的,constantvalue是看似,但只是看似。如果我不选择任何值,它会选择constantvalue Automatically我更新了我上面关于空值的答案我尝试过了,但它给出了相同的错误我不知道为什么。实际上你的代码是好的,但为什么它不工作。必须向文本小部件提供非空字符串。“package:flatter/src/widgets/Text.dart”:失败的断言:第370行pos 10:“data!=null”你到底什么时候遇到这个错误?从下拉列表中选择后,或在提交/点击提交按钮后?点击提交按钮,因此在发布后
Future submitNewPost(BuildContext context) async {
final provider = Provider.of<PostProvider>(context, listen: false);
final isValid = _formKey.currentState.validate();
FocusScope.of(context).unfocus();
if (isValid) {
if( provider.postCategory == null || provider.postCategory.isEmpty){
provider.postCategory = "League Of Legends";
}
_formKey.currentState.save();
final isSuccess = await provider.createNewPost();
if (isSuccess) {
Navigator.of(context).pop();
} else {
final message = 'An error occurred, please check your inputs!';
Center(child: Text(message),);
}
}
}