import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_audio_recorder/views/recorder.dart'; import 'package:flutter_audio_recorder/views/records.dart'; import 'package:path_provider/path_provider.dart'; class HomePage extends StatefulWidget { const HomePage({Key? key}) : super(key: key); @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State { late Directory? appDir; late List? records; @override Widget build(BuildContext context) { return Scaffold( floatingActionButton: FloatingActionButton( onPressed: () {}, child: InkWell( child: const Icon(Icons.mic), onTap: () { show(context); }, ), ), appBar: AppBar( title: const Text( "录音机App", style: TextStyle(color: Colors.white), ), centerTitle: true, ), body: Column( children: [ Expanded( flex: 2, child: Records( records: records!, ), ), ], ), ); } _onFinish() { records!.clear(); appDir!.list().listen((onData) { records!.add(onData.path); }).onDone(() { records!.sort(); records = records!.reversed.toList(); setState(() {}); }); } //底部弹出录音按钮模态框 void show(BuildContext context) { showModalBottomSheet( context: context, builder: (BuildContext context) { return Container( height: 200, color: Colors.white70, child: RecorderView( save: _onFinish, ), ); }, ); } @override void initState() { super.initState(); records = []; getExternalStorageDirectory().then((value) { appDir = value!; Directory appDirec = Directory("${appDir!.path}/Audiorecords/"); appDir = appDirec; appDir!.list().listen((onData) { records!.add(onData.path); }).onDone(() { records = records!.reversed.toList(); setState(() {}); }); }); } @override void dispose() { appDir = null; records = null; super.dispose(); } }