Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dart 使AppBar透明并显示设置为整个屏幕的背景图像_Dart_Flutter_Flutter Appbar - Fatal编程技术网

Dart 使AppBar透明并显示设置为整个屏幕的背景图像

Dart 使AppBar透明并显示设置为整个屏幕的背景图像,dart,flutter,flutter-appbar,Dart,Flutter,Flutter Appbar,我在颤振应用程序中添加了AppBar。我的屏幕已经有一个背景图像,我不想设置appBar颜色,也不想为appBar设置单独的背景图像 我想显示相同的屏幕背景图像的appBar也 我已经尝试将appBar颜色设置为透明,但它显示的颜色类似于灰色 示例代码: appBar: new AppBar( centerTitle: true, // backgroundColor: Color(0xFF0077ED), elevation: 0.0,

我在颤振应用程序中添加了AppBar。我的屏幕已经有一个背景图像,我不想设置appBar颜色,也不想为appBar设置单独的背景图像

我想显示相同的屏幕背景图像的appBar也

我已经尝试将appBar颜色设置为透明,但它显示的颜色类似于灰色

示例代码:

appBar: new AppBar(
        centerTitle: true,
//        backgroundColor: Color(0xFF0077ED),
        elevation: 0.0,
        title: new Text(
            "DASHBOARD",
            style: const TextStyle(
                color:  const Color(0xffffffff),
                fontWeight: FontWeight.w500,
                fontFamily: "Roboto",
                fontStyle:  FontStyle.normal,
                fontSize: 19.0
            )),
      )
Size size = MediaQuery.of(context).size;
    return Scaffold(
      extendBodyBehindAppBar: true,
      body: Container(
        // height: size.height * 0.3,
        child: Image.asset(
          'shopping_assets/images/Fruits/pineapple.png',
          fit: BoxFit.cover,
          height: size.height * 0.4,
          width: size.width,
        ),
      ),
    );

您可以使用堆栈小部件来执行此操作。遵循下面的例子

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Home(),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Scaffold(
            backgroundColor: Colors.transparent,
            appBar: new AppBar(
              title: new Text(
                "Hello World",
                style: TextStyle(color: Colors.amber),
              ),
              backgroundColor: Colors.transparent,
              elevation: 0.0,
            ),
            body: new Container(
              color: Colors.red,
            ),
          ),
        ],
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(新的MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
家:新家(),
);
}
}
类Home扩展了StatefulWidget{
@凌驾
_HomeState createState()=>\u HomeState();
}
类(HomeState扩展状态){
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:堆栈(
儿童:[
脚手架(
背景颜色:颜色。透明,
appBar:新的appBar(
标题:新文本(
“你好,世界”,
样式:TextStyle(颜色:Colors.amber),
),
背景颜色:颜色。透明,
标高:0.0,
),
主体:新容器(
颜色:颜色,红色,
),
),
],
),
);
}
}

在我的例子中,我是这样做的:

另外,创建一个带有自定义后退按钮的应用程序栏(在本例中,使用
FloatingActionButton
)。您仍然可以在
堆栈中添加小部件

class Home extends StatefulWidget {
  @override
  _EditProfilePageState createState() => _EditProfilePageState();
}

class _HomeState extends State< Home > {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          this._backgroundImage(), // --> Background Image
          Positioned( // --> App Bar
            child: AppBar(
              backgroundColor: Colors.transparent,
              elevation: 0.0,
              leading: Padding( // --> Custom Back Button
                padding: const EdgeInsets.all(8.0),
                child: FloatingActionButton(
                  backgroundColor: Colors.white,
                  mini: true,
                  onPressed: this._onBackPressed,
                  child: Icon(Icons.arrow_back, color: Colors.black),
                ),
              ),
            ),
          ),
          // ------ Other Widgets ------
        ],
      ),
    );
  }

  Widget _backgroundImage() {
    return Container(
      height: 272.0,
      width: MediaQuery.of(context).size.width,
      child: FadeInImage(
        fit: BoxFit.cover,
        image: NetworkImage(
            'https://images.unsplash.com/photo-1527555197883-98e27ca0c1ea?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80'),
        placeholder: AssetImage('assetName'),
      ),
    );
  }

  void _onBackPressed() {
    Navigator.of(context).pop();
  }
}
class Home扩展StatefulWidget{
@凌驾
_EditProfilePageState createState()=>\u EditProfilePageState();
}
类_HomeState扩展州{
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:堆栈(
儿童:[
这是._backgroundImage(),//-->背景图像
定位(//-->应用程序栏
孩子:AppBar(
背景颜色:颜色。透明,
标高:0.0,
前导:填充(//-->自定义后退按钮
填充:常数边集全部(8.0),
子:浮动操作按钮(
背景颜色:Colors.white,
米尼:是的,
按下:这个,
子:图标(Icons.arrow\u back,颜色:Colors.black),
),
),
),
),
//----其他小部件------
],
),
);
}
Widget_backgroundImage(){
返回容器(
身高:272.0,
宽度:MediaQuery.of(context).size.width,
孩子:法代尼玛(
适合:BoxFit.cover,
图片:NetworkImage(
'https://images.unsplash.com/photo-1527555197883-98e27ca0c1ea?ixlib=rb-1.2.1&ixid=Eyjhchbawqiojeymdd9&w=1000&q=80’,
占位符:AssetImage('assetName'),
),
);
}
void _onBackPressed(){
Navigator.of(context.pop();
}
}

在下面的链接中,您可以找到更多信息

您可以尝试此代码为我工作

@override
  Widget build(BuildContext context) {
    _buildContext = context;
    sw = MediaQuery.of(context).size.width;
    sh = MediaQuery.of(context).size.height;

    return new Container(
      child: new Stack(
        children: <Widget>[
          new Container(
            child: Stack(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(20.0),
                  decoration: BoxDecoration(image: backgroundImage),
                ),
              ],
            ),
          ),
          new Scaffold(
            backgroundColor: Colors.transparent,
            appBar: new AppBar(
              title: new Text(Strings.page_register),
              backgroundColor: Colors.transparent,
              elevation: 0.0,
              centerTitle: true,
            ),
            body: SingleChildScrollView(
              padding: EdgeInsets.all(20.0),
              physics: BouncingScrollPhysics(),
              scrollDirection: Axis.vertical,
              child: new Form(
                key: _formKey,
                autovalidate: _autoValidate,
                child: FormUI(),
              ),
            ),
          )
        ],
      ),
    );
  }

现在Scaffold支持这一点(在stable-v1.12.13+hotfix.5中)


  • 将Scaffold
    extendedbodybehindappbar
    设置为true
  • 将AppBar
    elevation
    设置为0以消除阴影
  • 根据需要设置AppBar
    backgroundColor
    透明度
@覆盖
小部件构建(构建上下文){
返回脚手架(
extendedBodyBehindAppBar:true,
背景颜色:Colors.red,
appBar:appBar(
//背景颜色:颜色。透明,
背景颜色:颜色(0x44000000),
海拔:0,
标题:文本(“标题”),
),
正文:中间(子项:文本(“内容”)),
);
}

您可以使用Scaffold的属性“extendedbodybehindappbartrue” 别忘了用安全带把孩子包起来

  @Override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.title,
          style: TextStyle(color: Colors.black),
        ),
        backgroundColor: Colors.transparent,
        elevation: 0.0,
      ),
      extendBodyBehindAppBar: true,
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/background/home.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: SafeArea(
            child: Center(
          child: Container(
            width: 300,
            height: 300,
            decoration: BoxDecoration(
              color: Colors.green,
            ),
            child: Center(child: Text('Test')),
          ),
        )),
      ),
    );
  }

这就是我所做的,它正在发挥作用


现在Scaffold支持这一点(在stable-v1.12.13+hotfix.5中)

将Scaffold extendedbodybehindappbar设置为true, 将AppBar高程设置为0以消除阴影, 根据需要设置AppBar backgroundColor透明度

致以最诚挚的问候

使用堆栈

  • 设置背景图像
    • 另一个脚手架()
      • 设置背景色透明剂
      • 设置自定义appbar
      • 将列与singleChildScrollView或ListView一起使用

@覆盖小部件生成(构建上下文){
返回脚手架(
主体:堆栈(
儿童:[
backgroundBGContainer(),
脚手架(
背景颜色:颜色。透明,
appBar:appBarWidgetCustomTitle(上下文:上下文,标题参数:“”),
正文:SingleChildScrollView(
子:列(
儿童:[
_spaceWdgt(),
居中(
子:堆栈(
儿童:[
新形象资产(
“资产/图像/用户图标.png”,
宽度:117,
身高:97,
),
],
),
),
Widget backgroundBGContainer(){
返回容器(
装饰:新盒子装饰(
图片:新装饰图片(
图像:新资产评估(“资产/图像/地面\u bg\u image.png”),
适合:BoxFit.cover,
),
颜色:MyColor().groundBackColor),
);
}

这些似乎都不适合我,我的是这样的:

return Scaffold(
  extendBodyBehindAppBar: true,
  appBar: AppBar(
    backgroundColor: Colors.transparent,
    iconTheme: IconThemeData(color: Colors.white),
    elevation: 0.0,
    brightness: Brightness.dark,
  ),
  body: Stack(
    children: <Widget>[
      Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: NetworkImage(
                'https://images.unsplash.com/photo-1517030330234-94c4fb948ebc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1275&q=80'),
            fit: BoxFit.cover,
          ),
        ),
        child: Padding(
          padding: const EdgeInsets.fromLTRB(0, 100, 0, 0),
          child:
          // Column of widgets here...
        ),
       ),
     ],
   ),
 );
返回脚手架(
extendedBodyBehindAppBar:true,
appBar:appBar(
背景颜色
return Scaffold(
  extendBodyBehindAppBar: true,
  appBar: AppBar(
    backgroundColor: Colors.transparent,
    iconTheme: IconThemeData(color: Colors.white),
    elevation: 0.0,
    brightness: Brightness.dark,
  ),
  body: Stack(
    children: <Widget>[
      Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: NetworkImage(
                'https://images.unsplash.com/photo-1517030330234-94c4fb948ebc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1275&q=80'),
            fit: BoxFit.cover,
          ),
        ),
        child: Padding(
          padding: const EdgeInsets.fromLTRB(0, 100, 0, 0),
          child:
          // Column of widgets here...
        ),
       ),
     ],
   ),
 );
Size size = MediaQuery.of(context).size;
    return Scaffold(
      extendBodyBehindAppBar: true,
      body: Container(
        // height: size.height * 0.3,
        child: Image.asset(
          'shopping_assets/images/Fruits/pineapple.png',
          fit: BoxFit.cover,
          height: size.height * 0.4,
          width: size.width,
        ),
      ),
    );
Scaffold(extendBodyBehindAppBar: true);