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 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: ParentWidget(),
    ),
  );
}

class ParentWidget extends StatefulWidget {
  @override
  _ParentWidgetState createState() => _ParentWidgetState();
}

class _ParentWidgetState extends State<ParentWidget> {

  bool _isInside = true;

  @override
  Widget build(BuildContext context) {
    Widget child;
    // 在这里有个 ChildWidget 移动 widget 树层级的操作
    // 这个就会导致无法复用了
    if (_isInside) {
      child = Container(
        child: ChildWidget(),
      );
    } else {
      child = ChildWidget();
    }

    child = GestureDetector(
      onTap: () {
        setState(() {
          _isInside = !_isInside;
        });
      },
      child: child,
    );
    
    child = Center(
      child: child,
    );

    return child;
  }
}

class ChildWidget extends StatefulWidget {
  @override
  _ChildWidgetState createState() => _ChildWidgetState();
}

class _ChildWidgetState extends State<ChildWidget> {

  @override
  void initState() {
    super.initState();
    print("每次 parent widget 触发了 onTap,我都会被调动");
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 100,
      height: 100,
      color: Colors.red,
    );
  }
}
              
            
!
999px

Console