如何在flutter中根据文本使SnackBar宽度灵活?

问题描述 投票:0回答:3

Snackbar
宽度只能用参数width来设置,否则使用全屏宽度。

有什么办法可以让它变得灵活吗?

编辑:如下所示。

flutter flutter-layout snackbar
3个回答
0
投票

有一个非常有用的软件包可供您使用 - fluttertoast

简单写-

Fluttertoast.showToast(
        msg: "This is Center Short Toast",
    );

您可以添加

toastLength
使其变短或变长 -

Fluttertoast.showToast(
        msg: "This is Center Short Toast",
        toastLength: Toast.LENGTH_SHORT,
    );

希望这有帮助。让我知道。


0
投票

这里有一个不使用包的棘手方法:

ScaffoldMessenger.of(context).showSnackBar(
  SnackBar(
    backgroundColor: Colors.transparent,
    elevation: 0,
    padding: EdgeInsets.zero,
    duration: const Duration(seconds: 2),
    behavior: SnackBarBehavior.floating,
    margin: const EdgeInsets.only(bottom: 76.0),
    content: Theme(
      data: ThemeData.dark(),  // Use different theme with main app!
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Card(
            shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(2.0))),
            child: Padding(
              padding: const EdgeInsets.symmetric(
                  horizontal: 16.0, vertical: 12.0),
              child: Text(text),
            ),
          ),
        ],
      ),
    ),
  ),
);

-2
投票

另一个解决方案是使用oktoast,我们可以通过位置参数改变位置

showToast(
      "$_counter",
      duration: Duration(seconds: 2),
      position: ToastPosition.bottom,
      backgroundColor: Colors.black.withOpacity(0.8),
      radius: 13.0,
      textStyle: TextStyle(fontSize: 18.0),
    );

© www.soinside.com 2019 - 2024. All rights reserved.