[英]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.