Flutter中带有圆形背景的对话框

时间:2019-11-26 06:41:31

标签: user-interface flutter widget

我想创建一个具有圆角背景的对话框,并且该背景具有3像素的笔触,就像附加的图像一样。我在下面的代码中使用了圆角,但是如何向背景添加笔触?

showDialog(
        context: context,
        builder: (BuildContext context) {
          return Dialog(
             backgroundColor: pinkBackground,
             shape: RoundedRectangleBorder(borderRadius: 
                BorderRadius.all(Radius.circular(10.0))),
  child: Text(
    "title",

    style: getBodyTextStyle(),
  )
);
        },
      );

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试将Container添加为您的Dialog子并在其中声明BoxDecoration

 showDialog(
    context: context,
    builder: (BuildContext context) {
    return Dialog(
    backgroundColor: AppColors.colorGreen,
    shape: RoundedRectangleBorder(
    borderRadius:
    BorderRadius.all(Radius.circular(10.0))),
    child: Container(
    decoration: BoxDecoration(
    border: Border.all(color: Colors.blueAccent,width: 2),
    borderRadius:
    BorderRadius.all(Radius.circular(10.0))),
    child: Padding(
    padding: const EdgeInsets.all(16.0),
    child: Text(
     "title",
        ),
     ),
    ));
  },
);

输出

enter image description here

答案 1 :(得分:1)

本地应用:

showDialog(
  context: context,
  builder: (context) {
    return Dialog(
      backgroundColor: Colors.grey,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(10.0)),
        side: BorderSide(width: 3.0, color: Colors.black),
      ),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Text(
          "title",
          style: getBodyTextStyle(),
        ),
      ),
    );
  },
);

全局应用:

class Application extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final baseThemeData = ThemeData.light();
    final themeData = baseThemeData.copyWith(
      dialogTheme: baseThemeData.dialogTheme.copyWith(
        backgroundColor: Colors.grey,
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(10.0)),
          side: BorderSide(width: 3.0, color: Colors.black),
        ),
      ),
    );

    return MaterialApp(
      themeMode: ThemeMode.light,
      theme: themeData,
      ...
    );
  }

  void _openDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) {
        return Dialog(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Text(
              "title",
              style: getBodyTextStyle(),
            ),
          ),
        );
      },
    );
  }
}

两个变体的结果:

result