home_page.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_audio_recorder/views/record_list.dart';
  4. import 'package:flutter_audio_recorder/views/recorder.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. /// Description: 首页
  7. /// Time : 04/04/2022 Monday
  8. /// Author : liuyuqi.gov@msn.cn
  9. class HomePage extends StatefulWidget {
  10. const HomePage({Key? key}) : super(key: key);
  11. @override
  12. _HomePageState createState() => _HomePageState();
  13. }
  14. class _HomePageState extends State<HomePage> {
  15. late Directory? appDir; // audio目录
  16. late List<String>? records; // audio列表
  17. @override
  18. Widget build(BuildContext context) {
  19. return Scaffold(
  20. appBar: AppBar(
  21. title: const Text(
  22. "录音机App",
  23. style: TextStyle(color: Colors.white),
  24. ),
  25. centerTitle: true,
  26. ),
  27. floatingActionButton: FloatingActionButton(
  28. onPressed: () {
  29. showRecord(context);
  30. },
  31. child: const Icon(Icons.mic),
  32. ),
  33. body: Stack(
  34. children: [
  35. Center(child: Text("天问科技")),
  36. Column(
  37. children: [
  38. Expanded(
  39. flex: 2,
  40. child: RecordList(
  41. records: records!,
  42. ),
  43. ),
  44. ],
  45. ),
  46. ],
  47. ),
  48. );
  49. }
  50. /// 回调函数
  51. _onFinish() {
  52. records!.clear();
  53. appDir!.list().listen((onData) {
  54. records!.add(onData.path);
  55. }).onDone(() {
  56. records!.sort();
  57. records = records!.reversed.toList();
  58. setState(() {});
  59. });
  60. }
  61. /// 底部弹出录音按钮模态框
  62. void showRecord(BuildContext context) {
  63. showModalBottomSheet<void>(
  64. context: context,
  65. builder: (BuildContext context) {
  66. return Container(
  67. height: 200,
  68. color: Colors.white70,
  69. child: RecorderView(
  70. saveVoice: _onFinish,
  71. ),
  72. );
  73. },
  74. );
  75. }
  76. @override
  77. void initState() {
  78. super.initState();
  79. records = [];
  80. getExternalStorageDirectory().then((value) {
  81. appDir = value!;
  82. Directory appDirec = Directory("${appDir!.path}/Audiorecords/");
  83. appDir = appDirec;
  84. appDir!.list().listen((onData) {
  85. records!.add(onData.path);
  86. }).onDone(() {
  87. records = records!.reversed.toList();
  88. setState(() {});
  89. });
  90. });
  91. }
  92. @override
  93. void dispose() {
  94. appDir = null;
  95. records = null;
  96. super.dispose();
  97. }
  98. }