fbpx
Escolha uma Página
Como Funcionam as Aulas
Você está pronto para começar a programar?

O dart.rocks tem tudo a ver com aprender fazendo e programando. Nesta aula, mostraremos os princípios básicos de como usar o dart.rocks e mostraremos um pouco do poder de aprender a programar!

A seção abaixo deste texto contém instruções sobre o que fazer e como prosseguir para o próximo exercício.

O painel a seguir é nosso Playground e contém um arquivo de código que pode ser executado no seu navegador.


  {$ begin main.dart $}
import 'dart:math';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart' show timeDilation;

// INICIO - ALTERE O TEXTO ENTRE AS ASPAS DUPLAS

var texto = "Bem-vindo(a)"; // <<<<<<<<<<<

// ALTERE O TEXTO ENTRE AS ASPAS DUPLAS - FIM

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          body: Stack(children: <Widget>[
        Particle(),
        Center(
            child: Text(texto,
                style: TextStyle(fontSize: 80, fontWeight: FontWeight.bold))),
      ])),
    );
  }
}

class Particle extends StatefulWidget {
  @override
  _ParticleState createState() => new _ParticleState();

  Particle() {
    timeDilation = 2.0;
  }
}

class _ParticleState extends State<Particle> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ParticleBody(screenSize: MediaQuery.of(context).size),
    );
  }
}

class ParticleBody extends StatefulWidget {
  final Size screenSize;

  ParticleBody({Key key, @required this.screenSize}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new _ParticleBodyState();
  }
}

class _ParticleBodyState extends State<ParticleBody> with TickerProviderStateMixin {
  AnimationController animationController;
  final nodeList = <Node>[];
  final numNodes = 40;

  @override
  void initState() {
    super.initState();

    // Generate list of node
    new List.generate(numNodes, (i) {
      nodeList.add(new Node(id: i, screenSize: widget.screenSize));
    });

    animationController = new AnimationController(
        vsync: this, duration: new Duration(seconds: 1))
      ..addListener(() {
        for (int i = 0; i < nodeList.length; i++) {
          nodeList[i].move(animationController.value);
          for (int j = i + 1; j < nodeList.length; j++) {
            nodeList[i].connect(nodeList[j]);
          }
        }
      })
      ..repeat();
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Container(
      child: new AnimatedBuilder(
        animation: new CurvedAnimation(
            parent: animationController, curve: Curves.easeInOut),
        builder: (context, child) => new CustomPaint(
          size: widget.screenSize,
          painter: new _DemoPainter(widget.screenSize, nodeList),
        ),
      ),
    );
  }
}

class _DemoPainter extends CustomPainter {
  final List<Node> nodeList;
  final Size screenSize;

  _DemoPainter(this.screenSize, this.nodeList);

  @override
  void paint(Canvas canvas, Size size) {
    for (var node in nodeList) {
      node.display(canvas);
    }
  }

  @override
  bool shouldRepaint(_DemoPainter oldDelegate) => true;
}

enum Direction {
  LEFT,
  RIGHT,
  TOP,
  BOTTOM,
  TOP_LEFT,
  TOP_RIGHT,
  BOTTOM_LEFT,
  BOTTOM_RIGHT
}

class Node {
  int id;
  Size screenSize;
  double radius;
  double size;
  Offset position;
  Direction direction;
  Random random;
  Paint notePaint, linePaint;

  Map<int, Node> connected;

  Node(
      {@required this.id,
      this.size = 5.0,
      this.radius = 200.0,
      @required this.screenSize}) {
    random = new Random();
    connected = new Map();
    position = screenSize.center(Offset.zero);
    direction = Direction.values[random.nextInt(Direction.values.length)];

    notePaint = new Paint()
      ..color = Colors.orange
      ..strokeWidth = 1.0
      ..style = PaintingStyle.fill;
    linePaint = new Paint()
      ..color = Colors.orange
      ..strokeWidth = 0.5
      ..style = PaintingStyle.stroke;
  }

  void move(double seed) {
    switch (direction) {
      case Direction.LEFT:
        position -= new Offset(1.0 + seed, 0.0);
        if (position.dx <= 5.0) {
          List<Direction> dirAvailableList = [
            Direction.RIGHT,
            Direction.BOTTOM_RIGHT,
            Direction.TOP_RIGHT
          ];
          direction = dirAvailableList[random.nextInt(dirAvailableList.length)];
        }

        break;
      case Direction.RIGHT:
        position += new Offset(1.0 + seed, 0.0);
        if (position.dx >= screenSize.width - 5.0) {
          List<Direction> dirAvailableList = [
            Direction.LEFT,
            Direction.BOTTOM_LEFT,
            Direction.TOP_LEFT
          ];
          direction = dirAvailableList[random.nextInt(dirAvailableList.length)];
        }
        break;
      case Direction.TOP:
        position -= new Offset(0.0, 1.0 + seed);
        if (position.dy <= 5.0) {
          List<Direction> dirAvailableList = [
            Direction.BOTTOM,
            Direction.BOTTOM_LEFT,
            Direction.BOTTOM_RIGHT
          ];
          direction = dirAvailableList[random.nextInt(dirAvailableList.length)];
        }
        break;
      case Direction.BOTTOM:
        position += new Offset(0.0, 1.0 + seed);
        if (position.dy >= screenSize.height - 5.0) {
          List<Direction> dirAvailableList = [
            Direction.TOP,
            Direction.TOP_LEFT,
            Direction.TOP_RIGHT,
          ];
          direction = dirAvailableList[random.nextInt(dirAvailableList.length)];
        }
        break;
      case Direction.TOP_LEFT:
        position -= new Offset(1.0 + seed, 1.0 + seed);
        if (position.dx <= 5.0 || position.dy <= 5.0) {
          List<Direction> dirAvailableList = [
            Direction.BOTTOM_RIGHT,
          ];

          //if y invalid and x valid
          if (position.dy <= 5.0 && position.dx > 5.0) {
            dirAvailableList.add(Direction.LEFT);
            dirAvailableList.add(Direction.RIGHT);
            dirAvailableList.add(Direction.BOTTOM);
            dirAvailableList.add(Direction.BOTTOM_LEFT);
          }
          //if x invalid and y valid
          if (position.dx <= 5.0 && position.dy > 5.0) {
            dirAvailableList.add(Direction.TOP);
            dirAvailableList.add(Direction.RIGHT);
            dirAvailableList.add(Direction.BOTTOM);
            dirAvailableList.add(Direction.TOP_RIGHT);
          }

          direction = dirAvailableList[random.nextInt(dirAvailableList.length)];
        }
        break;
      case Direction.TOP_RIGHT:
        position -= new Offset(-1.0 - seed, 1.0 + seed);
        if (position.dx >= screenSize.width - 5.0 || position.dy <= 5.0) {
          List<Direction> dirAvailableList = [
            Direction.BOTTOM_LEFT,
          ];

          //if y invalid and x valid
          if (position.dy <= 5.0 && position.dx < screenSize.width - 5.0) {
            dirAvailableList.add(Direction.LEFT);
            dirAvailableList.add(Direction.RIGHT);
            dirAvailableList.add(Direction.BOTTOM);
            dirAvailableList.add(Direction.BOTTOM_RIGHT);
          }
          //if x invalid and y valid
          if (position.dx >= screenSize.width - 5.0 && position.dy > 5.0) {
            dirAvailableList.add(Direction.TOP);
            dirAvailableList.add(Direction.BOTTOM);
            dirAvailableList.add(Direction.LEFT);
            dirAvailableList.add(Direction.TOP_LEFT);
          }

          direction = dirAvailableList[random.nextInt(dirAvailableList.length)];
        }
        break;
      case Direction.BOTTOM_LEFT:
        position -= new Offset(1.0 + seed, -1.0 + seed);
        if (position.dx <= 5.0 || position.dy >= screenSize.height - 5.0) {
          List<Direction> dirAvailableList = [
            Direction.TOP_RIGHT,
          ];
          //if y invalid and x valid
          if (position.dy >= screenSize.height - 5.0 && position.dx > 5.0) {
            dirAvailableList.add(Direction.LEFT);
            dirAvailableList.add(Direction.RIGHT);
            dirAvailableList.add(Direction.TOP);
            dirAvailableList.add(Direction.TOP_LEFT);
          }
          //if x invalid and y valid
          if (position.dx <= 5.0 && position.dy < screenSize.height - 5.0) {
            dirAvailableList.add(Direction.TOP);
            dirAvailableList.add(Direction.BOTTOM);
            dirAvailableList.add(Direction.RIGHT);
            dirAvailableList.add(Direction.BOTTOM_RIGHT);
          }

          direction = dirAvailableList[random.nextInt(dirAvailableList.length)];
        }
        break;
      case Direction.BOTTOM_RIGHT:
        position += new Offset(1.0 + seed, 1.0 + seed);
        if (position.dx >= screenSize.width - 5.0 ||
            position.dy >= screenSize.height - 5.0) {
          List<Direction> dirAvailableList = [
            Direction.TOP_LEFT,
          ];
          //if y invalid and x valid
          if (position.dy >= screenSize.height - 5.0 &&
              position.dx < screenSize.width - 5.0) {
            dirAvailableList.add(Direction.LEFT);
            dirAvailableList.add(Direction.RIGHT);
            dirAvailableList.add(Direction.TOP);
            dirAvailableList.add(Direction.TOP_RIGHT);
          }
          //if x invalid and y valid
          if (position.dx >= screenSize.width - 5.0 &&
              position.dy < screenSize.height - 5.0) {
            dirAvailableList.add(Direction.TOP);
            dirAvailableList.add(Direction.BOTTOM);
            dirAvailableList.add(Direction.LEFT);
            dirAvailableList.add(Direction.BOTTOM_LEFT);
          }

          direction = dirAvailableList[random.nextInt(dirAvailableList.length)];
        }
        break;
    }
  }

  bool canConnect(Node node) {
    double x = node.position.dx - position.dx;
    double y = node.position.dy - position.dy;
    double d = x * x + y * y;
    return d <= radius * radius;
  }

  void connect(Node node) {
    if (canConnect(node)) {
      if (!node.connected.containsKey(id)) {
        connected.putIfAbsent(node.id, () => node);
      }
    } else if (connected.containsKey(node.id)) {
      connected.remove(node.id);
    }
  }

  void display(Canvas canvas) {
    canvas.drawCircle(position, size, notePaint);

    connected.forEach((id, node) {
      canvas.drawLine(position, node.position, linePaint);
    });
  }

  bool operator ==(o) => o is Node && o.id == id;

  int get hashCode => id;
}

  {$ end main.dart $}
  {$ begin solution.dart $}
  {$ end solution.dart $}
  {$ begin test.dart $}
  {$ end test.dart $}
  {$ begin hint.txt $}
  {$ end hint.txt $}

Por agora, ignore todo o código. Apenas altere o texto que está em as aspas duplas e clique no botão Run para ver a mágica acontecer.

Clique no link da próxima aula no canto direito inferior quando estiver pronto para começar a programar!

Pronto para acelerar? Aproveite a promoção de outubro, e assine nosso plano vitalício.