【问题标题】:Vibration non stop using flutter使用颤振不停地振动
【发布时间】:2020-11-11 07:39:11
【问题描述】:

我想写一个点击时的功能我想让手机不停地振动。

onPressed: () 
{
  Vibration.vibrate(duration: 100000,  );
},

我该怎么做?

【问题讨论】:

    标签: android flutter dart vibration


    【解决方案1】:

    你可以这样做

    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"),
                  )
                ],
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 参数类型'Timer'不能分配给参数类型'int'。
    • 我不能用这个
    • 参数类型'Timer'不能赋值给参数类型'int'
    • 它根本不起作用。现在它什么都不做
    • 你用的怎么样,能更新一下你的代码吗?
    猜你喜欢
    • 2021-01-26
    • 2021-10-31
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2018-11-17
    相关资源
    最近更新 更多