home_page.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. floatingActionButton: FloatingActionButton(
  21. onPressed: () {},
  22. child: InkWell(
  23. child: const Icon(Icons.mic),
  24. onTap: () {
  25. showRecord(context);
  26. },
  27. ),
  28. ),
  29. appBar: AppBar(
  30. title: const Text(
  31. "录音机App",
  32. style: TextStyle(color: Colors.white),
  33. ),
  34. centerTitle: true,
  35. ),
  36. body: Column(
  37. children: [
  38. Expanded(
  39. flex: 2,
  40. child: RecordList(
  41. records: records!,
  42. ),
  43. ),
  44. ],
  45. ),
  46. );
  47. }
  48. _onFinish() {
  49. records!.clear();
  50. appDir!.list().listen((onData) {
  51. records!.add(onData.path);
  52. }).onDone(() {
  53. records!.sort();
  54. records = records!.reversed.toList();
  55. setState(() {});
  56. });
  57. }
  58. //底部弹出录音按钮模态框
  59. void showRecord(BuildContext context) {
  60. showModalBottomSheet<void>(
  61. context: context,
  62. builder: (BuildContext context) {
  63. return Container(
  64. height: 200,
  65. color: Colors.white70,
  66. child: RecorderView(
  67. save: _onFinish,
  68. ),
  69. );
  70. },
  71. );
  72. }
  73. @override
  74. void initState() {
  75. super.initState();
  76. records = [];
  77. getExternalStorageDirectory().then((value) {
  78. appDir = value!;
  79. Directory appDirec = Directory("${appDir!.path}/Audiorecords/");
  80. appDir = appDirec;
  81. appDir!.list().listen((onData) {
  82. records!.add(onData.path);
  83. }).onDone(() {
  84. records = records!.reversed.toList();
  85. setState(() {});
  86. });
  87. });
  88. }
  89. @override
  90. void dispose() {
  91. appDir = null;
  92. records = null;
  93. super.dispose();
  94. }
  95. }