简体   繁体   English

为什么我的 toast 消息不起作用或在 Flutter 中显示?

[英]Why my toast message not working or displayed in Flutter?

I have a scrollable listView full of quotes, now i want to display a toast message whenever the user scrolled to the bottom of the list.我有一个充满引号的可滚动列表视图,现在我想在用户滚动到列表底部时显示一条吐司消息。 Tight now, I use scroll_edge_listener to detect the scrolling position.现在,我使用 scroll_edge_listener 来检测滚动位置。 I tried to use 'if' condition to create a function to display a toast message, but it does not seem to be working.我尝试使用“if”条件来创建一个显示 toast 消息的函数,但它似乎不起作用。 Really appreciate any help.非常感谢任何帮助。 Anyway, this is my code.无论如何,这是我的代码。 Still new to Flutter. Flutter 还是新手。

import 'package:flutter/material.dart';
import 'quote.dart';
import 'package:scroll_edge_listener/scroll_edge_listener.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() => runApp(MaterialApp(
  home: QuoteList(),
));

class QuoteList extends StatefulWidget {
@override
_QuoteListState createState() => _QuoteListState();
}

class _QuoteListState extends State<QuoteList> {
final controller = ScrollController(); //For detecting sroll activity
int index = 0;

List<Quote> quotes = [
Quote(author: 'Osca Wilde', text: '1'),
Quote(author: 'Oscar Wilde', text: '2'),
Quote(author: 'Osca Wilde', text: '3'),
Quote(author: 'Oscar Wilde', text: '4'),
Quote(author: 'Osca Wilde', text: '5'),
Quote(author: 'Oscar Wilde', text: '6'),
Quote(author: 'Osca Wilde', text: '7'),
Quote(author: 'Oscar Wilde', text: '8'),
Quote(author: 'Oscar Wilde', text: '6'),
Quote(author: 'Osca Wilde', text: '7'),
Quote(author: 'Oscar Wilde', text: '8'),
];

Widget quoteTemplate(quote) {
return Card(
  margin: EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
  child: Padding(
    padding: const EdgeInsets.all(12.0),
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        Text(
          quote.text,
          style: TextStyle(
            fontSize: 18.0,
            color: Colors.grey[800],
          ),
        ),
        SizedBox(height: 6.0),
        Text(
          quote.author,
          style: TextStyle(
            fontSize: 14.0,
            color: Colors.grey[800],
          ),
        )
      ],
    ),
  ),
);
}

var refreshKey = GlobalKey<RefreshIndicatorState>();

@override
void initState() {
super.initState();

controller.addListener(listenScrolling);
}

void listenScrolling() {
if (controller.position.atEdge) {
  final isTop = controller.position.pixels == 0;

  if (isTop) {
    Fluttertoast.showToast(
        msg: "This is top of the list",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0);
   } else {
    Fluttertoast.showToast(
        msg: "This is bottom of the list",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0);
    }
  }
 }

@override
Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.grey[200],
  appBar: AppBar(
    title: Text('Awesome Quotes'),
    centerTitle: true,
    backgroundColor: Colors.redAccent,
  ),
  body: RefreshIndicator(
    key: refreshKey,
    onRefresh: refreshlist,
    child: ListView(
      children: quotes.map((quote) => quoteTemplate(quote)).toList(),
    ),
  ),
 );
}

Future<void> refreshlist() async {
refreshKey.currentState?.show(atTop: false);
await Future.delayed(Duration(seconds: 1));
quotes.shuffle();
setState(() {});
}
}

My quote.dart model class我的 quote.dart 模型类

class Quote {
String text;
String author;

Quote({required this.text, required this.author});
}

in which scroll do you use your controller?你在哪个卷轴上使用你的控制器? You should use it in your ListView.您应该在 ListView 中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法使用 Flutter 显示我的 toast 消息 - I can't show my toast message with Flutter flutter 中吐司消息的背景颜色 - background color of the toast message in a flutter 在 flutter 中复制文本后显示吐司消息 - Show toast message after copy text in flutter 为什么我的 RaisedButton 在 Flutter 中不起作用? - Why is my RaisedButton not working in Flutter? Flutter:为什么我的 SharedPreferences 不起作用? - Flutter: Why my SharedPreferences is not working? 为什么我的 setState 在 Flutter 中不起作用? - Why is my setState not working in Flutter? 当我尝试在 initState 使用连接活动颤动时,为什么我的 toast 无法显示? - Why my toast cannot show when i try use connect activity flutter at initState? 为什么破折号在 Android 上显示为破折号,但在我的 Flutter 移动应用程序中的 iOS 上显示为下划线? - Why is a dash displayed as a dash on Android, but displayed as an underscore on iOS in my Flutter mobile app? 颤振中的吐司消息样式变化? 只有 Toast 消息没有 Snackbar 其他没有小部件 - Toast Message Style Change in flutter? Only Toast Message not Snackbar Other No Widgets 为什么我的更新 Email 不起作用,Firebase 和 Flutter? - Why My Update Email is not WORKING, Firebase and Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM