IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> flutter 界面的另一种写法先写一部分再用Material在方法体里面放方法体 -> 正文阅读

[移动开发]flutter 界面的另一种写法先写一部分再用Material在方法体里面放方法体

//这里的方法就是:可以先在Widget build写一部分,用Expanded把Material的控件放底下,然后再用Material(child: _buildPlayer()),显示其他界面,可以在_buildPlayer方法体里面放其他方法体,可以用列的方式排序,方法体的写法要学会
方法体如下

  Row _buildMuteButtons() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        if (!isMuted)
          FlatButton.icon(
            onPressed: () => mute(true),
            icon: Icon(
              Icons.headset_off,
              color: Colors.cyan,
            ),
            label: Text('Mute', style: TextStyle(color: Colors.cyan)),
          ),
        if (isMuted)
          FlatButton.icon(
            onPressed: () => mute(false),
            icon: Icon(Icons.headset, color: Colors.cyan),
            label: Text('Unmute', style: TextStyle(color: Colors.cyan)),
          ),
      ],
    );
  }
import 'dart:async';
import 'package:audioplayer/audioplayer.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

//音乐播放器
typedef void OnError(Exception exception);

// const kUrl =
//     "https://www.mediacollege.com/downloads/sound-effects/nature/forest/rainforest-ambient.mp3";
const kUrl = "https://s3.amazonaws.com/scifri-segments/scifri201711241.mp3";

enum PlayerState { stopped, playing, paused }

class AudioPlay extends StatefulWidget {
  const AudioPlay({Key key}) : super(key: key);

  @override
  _AudioPlayState createState() => _AudioPlayState();
}

class _AudioPlayState extends State<AudioPlay> {
  Duration duration;
  Duration position;

  AudioPlayer audioPlayer;

  String localFilePath;

  PlayerState playerState = PlayerState.stopped;

  get isPlaying => playerState == PlayerState.playing;

  get isPaused => playerState == PlayerState.paused;

  get durationText =>
      duration != null ? duration.toString().split('.').first : '';

  get positionText =>
      position != null ? position.toString().split('.').first : '';

  bool isMuted = false;

  StreamSubscription _positionSubscription;
  StreamSubscription _audioPlayerStateSubscription;

  @override
  void initState() {
    super.initState();
    initAudioPlayer();
  }

  @override
  void dispose() {
    _positionSubscription.cancel();
    _audioPlayerStateSubscription.cancel();
    audioPlayer.stop();
    super.dispose();
  }

  void initAudioPlayer() {
    audioPlayer = AudioPlayer();
    _positionSubscription = audioPlayer.onAudioPositionChanged
        .listen((p) => setState(() => position = p));
    _audioPlayerStateSubscription =
        audioPlayer.onPlayerStateChanged.listen((s) {
      if (s == AudioPlayerState.PLAYING) {
        setState(() => duration = audioPlayer.duration);
      } else if (s == AudioPlayerState.STOPPED) {
        onComplete();
        setState(() {
          position = duration;
        });
      }
    }, onError: (msg) {
      setState(() {
        playerState = PlayerState.stopped;
        duration = Duration(seconds: 0);
        position = Duration(seconds: 0);
      });
    });
  }

  //播放
  Future play() async {
    await audioPlayer.play(kUrl);
    setState(() {
      playerState = PlayerState.playing;
    });
  }

  Future _playLocal() async {
    await audioPlayer.play(localFilePath, isLocal: true);
    setState(() => playerState = PlayerState.playing);
  }

  //暂停
  Future pause() async {
    await audioPlayer.pause();
    setState(() => playerState = PlayerState.paused);
  }

  //停止
  Future stop() async {
    await audioPlayer.stop();
    setState(() {
      playerState = PlayerState.stopped;
      position = Duration();
    });
  }

  Future mute(bool muted) async {
    await audioPlayer.mute(muted);
    setState(() {
      isMuted = muted;
    });
  }

  void onComplete() {
    setState(() => playerState = PlayerState.stopped);
  }

  // Future _loadFile() async {
  //   final bytes = await _loadFileBytes(kUrl,
  //       onError: (Exception exception) =>
  //           print('_loadFile => exception $exception'));
  //
  //   final dir = await getApplicationDocumentsDirectory();
  //   final file = File('${dir.path}/audio.mp3');
  //
  //   await file.writeAsBytes(bytes);
  //   if (await file.exists())
  //     setState(() {
  //       localFilePath = file.path;
  //     });
  // }

  //这里的方法就是:可以先写一部分,然后再用Material(child: _buildPlayer()),显示其他界面,
  // 可以在_buildPlayer里面放其他方法体
  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;
    return Center(
      child: Center(
        child: Container(
          decoration: BoxDecoration(color: Color(0xFF1F1F1F)),
          //整个界面以列的形式排放
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              //封面,同时把按键放到底部
              Expanded(
                child: Column(
                  children: [
                    Container(
                      // decoration: BoxDecoration(color: Colors.black),
                      child: Text(
                        '',
                      ),
                    ),
                    Container(
                      margin: const EdgeInsets.only(left: 15, right: 15),
                      child: Image.asset(
                        'assets/base_widgets/bg_player.png',
                        fit: BoxFit.fitWidth,
                      ),
                    ),
                    Container(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            margin: const EdgeInsets.only(
                                left: 30, top: 30, bottom: 15),
                            child: Text("Mojito",
                                style: TextStyle(
                                    color: Color(0xFFCCCCCC), fontSize: 20)),
                          ),
                          Row(
                            children: [
                              Container(
                                margin:
                                    const EdgeInsets.only(left: 30, bottom: 12),
                                child: Text(
                                  "周杰倫",
                                  style: TextStyle(color: Color(0xFF999999)),
                                ),
                              ),
                              Container(
                                width: 30,
                                margin:
                                    const EdgeInsets.only(left: 15, bottom: 12),
                                child: Image.asset(
                                  'assets/base_widgets/icon_player_vip.png',
                                  fit: BoxFit.fitWidth,
                                ),
                              ),
                              Container(
                                width: 30,
                                margin:
                                    const EdgeInsets.only(left: 15, bottom: 12),
                                child: Image.asset(
                                  'assets/base_widgets/icon_player_standard.png',
                                  fit: BoxFit.fitWidth,
                                ),
                              ),
                            ],
                          ),
                          Container(
                            margin: const EdgeInsets.only(left: 30, bottom: 12),
                            child: Text("麻烦给我的爱人来一杯Mojito",
                                style: TextStyle(color: Color(0xFF666666))),
                          ),
                          Container(
                            margin: const EdgeInsets.only(left: 30, bottom: 12),
                            child: Text("我喜欢阅读她微醺时的眼眸",
                                style: TextStyle(color: Color(0xFFFFFFFF))),
                          ),
                          Container(
                            margin: const EdgeInsets.only(left: 30, bottom: 12),
                            child: Text("而我的咖啡糖不用太多",
                                style: TextStyle(color: Color(0xFF666666))),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              //接着来到这里叫开始调用方法了
              Material(child: _buildPlayer()),
              if (!kIsWeb)
                localFilePath != null ? Text(localFilePath) : Container(),
              // if (!kIsWeb)
              //   Padding(
              //     padding: const EdgeInsets.all(8.0),
              // child: Row(
              //   mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              //   children: [
              //     RaisedButton(
              //       // onPressed: () => _loadFile(),
              //       child: Text('Download'),
              //     ),
              //     if (localFilePath != null)
              //       RaisedButton(
              //         onPressed: () => _playLocal(),
              //         child: Text('play local'),
              //       ),
              //   ],
              // ),
              // ),
            ],
          ),
        ),
      ),
    );

    // return MaterialApp(
    //     home: Scaffold(
    //       body: Container(
    //         child: ListView(
    //           children: <Widget>[inputTextArea, loginButtonArea, loginSection],
    //         ),
    //       ),
    //     ));
  }

  Widget _buildPlayer() => Container(
        decoration: BoxDecoration(color: Color(0xFF1F1F1F)),
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            buttonMenuSection(),
            if (duration != null)
              //进度条
              Slider(
                  value: position?.inMilliseconds?.toDouble() ?? 0.0,
                  onChanged: (double value) {
                    return audioPlayer.seek((value / 1000).roundToDouble());
                  },
                  min: 0.0,
                  max: duration.inMilliseconds.toDouble()),
            //如果是空的就显示个空进度条
            if (duration == null)
              Slider(
                value: position?.inMilliseconds?.toDouble() ?? 0.0,
              ),
            //进度条
            _buildProgressView(),
            Row(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  //循环
                  Container(
                    width: 40,
                    margin: const EdgeInsets.only(left: 15, right: 15),
                    child: Image.asset(
                      'assets/base_widgets/icon_player_cycle.png',
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                  //上一首
                  GestureDetector(
                    onTap: isPlaying ? () => pause() : () => play(),
                    child: Container(
                      width: 40,
                      margin: const EdgeInsets.only(left: 15, right: 15),
                      child: Image.asset(
                        'assets/base_widgets/icon_player_previous.png',
                        fit: BoxFit.fitWidth,
                      ),
                    ),
                  ),
                  //播放
                  GestureDetector(
                    onTap: isPlaying ? () => pause() : () => play(),
                    child: Container(
                      width: 50,
                      margin: const EdgeInsets.only(left: 15, right: 15),
                      child: Image.asset(
                        'assets/base_widgets/icon_player_play.png',
                        fit: BoxFit.fitWidth,
                      ),
                    ),
                  ),
                  //下一首
                  GestureDetector(
                    onTap: isPlaying ? () => pause() : () => play(),
                    child: Container(
                      width: 40,
                      margin: const EdgeInsets.only(left: 15, right: 15),
                      child: Image.asset(
                        'assets/base_widgets/icon_player_next.png',
                        fit: BoxFit.fitWidth,
                      ),
                    ),
                  ),
                  //均衡器
                  Container(
                    width: 40,
                    margin: const EdgeInsets.only(left: 15, right: 15),
                    child: Image.asset(
                      'assets/base_widgets/icon_player_tuning.png',
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                  // IconButton(
                  //   onPressed: isPlaying ? null : () => play(),
                  //   iconSize: 64.0,
                  //   icon: Icon(Icons.play_arrow),
                  //   color: Colors.cyan,
                  // ),
                  // IconButton(
                  //   onPressed: isPlaying ? () => pause() : null,
                  //   iconSize: 64.0,
                  //   icon: Icon(Icons.pause),
                  //   color: Colors.cyan,
                  // ),
                  // IconButton(
                  //   onPressed: isPlaying || isPaused ? () => stop() : null,
                  //   iconSize: 64.0,
                  //   icon: Icon(Icons.stop),
                  //   color: Colors.cyan,
                  // ),
                ]),
            // if (position != null)
            //静音
            //   _buildMuteButtons(),
            // if (position != null)
          ],
        ),
      );

  Row _buildProgressView() =>
      Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        Container(
          margin: const EdgeInsets.only(left: 15),
          child: Text(
            position != null
                ? "${positionText ?? ''} "
                : duration != null
                    ? durationText
                    : '',
            style: TextStyle(fontSize: 12.0, color: Colors.white),
          ),
        ),
        Container(
            margin: const EdgeInsets.only(left: 15),
            child: Text(
              position != null
                  ? "${durationText ?? ''}"
                  : duration != null
                      ? durationText
                      : '',
              style: TextStyle(fontSize: 12.0, color: Colors.white),
            )),
        // Padding(
        //   padding: EdgeInsets.all(12.0),
        //圆形进度条
        // child: CircularProgressIndicator(
        //   value: position != null && position.inMilliseconds > 0
        //       ? (position?.inMilliseconds?.toDouble() ?? 0.0) /
        //       (duration?.inMilliseconds?.toDouble() ?? 0.0)
        //       : 0.0,
        //   valueColor: AlwaysStoppedAnimation(Colors.cyan),
        //   backgroundColor: Colors.grey.shade400,
        // ),
        // ),
        // Text(
        //   position != null
        //       ? "${positionText ?? ''} / ${durationText ?? ''}"
        //       : duration != null
        //           ? durationText
        //           : '',
        //   style: TextStyle(fontSize: 24.0, color: Colors.white),
        // )
      ]);

  //是否静音
  Row _buildMuteButtons() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: <Widget>[
        if (!isMuted)
          FlatButton.icon(
            onPressed: () => mute(true),
            icon: Icon(
              Icons.headset_off,
              color: Colors.cyan,
            ),
            label: Text('Mute', style: TextStyle(color: Colors.cyan)),
          ),
        if (isMuted)
          FlatButton.icon(
            onPressed: () => mute(false),
            icon: Icon(Icons.headset, color: Colors.cyan),
            label: Text('Unmute', style: TextStyle(color: Colors.cyan)),
          ),
      ],
    );
  }

  //上面图片下面文字的那一排按键的方法
  Column menuButtonColumn(String icon, String label) {
    //嵌套函数:把共同的属性写好,方便buttonSection调用
    //设置为主题颜色
    Color color = const Color(0xFF999999);
    //返回列的形式
    return Column(
      //调用该方法后返回这个显示,每个元素以列的方式显示
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      //有两个元素
      children: [
        Image.asset(
          icon,
          height: 45.0,
          fit: BoxFit.cover,
        ),
        Container(
          //将文字设置为容器,设置边距
          margin: const EdgeInsets.only(top: 8.0), //文字距离上面图标的距离
          child: Text(
            //元素就是一个文字
            label,
            style: TextStyle(
              //设置文字风格
              fontSize: 12.0,
              fontWeight: FontWeight.w400,
              color: color,
            ),
          ),
        )
      ],
    );
  }

  //底部那一排五个按键
  Row buttonMenuSection() {
    //先从行上开始进行分析
    return Row(
      //平均的分配每个列占据的行空间
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      //行上的四个元素
      //调用方法,不然要写很多
      children: [
        menuButtonColumn("assets/base_widgets/icon_player_cover.png", '翻唱'),
        menuButtonColumn(
            "assets/base_widgets/icon_player_equalizer.png", '均衡器'),
        menuButtonColumn(
            "assets/base_widgets/icon_player_member_download.png", '下載'),
        menuButtonColumn("assets/base_widgets/icon_player_comment.png", '評論'),
        menuButtonColumn("assets/base_widgets/icon_player_more.png", '更多'),
      ],
    );
  }
}

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-04-23 10:58:30  更:2022-04-23 10:59:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 23:21:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码