首页+记录页面跳转 摇晃待优化

This commit is contained in:
ddshi 2024-12-09 14:40:18 +08:00
parent dc3811367b
commit 2bf0d8e22b
8 changed files with 304 additions and 113 deletions

25
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,25 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "swing_account",
"request": "launch",
"type": "dart"
},
{
"name": "swing_account (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "swing_account (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
]
}

View File

@ -1,4 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:label="swing_account"
android:name="${applicationName}"

View File

@ -1,8 +1,12 @@
buildscript {
ext.kotlin_version = '1.7.10'
repositories {
google()
mavenCentral()
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/public' }
// google()
// mavenCentral()
}
dependencies {
@ -12,8 +16,11 @@ buildscript {
allprojects {
repositories {
google()
mavenCentral()
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/jcenter' }
maven { url 'https://maven.aliyun.com/repository/public' }
// google()
// mavenCentral()
}
}

View File

@ -1,125 +1,23 @@
import 'package:flutter/material.dart';
import './pages/home_page.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
// debug标签
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
title: '记账本',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
home: const HomePage(),
);
}
}

116
lib/pages/home_page.dart Normal file
View File

@ -0,0 +1,116 @@
import 'package:flutter/material.dart';
import 'package:sensors_plus/sensors_plus.dart';
import 'package:vibration/vibration.dart';
import 'dart:async';
import './record_page.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
StreamSubscription? _accelerometerSubscription;
DateTime? _lastShakeTime;
int _shakeCount = 0;
bool _isNavigating = false;
@override
void initState() {
super.initState();
_initShakeDetection();
}
///
void _initShakeDetection() {
_accelerometerSubscription = accelerometerEvents.listen((event) {
// x轴
if (event.x.abs() > 25) {
final now = DateTime.now();
if (_lastShakeTime == null) {
_lastShakeTime = now;
_shakeCount = 1;
} else {
//
if (now.difference(_lastShakeTime!) < const Duration(milliseconds: 500)) {
_shakeCount++;
if (_shakeCount >= 2) {
_navigateToRecordPageWithVibration();
_shakeCount = 0;
_lastShakeTime = null;
}
} else {
//
_shakeCount = 1;
}
_lastShakeTime = now;
}
}
});
}
///
void _navigateToRecordPageWithVibration() async {
if (_isNavigating) return;
_isNavigating = true;
//
if (await Vibration.hasVibrator() ?? false) {
Vibration.vibrate(duration: 200);
}
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const RecordPage(),
maintainState: false,
),
).then((_) {
_isNavigating = false;
});
}
///
void _navigateToRecordPage() {
if (_isNavigating) return;
_isNavigating = true;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const RecordPage(),
maintainState: false,
),
).then((_) {
_isNavigating = false;
});
}
@override
void dispose() {
_accelerometerSubscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('记账本'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: _navigateToRecordPage,
child: const Text('记账'),
),
],
),
),
);
}
}

View File

@ -0,0 +1,32 @@
import 'package:flutter/material.dart';
class RecordPage extends StatefulWidget {
const RecordPage({Key? key}) : super(key: key);
@override
State<RecordPage> createState() => _RecordPageState();
}
class _RecordPageState extends State<RecordPage> {
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
Navigator.of(context).pop();
return false;
},
child: Scaffold(
appBar: AppBar(
title: const Text('记账'),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () => Navigator.of(context).pop(),
),
),
body: const Center(
child: Text('记账页面'),
),
),
);
}
}

View File

@ -49,6 +49,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.8"
device_info_plus:
dependency: transitive
description:
name: device_info_plus
sha256: "77f757b789ff68e4eaf9c56d1752309bd9f7ad557cb105b938a7f8eb89e59110"
url: "https://pub.dev"
source: hosted
version: "9.1.2"
device_info_plus_platform_interface:
dependency: transitive
description:
name: device_info_plus_platform_interface
sha256: "282d3cf731045a2feb66abfe61bbc40870ae50a3ed10a4d3d217556c35c8c2ba"
url: "https://pub.dev"
source: hosted
version: "7.0.1"
fake_async:
dependency: transitive
description:
@ -57,6 +73,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
sha256: "7bf0adc28a23d395f19f3f1eb21dd7cfd1dd9f8e1c50051c069122e6853bc878"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
file:
dependency: transitive
description:
name: file
sha256: a3b4f84adafef897088c160faf7dfffb7696046cb13ae90b508c2cbc95d3b8d4
url: "https://pub.dev"
source: hosted
version: "7.0.1"
flutter:
dependency: "direct main"
description: flutter
@ -75,6 +107,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
lints:
dependency: transitive
description:
@ -83,6 +120,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
logging:
dependency: transitive
description:
name: logging
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
matcher:
dependency: transitive
description:
@ -115,6 +160,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.3"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
sensors_plus:
dependency: "direct main"
description:
name: sensors_plus
sha256: "362c8f4f001838b90dd5206b898bbad941bc0142479eab9a3415f0f79e622908"
url: "https://pub.dev"
source: hosted
version: "1.4.1"
sensors_plus_platform_interface:
dependency: transitive
description:
name: sensors_plus_platform_interface
sha256: bc472d6cfd622acb4f020e726433ee31788b038056691ba433fec80e448a094f
url: "https://pub.dev"
source: hosted
version: "1.2.0"
sensors_plus_web:
dependency: transitive
description:
name: sensors_plus_web
sha256: fca8d7d9ab6233b2a059952666415508e252420be1ef54f092d07884da53ec5e
url: "https://pub.dev"
source: hosted
version: "1.1.2"
sky_engine:
dependency: transitive
description: flutter
@ -176,6 +253,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
vibration:
dependency: "direct main"
description:
name: vibration
sha256: "06588a845a4ebc73ab7ff7da555c2b3dbcd9676164b5856a38bf0b2287f1045d"
url: "https://pub.dev"
source: hosted
version: "1.9.0"
vibration_platform_interface:
dependency: transitive
description:
name: vibration_platform_interface
sha256: f66b39aab2447038978c16f3d6f77228e49ef5717556e3da02313e044e4a7600
url: "https://pub.dev"
source: hosted
version: "0.0.2"
web:
dependency: transitive
description:
@ -184,5 +277,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.3.0"
win32:
dependency: transitive
description:
name: win32
sha256: "464f5674532865248444b4c3daca12bd9bf2d7c47f759ce2617986e7229494a8"
url: "https://pub.dev"
source: hosted
version: "5.2.0"
win32_registry:
dependency: transitive
description:
name: win32_registry
sha256: "41fd8a189940d8696b1b810efb9abcf60827b6cbfab90b0c43e8439e3a39d85a"
url: "https://pub.dev"
source: hosted
version: "1.1.2"
sdks:
dart: ">=3.2.5 <4.0.0"
flutter: ">=3.3.0"

View File

@ -30,6 +30,8 @@ environment:
dependencies:
flutter:
sdk: flutter
sensors_plus: ^1.4.1 # 用于检测手机摇晃
vibration: ^1.8.4 # 添加震动支持
# The following adds the Cupertino Icons font to your application.