簡體   English   中英

Flutter中如何使漸變背景色全屏並在appbar和body之間混合

[英]How to make a gradient background color full screen and blend between the appbar and body in Flutter

如何使漸變背景顏色全屏並在appbar和body之間混合,因此colors看起來合並並且在appbar和body之間看起來不斷開連接。 這是視圖:

在此處輸入圖像描述

這是我的代碼:

    return Scaffold(
    appBar: AppBar(
      title: Text("Title"),
      elevation: 0,
      flexibleSpace: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.centerRight,
            colors: [
              Color.fromRGBO(205, 193, 255, 1.0),
              Color.fromARGB(255, 252, 99, 99),
              Color.fromARGB(255, 173, 41, 1),
            ],
          ),
        ),
      ),
    ),
    body: Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.centerRight,
          colors: [
            Color.fromRGBO(205, 193, 255, 1.0),
            Color.fromARGB(255, 252, 99, 99),
            Color.fromARGB(255, 173, 41, 1),
          ],
        ),
      ),
      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
      child: Center(
        child: Column(
          children: [
            Text("bla bla bla"),
            Text("bla bla bla"),
          ],
        ),
      ),
    ));

解決方案一:appbar背景和scaffold背景設置為透明並將extendBodyBehindAppBar設置為true並手動設置頂部填充:

Scaffold(
      backgroundColor: Colors.transparent,
      appBar: AppBar(
        leading: Icon(Icons.arrow_back),
        backgroundColor: Colors.transparent,
        elevation: 0,
        title: Text('test'),
      ),
      extendBodyBehindAppBar: true,
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.centerRight,
            colors: [
              Color.fromRGBO(205, 193, 255, 1.0),
              Color.fromARGB(255, 252, 99, 99),
              Color.fromARGB(255, 173, 41, 1),
            ],
          ),
        ),
        padding: EdgeInsets.fromLTRB(
        10,
        AppBar().preferredSize.height +
            MediaQuery.of(context).viewPadding.top,
        10,
        0),
        child: Center(
          child: Column(
            children: [
              Text("bla bla bla"),
              Text("bla bla bla"),
            ],
          ),
        ),
      ),
    )

解決方案二:使用DecoratedBox並將appbarscaffold背景設置為透明:

DecoratedBox(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.centerRight,
          colors: [
            Color.fromRGBO(205, 193, 255, 1.0),
            Color.fromARGB(255, 252, 99, 99),
            Color.fromARGB(255, 173, 41, 1),
          ],
        ),
      ),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          leading: Icon(Icons.arrow_back),
          backgroundColor: Colors.transparent,
          elevation: 0,
          title: Text('test'),
        ),
        body: Container(
          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
          child: Center(
            child: Column(
              children: [
                Text("bla bla bla"),
                Text("bla bla bla"),
              ],
            ),
          ),
        ),
      ),
    )

在此處輸入圖像描述

AppBar中添加backgroundColor: Colors.transparent,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM