flutter_joystick 0.0.4 flutter_joystick: ^0.0.4 copied to clipboard
A virtual joystick widget for Flutter applications. Highly flexible and customizable.
import 'package:flutter/material.dart';
import 'package:flutter_joystick/flutter_joystick.dart';
void main() {
runApp(const JoystickExampleApp());
}
const ballSize = 20.0;
const step = 10.0;
class JoystickExampleApp extends StatelessWidget {
const JoystickExampleApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Joystick Example'),
),
body: const MainPage(),
),
);
}
}
class MainPage extends StatelessWidget {
const MainPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const JoystickExample()),
);
},
child: const Text('Joystick'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const JoystickAreaExample()),
);
},
child: const Text('Joystick Area'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SquareJoystickExample()),
);
},
child: const Text('Square Joystick'),
),
],
),
);
}
}
class JoystickExample extends StatefulWidget {
const JoystickExample({Key? key}) : super(key: key);
@override
State<JoystickExample> createState() => _JoystickExampleState();
}
class _JoystickExampleState extends State<JoystickExample> {
double _x = 100;
double _y = 100;
JoystickMode _joystickMode = JoystickMode.all;
@override
void didChangeDependencies() {
_x = MediaQuery.of(context).size.width / 2 - ballSize / 2;
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
appBar: AppBar(
title: const Text('Joystick'),
actions: [
JoystickModeDropdown(
mode: _joystickMode,
onChanged: (JoystickMode value) {
setState(() {
_joystickMode = value;
});
},
),
],
),
body: SafeArea(
child: Stack(
children: [
Container(
color: Colors.green,
),
Ball(_x, _y),
Align(
alignment: const Alignment(0, 0.8),
child: Joystick(
mode: _joystickMode,
listener: (details) {
setState(() {
_x = _x + step * details.x;
_y = _y + step * details.y;
});
},
),
),
],
),
),
);
}
}
class JoystickAreaExample extends StatefulWidget {
const JoystickAreaExample({Key? key}) : super(key: key);
@override
State<JoystickAreaExample> createState() => _JoystickAreaExampleState();
}
class _JoystickAreaExampleState extends State<JoystickAreaExample> {
double _x = 100;
double _y = 100;
JoystickMode _joystickMode = JoystickMode.all;
@override
void didChangeDependencies() {
_x = MediaQuery.of(context).size.width / 2 - ballSize / 2;
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
appBar: AppBar(
title: const Text('Joystick Area'),
actions: [
JoystickModeDropdown(
mode: _joystickMode,
onChanged: (JoystickMode value) {
setState(() {
_joystickMode = value;
});
},
),
],
),
body: SafeArea(
child: JoystickArea(
mode: _joystickMode,
initialJoystickAlignment: const Alignment(0, 0.8),
listener: (details) {
setState(() {
_x = _x + step * details.x;
_y = _y + step * details.y;
});
},
child: Stack(
children: [
Container(
color: Colors.green,
),
Ball(_x, _y),
],
),
),
),
);
}
}
class SquareJoystickExample extends StatefulWidget {
const SquareJoystickExample({Key? key}) : super(key: key);
@override
State<SquareJoystickExample> createState() => _SquareJoystickExampleState();
}
class _SquareJoystickExampleState extends State<SquareJoystickExample> {
double _x = 100;
double _y = 100;
JoystickMode _joystickMode = JoystickMode.all;
@override
void didChangeDependencies() {
_x = MediaQuery.of(context).size.width / 2 - ballSize / 2;
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
appBar: AppBar(
title: const Text('Square Joystick'),
actions: [
JoystickModeDropdown(
mode: _joystickMode,
onChanged: (JoystickMode value) {
setState(() {
_joystickMode = value;
});
},
),
],
),
body: SafeArea(
child: Stack(
children: [
Container(
color: Colors.green,
),
Ball(_x, _y),
Align(
alignment: const Alignment(0, 0.8),
child: Joystick(
mode: _joystickMode,
base: JoystickSquareBase(mode: _joystickMode),
stickOffsetCalculator: const RectangleStickOffsetCalculator(),
listener: (details) {
setState(() {
_x = _x + step * details.x;
_y = _y + step * details.y;
});
},
),
),
],
),
),
);
}
}
class JoystickModeDropdown extends StatelessWidget {
final JoystickMode mode;
final ValueChanged<JoystickMode> onChanged;
const JoystickModeDropdown(
{Key? key, required this.mode, required this.onChanged})
: super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
width: 150,
child: Padding(
padding: const EdgeInsets.only(left: 16.0),
child: FittedBox(
child: DropdownButton(
value: mode,
onChanged: (v) {
onChanged(v as JoystickMode);
},
items: const [
DropdownMenuItem(
value: JoystickMode.all, child: Text('All Directions')),
DropdownMenuItem(
value: JoystickMode.horizontalAndVertical,
child: Text('Vertical And Horizontal')),
DropdownMenuItem(
value: JoystickMode.horizontal, child: Text('Horizontal')),
DropdownMenuItem(
value: JoystickMode.vertical, child: Text('Vertical')),
],
),
),
),
);
}
}
class Ball extends StatelessWidget {
final double x;
final double y;
const Ball(this.x, this.y, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Positioned(
left: x,
top: y,
child: Container(
width: ballSize,
height: ballSize,
decoration: const BoxDecoration(
shape: BoxShape.circle,
color: Colors.redAccent,
boxShadow: [
BoxShadow(
color: Colors.black12,
spreadRadius: 2,
blurRadius: 3,
offset: Offset(0, 3),
)
],
),
),
);
}
}