赞
踩
例如监听ListView的滚动状态,就可以使用ScrollNotification,代码如下:
class _ScrollNotificationState extends State<ScrollNotificationPage> { String _message = '通知'; get palyload => _message; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Column( children: [ Container( color: Colors.green, height: 50, width: MediaQuery.of(context).size.width, child: Center( child: Text( _message, style: const TextStyle(fontSize: 30, color: Colors.white), ), ), ), Expanded( child: NotificationListener<ScrollNotification>( child: ListView.builder( itemBuilder: (BuildContext, index) { return ListTile( title: Text('第$index个猴子'), ); }, itemCount: 50, ), onNotification: (scrollNotification) { if (scrollNotification is ScrollStartNotification) { _onScrollStart(scrollNotification.metrics); } else if (scrollNotification is ScrollUpdateNotification) { _onScrollUpdate(scrollNotification.metrics); } else if (scrollNotification is ScrollEndNotification) { _onScrollEnd(scrollNotification.metrics); } return false; }, )) ], )); } void _onScrollStart(ScrollMetrics scrollMetrics) { print(scrollMetrics.pixels); setState(() { this._message = '滚动开始'; }); showNotification(); } void _onScrollUpdate(ScrollMetrics scrollMetrics) { print(scrollMetrics.pixels); setState(() { this._message = '滚动中'; }); showNotification(); FlutterAppBadger.updateBadgeCount(10); } void _onScrollEnd(ScrollMetrics scrollMetrics) { print(scrollMetrics.pixels); setState(() { this._message = '滚动结束'; }); showNotification(); } late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; @override void initState() { super.initState(); flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); var android = const AndroidInitializationSettings('@mipmap/ic_launcher'); var ios = const IOSInitializationSettings(); var initSettings = InitializationSettings(android: android, iOS: ios); flutterLocalNotificationsPlugin.initialize(initSettings); } showNotification() async { var android = const AndroidNotificationDetails('channel id', 'channel NAME', priority: Priority.high, importance: Importance.max); var iOS = IOSNotificationDetails(); var platform = NotificationDetails(android: android, iOS: iOS); await flutterLocalNotificationsPlugin.show(0, '通知', '通知:$_message', platform, payload: '通知界面'); } Future onSelectNotification(String payload) async { await Navigator.push(context, MaterialPageRoute(builder: (context) => ScrollNotificationPage(title: palyload)),); } }
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; @override void initState() { super.initState(); flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); var android = const AndroidInitializationSettings('@mipmap/ic_launcher'); var ios = const IOSInitializationSettings(); var initSettings = InitializationSettings(android: android, iOS: ios); flutterLocalNotificationsPlugin.initialize(initSettings); } showNotification() async { var android = const AndroidNotificationDetails('channel id', 'channel NAME', priority: Priority.high, importance: Importance.max); var iOS = IOSNotificationDetails(); var platform = NotificationDetails(android: android, iOS: iOS); await flutterLocalNotificationsPlugin.show(0, '通知', '通知:$_message', platform, payload: '通知界面'); } Future onSelectNotification(String payload) async { await Navigator.push(context, MaterialPageRoute(builder: (context) => ScrollNotificationPage(title: palyload)),); }
运行效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。