home_page.dart 2.2 KB

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