Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振:带有命名参数的AlertDialog和TextButton';启用';isn';t定义_Flutter - Fatal编程技术网

Flutter 颤振:带有命名参数的AlertDialog和TextButton';启用';isn';t定义

Flutter 颤振:带有命名参数的AlertDialog和TextButton';启用';isn';t定义,flutter,Flutter,我正在使用AlertDialog并希望根据条件启用或禁用。然而,我得到这个下划线错误说 未定义命名参数“enabled”。请尝试将名称更正为现有命名参数的名称,或使用名称“enabled”定义命名参数。 actions: <Widget>[ TextButton( enabled: false, child: Text('Approve'), onPressed: () async {

我正在使用
AlertDialog
并希望根据条件启用或禁用。然而,我得到这个下划线错误说

未定义命名参数“enabled”。请尝试将名称更正为现有命名参数的名称,或使用名称“enabled”定义命名参数。

  actions: <Widget>[
          TextButton(
            enabled: false,
            child: Text('Approve'),
            onPressed: () async {
              print('hello');
            },
          ),
        ],
操作:[
文本按钮(
启用:false,
子项:文本('Approve'),
onPressed:()异步{
打印(“你好”);
},
),
],


我怎样才能做到这一点;DR:
enabled
是一个动态getter,如果
onPressed
onLongPress
设置为非空值,则返回
true
,否则返回
false


可以通过调查
按钮样式按钮
找到答案,该按钮是
文本按钮
扩展的
,具体如下:

/// Whether the button is enabled or disabled.
///
/// Buttons are disabled by default. To enable a button, set its [onPressed]
/// or [onLongPress] properties to a non-null value.
bool get enabled => onPressed != null || onLongPress != null;
如果
text按钮的
onLongPress
onPressed
回调设置为非空值,则将启用
按钮

也就是说,根据您指定的场景,我们可以按如下方式构建
TextButton

bool _enabled = true;

Widget _buildTextButton() {
   return TextButton(
     child: Text('Approve'),
     onPressed: _enabled ? _onPressed() : null,
   );
)

void _toggleEnabled() {
   setState(() => _enabled = != _enabled);
}

void _onPressed() {
   print('pressed!');
}

您可以复制粘贴运行下面的完整代码
步骤1:您可以使用StatefulBuilder wrap AlertDialog
步骤2:调用
setState(()=>enable=true)符合您的条件
步骤3:在按下的
on中设置

onPressed: enable
        ? () {
            Navigator.of(context).pop();
          }
        : null),
代码片段

Future<void> _showMyDialog(bool enable) async {
    return showDialog<void>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
            return AlertDialog(
              title: Text('AlertDialog Title'),
              content: SingleChildScrollView(
                child: ListBody(
                  children: <Widget>[
                    Text('This is a demo alert dialog.'),
                    Text('Would you like to approve of this message?'),
                    TextButton(
                      child: Text("enable Approve"),
                      onPressed: () {
                        setState(() => enable = true);
                      },
                    ),
                  ],
                ),
              ),
              actions: <Widget>[
                TextButton(
                    child: Text('Approve'),
                    onPressed: enable
                        ? () {
                            Navigator.of(context).pop();
                          }
                        : null),
              ],
            );
          });
        });
  }
Future\u showMyDialog(bool enable)异步{
返回显示对话框(
上下文:上下文,
禁止:错误,
生成器:(BuildContext上下文){
返回状态生成器(
生成器:(BuildContext上下文,StateSetter setState){
返回警报对话框(
标题:文本(“AlertDialog标题”),
内容:SingleChildScrollView(
子:列表体(
儿童:[
Text('这是一个演示警报对话框'),
Text('您想批准此邮件吗?'),
文本按钮(
子项:文本(“启用批准”),
已按下:(){
设置状态(()=>enable=true);
},
),
],
),
),
行动:[
文本按钮(
子项:文本('Approve'),
onPressed:启用
? () {
Navigator.of(context.pop();
}
:null),
],
);
});
});
}
工作演示

完整代码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<void> _showMyDialog(bool enable) async {
    return showDialog<void>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return StatefulBuilder(
              builder: (BuildContext context, StateSetter setState) {
            return AlertDialog(
              title: Text('AlertDialog Title'),
              content: SingleChildScrollView(
                child: ListBody(
                  children: <Widget>[
                    Text('This is a demo alert dialog.'),
                    Text('Would you like to approve of this message?'),
                    TextButton(
                      child: Text("enable Approve"),
                      onPressed: () {
                        setState(() => enable = true);
                      },
                    ),
                  ],
                ),
              ),
              actions: <Widget>[
                TextButton(
                    child: Text('Approve'),
                    onPressed: enable
                        ? () {
                            Navigator.of(context).pop();
                          }
                        : null),
              ],
            );
          });
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _showMyDialog(false);
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
Future\u showMyDialog(布尔启用)异步{
返回显示对话框(
上下文:上下文,
禁止:错误,
生成器:(BuildContext上下文){
返回状态生成器(
生成器:(BuildContext上下文,StateSetter setState){
返回警报对话框(
标题:文本(“AlertDialog标题”),
内容:SingleChildScrollView(
子:列表体(
儿童:[
Text('这是一个演示警报对话框'),
Text('您想批准此邮件吗?'),
文本按钮(
子项:文本(“启用批准”),
已按下:(){
设置状态(()=>enable=true);
},
),
],
),
),
行动:[
文本按钮(
子项:文本('Approve'),
onPressed:启用
? () {
Navigator.of(context.pop();
}
:null),
],
);
});
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),
],
),
),
浮动操作按钮:浮动操作按钮(
已按下:(){
_showMyDialog(假);
},
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
}

哦,所以我认为这是TextButton的直接属性的一部分。非常感谢你!