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

Save Automatically?

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 'dart:html';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: FlutterPen(),
      theme: ThemeData(
        brightness: Brightness.dark,
      ),
    ),
  );
}

class FlutterPen extends StatefulWidget {

  @override
  _FlutterPenState createState() => _FlutterPenState();

}

class _FlutterPenState extends State<FlutterPen> {
  
  static const int keyW = 119;
  static const int keyA = 97;
  static const int keyS = 115;
  static const int keyD = 100;
  static const int arrowUp = 4295426130;
  static const int arrowLeft = 4295426128;
  static const int arrowDown = 4295426129;
  static const int arrowRight = 4295426127;
  static const int maskW = 8;
  static const int maskA = 4;
  static const int maskS = 2;
  static const int maskD = 1;

  num top = -1;
  num left = -1;
  num moveBy = 15;
  int state = 0;

  @override
  void initState() {
    super.initState();
  }
  
  @override
  Widget build(BuildContext context) {
    if(left == -1) {
      left = (MediaQuery.of(context).size.width-PenConfig.logoSize)/2;
      top = (MediaQuery.of(context).size.height-PenConfig.logoSize)/2;
    }
    return RawKeyboardListener(
      autofocus: true,
      focusNode: FocusNode(),
      onKey: (RawKeyEvent keyboardEvent) { 
        setState(() {
          if(keyboardEvent.runtimeType == RawKeyDownEvent) {
            switch(keyboardEvent.logicalKey.keyId) {
              case keyD:
              case arrowRight:
                state |= maskD;
                break;
              case keyS:
              case arrowDown:
                state |= maskS;
                break;
              case keyA:
              case arrowLeft:
                state |= maskA;
                break;
              case keyW:
              case arrowUp:
                state |= maskW;
                break;
            }
          } else {
            switch(keyboardEvent.logicalKey.keyId) {
              case keyD:
              case arrowRight:
                state &= ~maskD;
                break;
              case keyS:
              case arrowDown:
                state &= ~maskS;
                break;
              case keyA:
              case arrowLeft:
                state &= ~maskA;
                break;
              case keyW:
              case arrowUp:
                state &= ~maskW;
                break;
            }
          }
          if(state == 0) {
            left = (MediaQuery.of(context).size.width-PenConfig.logoSize)/2;
            top = (MediaQuery.of(context).size.height-PenConfig.logoSize)/2;
          } else if(state != (maskA+maskD+maskW+maskS) 
            && state != (maskA+maskD) 
            && state != (maskW+maskS)) {
            left += (state & maskD == maskD)?moveBy:0; // move right
            top += (state & maskS == maskS)?moveBy:0; // move down
            left -= (state & maskA == maskA)?moveBy:0; // move left
            top -= (state & maskW == maskW)?moveBy:0; // move up
          }
        });
      },
      child: Scaffold(
        backgroundColor: PenColors.scaffold,
        body: Center(
          child: Stack(
            children: [
              AnimatedPositioned(
                top: top,
                left: left,
                duration: Duration(milliseconds: 300),
                child: Container(
                  width: PenConfig.logoSize,
                  height: PenConfig.logoSize,
                  child: Image.network('https://assets.codepen.io/4703670/logo.png')
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
  
  @override
  void dispose() {
    super.dispose();
  }
}

class PenColors {
  static const scaffold = const Color(0xFF1a1b1d);
}

class PenData {
}

class PenConfig {
  static const num logoSize = 200;
}

class Utils {
  static bool notNull(Object o) => o != null;

  static void showSnack(BuildContext context, String msg) {
    Scaffold.of(context).showSnackBar(
      SnackBar(content: Text(msg)),
    );
  }
}
              
            
!
999px

Console