颤振背景图像

Posted

技术标签:

【中文标题】颤振背景图像【英文标题】:Flutter Background Image 【发布时间】:2020-10-01 13:00:00 【问题描述】:

我对 Flutter 非常陌生,我正在制作一个有趣的应用程序。我想向我的应用程序添加背景图像,但我找不到方法。我知道我必须把它放在BuildContext context,但我找不到怎么做。这是我的代码:

import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';

typedef void OnError(Exception exception);

void main() 
  runApp(new MaterialApp(debugShowCheckedModeBanner: false,home:  LocalAudio()));


class LocalAudio extends StatefulWidget 
  @override
  _LocalAudio createState() =>  _LocalAudio();



class _LocalAudio extends State<LocalAudio> 
  Duration _duration = new Duration();
  Duration _position = new Duration();
  AudioPlayer advancedPlayer;
  AudioCache audioCache;

  @override
  void initState() 
    super.initState();
    initPlayer();
 

  void initPlayer() 
    advancedPlayer = new AudioPlayer();
    audioCache = new AudioCache(fixedPlayer: advancedPlayer);

    advancedPlayer.durationHandler = (d) => setState(() 
      _duration = d;
    );

    advancedPlayer.positionHandler = (p) => setState(() 
      _position = p;
    );
  

  String localFilePath;

  Widget _tab(List<Widget> children) 
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Container(
          padding: EdgeInsets.all(16.0),
          child: Column(
            children: children
                .map((w) => Container(child: w, padding: EdgeInsets.all(6.0)))
                .toList(),
          ),
        ),
      ],
    );
  

  Widget _btn(String txt, VoidCallback onPressed) 
    return ButtonTheme(
      minWidth: 48.0,
      child: Container(
        width: 150,
        height: 150,
        child: RaisedButton(
            shape:
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
            child: Text(txt),
            color: Colors.greenAccent[900],
            textColor: Colors.white,
            onPressed: onPressed),
      ),
    );
  



  Widget LocalAudio() 
    return _tab([
      _btn('Play', () => audioCache.play('bruh.mp3')),
    ]);
  

  void seekToSecond(int second) 
    Duration newDuration = Duration(seconds: second);

    advancedPlayer.seek(newDuration);
  

  @override
  Widget build(BuildContext context) 
    return DefaultTabController(
      length: 1,
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          elevation: 1.0,
          backgroundColor: Colors.teal,
          title: Center(child: Text('BRUH')),
        ),
        body: TabBarView(
          children: [LocalAudio()],
        ),
      ),
    );
  

【问题讨论】:

这是***.com/questions/44179889/…的副本 【参考方案1】:

我只是将脚手架的主体设置为 Stack(),然后将图像放在堆栈的底部。应该看起来像这样。

@override
  Widget build(BuildContext context) 
    return DefaultTabController(
      length: 1,
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          elevation: 1.0,
          backgroundColor: Colors.teal,
          title: Center(child: Text('BRUH')),
        ),
        body: Stack(
          children: [
            Image.asset('assets/images/background.jpg'),
            TabBarView(
              children: [LocalAudio()],
            ),
          ]
        );

      ),

【讨论】:

【参考方案2】:

您可以使用 Stack 并将图像放在背景中,然后在 Scaffold 小部件中设置 backgroundColor: Colors.transparent,

    Stack(
      children: <Widget>[
        Image.asset(
          "<Image.Path>",
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          fit: BoxFit.cover,
        ),
        Scaffold(
          backgroundColor: Colors.transparent,
          body: Container(),
        ),
      ],
    );

【讨论】:

以上是关于颤振背景图像的主要内容,如果未能解决你的问题,请参考以下文章

颤振抽屉背景图片

我想更改颤振应用的背景颜色?

背景图像未与 s-s-rS 中的气泡图对齐

背景图全屏显示

常见的几种抠图方式?

使用颤振进行实时图像处理[关闭]