Pen Settings

JavaScript

Babel includes JSX processing.

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

Flutter

              
                import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('NotificationListener 事件通知 - 自定义事件通知'),
        ),
        body: MyNotificationPage(),
      ),
    );
  }
}

// codepen 演示无效,需要在模拟器或真机上运行

class MyNotification extends Notification {
  final String msg;
  MyNotification(this.msg);
}

class MyNotificationPage extends StatefulWidget {
  @override
  _MyNotificationPageState createState() => _MyNotificationPageState();
}

class _MyNotificationPageState extends State<MyNotificationPage> {
  String _msg = "";
  @override
  Widget build(BuildContext context) {
    return NotificationListener(
      onNotification: (notification) {
        setState(() {
          _msg += notification.msg + ' ';
        });
        return true;
      },
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Builder(
              builder: (context) {
                return ElevatedButton(
                  onPressed: () => MyNotification("Hi").dispatch(context),
                  child: Text(
                    "发送通知",
                    style: TextStyle(fontSize: 20),
                  ),
                );
              },
            ),
            Text(
              '收到通知:' + _msg,
              style: TextStyle(fontSize: 16),
            )
          ],
        ),
      ),
    );
  }
}

              
            
!
999px

Console