Memo : Flutter x Dart [2015-2016]
  • Introduction
  • hello
    • helloworld
    • build flutter
    • build as standalone android app(old)
      • draw text
      • draw image
      • set icon
    • build as standalone apk(now)
    • build as ios app
    • build as linux app
    • build as mac app
    • build mojo
    • build as chromeapp
  • rendering
    • draw_rect
    • anime_rect
    • anime_rect(use animation.dart)
    • draw_image
    • rotate_image
    • draw_image_from_assets
    • sound_test(rendering & service)
    • touch_test
    • multitouch_test
    • [@]demo::mini game(spacewar!)
    • draw_text
    • draw_path
    • draw_vertexs
    • draw_vertexs_with_image
    • [@]demo:3d anime
    • input text from ime(RenderObject)
  • widget
    • text
    • network image
    • assets image
    • raw image
    • container
    • row
    • col
    • flexible
    • navigator
    • stateful component
    • scaffold
    • scaffold x drawer
    • scrollable viewport
    • scrollable list
    • input text from ime(EditableText)
  • dart:io
    • HttpClient Post & Get
    • NetworkInterface
    • TCP Socket
    • UDP Socket
    • File Path(dart:io & service)
    • File Create & Permission
  • dart:ui
    • draw rect
    • pointer event
  • mojo for flutter
    • get and post request
    • sensor test
    • [@]demo:gravity sensor
  • 2d physics for scratch
    • collision without rotation
    • [@]democollision without friction
    • [b]
  • 2d physics for newton
  • mojo for native
    • build mojo
  • 48 hour trial
    • create iron heart
    • tiny tetris
Powered by GitBook
On this page

Was this helpful?

  1. mojo for flutter

[@]demo:gravity sensor

Previoussensor testNext2d physics for scratch

Last updated 5 years ago

Was this helpful?

//
// following code is checked in 2016/01/13
//
import 'package:flutter/services.dart';
import 'package:mojo_services/sensors/sensors.mojom.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';

import 'dart:ui' as sky;

//
double worldDx = 0.0;
double worldDy = 0.0;
double worldDz = 0.0;

main() async {
  print("######====================####s##");
  SensorServiceProxy sensor = new SensorServiceProxy.unbound();
  shell.connectToService("h", sensor);

  SensorListenerStub stub = new SensorListenerStub.unbound();
  stub.impl = new MySensorListener();
  sensor.ptr.addListener(SensorType.GRAVITY, stub);
  print("######====================###s###");

  runApp(new DrawRectWidget());
}

class MySensorListener extends SensorListener {
  void onAccuracyChanged(int accuracy) {
    print("accuracy: ${accuracy}");
  }

  void onSensorChanged(SensorData data) {
    print("data: ${data.accuracy} ${data.values}");
    worldDx = data.values[0];
    worldDy = data.values[1];
    worldDz = data.values[2];
  }
}
class DrawRectWidget extends OneChildRenderObjectWidget {
  RenderObject createRenderObject() {
    return new DrawRectObject();
  }
}

class DrawRectObject extends RenderBox {
  double s = 100.0;
  double x = 100.0;
  double y = 100.0;
  double dx = 0.0;
  double dy = 0.0;

  @override
  void performLayout() {
    size = constraints.biggest;
    Scheduler.instance.addFrameCallback(onTick);
  }

  @override
  void paint(PaintingContext context, Offset offset) {
    Paint p = new Paint();
    p.color = new Color.fromARGB(0xff, 0xff, 0xff, 0xff);
    Rect r = new Rect.fromLTWH(x - s/2.0, y - s/2.0, s, s);
    context.canvas.drawOval(r, p);
    p.style = sky.PaintingStyle.stroke;
    Rect w = new Rect.fromLTWH(s/2, s/2, this.paintBounds.width-s, this.paintBounds.height-s);
    context.canvas.drawRect(w, p);
  }

  onTick(Duration timeStamp) {
    Scheduler.instance.addFrameCallback(onTick);
    x += dx;
    y += dy;
    if(y-s < paintBounds.top) {
      y = paintBounds.top+s;
      dy = -0.8*dy;
    }
    if(y+s > paintBounds.bottom) {
      y = paintBounds.bottom-s;
      dy = -0.8*dy;
    }
    if(x-s < paintBounds.left) {
      x = paintBounds.left+s;
      dx = -0.8*dx;
    }
    if(x+s > paintBounds.right) {
      x = paintBounds.right-s;
      dx = -0.8*dx;
    }
    dx += -1*worldDx/3.0;
    dy += worldDy/3.0;
    markNeedsPaint();
  }
}
https://github.com/kyorohiro/hello_skyengine/tree/master/mojo_sensor_demo