Flutter - FloatingActionButton中的SimpleDialog

raf*_*b21 15 dart flutter

我试图SimpleDialog在敲击之后创建一个FloatingActionButton,但是当按下该按钮时没有任何反应.

我做错了什么?

import "package:flutter/material.dart";

void main() {
  runApp(new ControlleApp());
}

class ControlleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) => new Scaffold(
    appBar: new AppBar(
      backgroundColor: new Color(0xFF26C6DA),
    ),
    floatingActionButton: new FloatingActionButton(
      tooltip: 'Add',
      child: new Icon(Icons.add),
      backgroundColor: new Color(0xFFF44336),
      onPressed: (){
        new SimpleDialog(
          title: new Text('Test'),
          children: <Widget>[
            new RadioListTile(
              title: new Text('Testing'), value: null, groupValue: null, onChanged: (value) {},
            )
          ],
        );
      }     
    ),    
  );
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*van 37

我注意到接受的答案是使用childshowDialog这实际上是过时了,所以我会建议避免它.你应该使用builder,我提供了一个例子:

onPressed: () {
    showDialog(
        context: context,
        builder: (_) => new AlertDialog(
            title: new Text("Dialog Title"),
            content: new Text("This is my content"),
        )
    );
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*ard 17

你需要将它包装在show ActionDialog上

showDialog(context: context, child:
    new AlertDialog(
      title: new Text("My Super title"),
      content: new Text("Hello World"),
    )
);
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是对的.您最终可能希望得到对话框的结果,在这种情况下,您可以"等待"`showDialog`的结果并将要返回的值放入调用`Navigator.pop()`中对话框动作. (4认同)
  • 这现在已经过时了 (2认同)

Dak*_*ksh 8

有一个特定的场景在显示对话框时应该注意 floatingActionButton

如果你这样写你的代码

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
            onPressed: () {
              showDialog(
                  context: context,
                  builder: (ctxt) => new AlertDialog(
                    title: Text("Text Dialog"),
                  )
              );
            }),
      )
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

它不会显示警报对话框,但会引发异常“未找到 MaterialLocalizations”。

MaterialApp不是调用对话框的根时会发生这种情况。在这种情况下,根小部件是应用程序。但是,如果我们将代码更改为

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyAppImpl()
    );
  }
}

class MyAppImpl extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
          onPressed: () {
            showDialog(
                context: context,
                builder: (ctxt) => new AlertDialog(
                  title: Text("Text Dialog"),
                )
            );
          }),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

MaterialApp成为根,一切工作正常。在这种情况下,flutter 会自动创建材质定位,否则需要手动创建。

我在官方文档中没有找到任何相同的文档。

希望能帮助到你