flutter_gamepad 0.0.4 copy "flutter_gamepad: ^0.0.4" to clipboard
flutter_gamepad: ^0.0.4 copied to clipboard

outdated

An iOS gamepad library for Flutter. (Android support coming soon.)

example/lib/main.dart

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

import 'package:flutter_gamepad/flutter_gamepad.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<GamepadEvent> _eventLog = [];
  StreamSubscription<GamepadEvent> _subscription;

  @override
  void initState() {
    super.initState();
    _subscription = FlutterGamepad.eventStream.listen(onGamepadEvent);
  }

  void dispose() {
    _subscription.cancel();
    super.dispose();
  }

  String _describeEvent(GamepadEvent e) {
    if (e is GamepadConnectedEvent) {
      return 'Gamepad connected: ${e.gamepadInfo.vendorName} = ${e.gamepadId}';
    } else if (e is GamepadDisconnectedEvent) {
      return 'Gamepad disconnected: ${e.gamepadInfo.vendorName} = ${e.gamepadId}';
    } else if (e is GamepadButtonEvent) {
      return '[${e.gamepadId}] ${e.button} -> ${e.value.toStringAsFixed(3)}';
    } else if (e is GamepadThumbstickEvent) {
      return '[${e.gamepadId}] ${e.thumbstick} -> ${e.x.toStringAsFixed(3)}, ${e.y.toStringAsFixed(3)}';
    } else {
      return 'Unknown event: $e';
    }
  }

  String _eventLogBin(GamepadEvent e) {
    return e.runtimeType.toString() +
        (e is GamepadButtonEvent ? e.button.toString() : '') +
        (e is GamepadThumbstickEvent ? e.thumbstick.toString() : '');
  }

  void onGamepadEvent(GamepadEvent e) {
    setState(() {
      if (_eventLog.isNotEmpty && _eventLogBin(e) == _eventLogBin(_eventLog.last)) {
        _eventLog.removeLast();
      }
      _eventLog.add(e);
      if (_eventLog.length >= 20) _eventLog.removeAt(0);
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: ${Theme.of(context).platform}\n' +
              _eventLog.map(_describeEvent).toList().join('\n')),
        ),
      ),
    );
  }
}
12
likes
0
pub points
38%
popularity

Publisher

verified publisherrainway.com

An iOS gamepad library for Flutter. (Android support coming soon.)

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_gamepad