【问题标题】:Can I make Toast text change immediately without waiting the toast end?我可以在不等待 toast 结束的情况下立即更改 Toast 文本吗?
【发布时间】:2022-01-08 18:09:21
【问题描述】:

在我的应用程序中,我为我的活动构建了一个 pdf 查看器小部件,当我将其滚动到上一页或下一页时,我让它敬酒并显示当前页码。

问题是,toast 需要时间显示,当我滚动它的速度足够快时,例如,我滚动到页面“5”和“06”并滚动到“07”而没有暂停,我必须等待 Toast “05”、“06”、“07”中的“05”、“06”、“07”一个一个慢慢展示。

我可以不等待就立即显示特定的 Toast 吗?

class _PdfoneState extends State<Pdfone> {
 int currentPage1 = 0;

 SharedPreferences? preferences;
 final toast = FToast();

 @override
 void initState() {
   super.initState();
   toast.init(context);
   initializePreference().whenComplete(() {
     setState(() {});
   });
 }

 @override
 Widget build(BuildContext context) {
   return Container(
       child: PDF(
     enableSwipe: true,
     pageFling: b,
     fitPolicy: FitPolicy.BOTH,
     nightMode: a,
     onPageChanged: (page, total) {
       setState(() {
         currentPage1 = page!;
         this.preferences?.setInt("page1", currentPage1);
       });
       Widget buildToast() => Container(
             padding: EdgeInsets.symmetric(vertical: 10, horizontal: 14),
             decoration: BoxDecoration(
               borderRadius: BorderRadius.circular(25),
               color: Colors.white70,
             ),
             child: Row(
               mainAxisAlignment: MainAxisAlignment.center,
               crossAxisAlignment: CrossAxisAlignment.center,
               mainAxisSize: MainAxisSize.min,
               children: [
                 Text(
                   "${page! + 1} / $total",
                   style: TextStyle(fontSize: 16),
                 )
               ],
             ),
           );
       void showToast() => toast.showToast(
             child: buildToast(),
             positionedToastBuilder: (context, child) => Positioned(
               child: child,
               top: 32,
               left: 22,
             ),
             fadeDuration: 300,
           );
       showToast();
     },

【问题讨论】:

  • 在你的情况下FToast 是什么?
  • 我正在使用flutterToast包并构建一个自定义吐司你需要使用FToast类

标签: flutter dart flutter-layout toast


【解决方案1】:

您可以清除之前的 toast 并显示当前的 toast

// To remove present shwoing toast
fToast.removeCustomToast()
 
// To clear the queue
fToast.removeQueuedCustomToasts();
void showToast(){ 
  toast.removeCustomToast(); // based on your need
  toast.removeQueuedCustomToasts();

  toast.showToast(
             child: buildToast(),
             positionedToastBuilder: (context, child) => Positioned(
               child: child,
               top: 32,
               left: 22,
             ),
             fadeDuration: 300,
           );
    }

更多关于fluttertoast

【讨论】:

  • 它的工作谢谢
猜你喜欢
  • 2019-06-21
  • 1970-01-01
  • 1970-01-01
  • 2018-12-30
  • 1970-01-01
  • 2017-04-27
  • 2014-02-27
  • 1970-01-01
  • 2021-09-11
相关资源
最近更新 更多