使用 flutter 不停地振动
Vibration non stop using flutter
我想写一个点击时的功能我想让phone不停地振动。
onPressed: ()
{
Vibration.vibrate(duration: 100000, );
},
我该怎么做?
你可以这样做
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
bool _cancel = false;
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("Vibration Demo"),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
RaisedButton(
onPressed: () {
Timer.periodic(
const Duration(seconds: 1),
(Timer timer) {
if (_cancel) {
timer.cancel();
_cancel = false;
return false;
}
return Vibration.vibrate(duration: 1000);
},
);
},
child: const Text("Start Vibrations"),
),
RaisedButton(
onPressed: () {
_cancel = true;
},
child: const Text("Stop Vibrations"),
)
],
),
),
),
);
}
}
我想写一个点击时的功能我想让phone不停地振动。
onPressed: ()
{
Vibration.vibrate(duration: 100000, );
},
我该怎么做?
你可以这样做
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
bool _cancel = false;
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("Vibration Demo"),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
RaisedButton(
onPressed: () {
Timer.periodic(
const Duration(seconds: 1),
(Timer timer) {
if (_cancel) {
timer.cancel();
_cancel = false;
return false;
}
return Vibration.vibrate(duration: 1000);
},
);
},
child: const Text("Start Vibrations"),
),
RaisedButton(
onPressed: () {
_cancel = true;
},
child: const Text("Stop Vibrations"),
)
],
),
),
),
);
}
}