123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:flutter_audio_recorder/views/record_list.dart';
- import 'package:flutter_audio_recorder/views/recorder.dart';
- import 'package:path_provider/path_provider.dart';
- /// Description: 首页
- /// Time : 04/04/2022 Monday
- /// Author : liuyuqi.gov@msn.cn
- class HomePage extends StatefulWidget {
- const HomePage({Key? key}) : super(key: key);
- @override
- _HomePageState createState() => _HomePageState();
- }
- class _HomePageState extends State<HomePage> {
- late Directory? appDir; // audio目录
- late List<String>? records; // audio列表
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: const Text(
- "录音机App",
- style: TextStyle(color: Colors.white),
- ),
- centerTitle: true,
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: () {
- showRecord(context);
- },
- child: const Icon(Icons.mic),
- ),
- body: Stack(
- children: [
- const Center(child: Text("天问科技")),
- Column(
- children: [
- Expanded(
- flex: 2,
- child: RecordList(
- records: records!,
- ),
- ),
- ],
- ),
- ],
- ),
- );
- }
- /// 回调函数
- _onFinish() {
- records!.clear();
- appDir!.list().listen((onData) {
- records!.add(onData.path);
- }).onDone(() {
- records!.sort();
- records = records!.reversed.toList();
- setState(() {});
- });
- }
- /// 底部弹出录音按钮模态框
- void showRecord(BuildContext context) {
- showModalBottomSheet<void>(
- context: context,
- builder: (BuildContext context) {
- return Container(
- height: 200,
- color: Colors.white70,
- child: RecorderView(
- saveVoice: _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();
- }
- }
|