グラデーション背景のコンテナでスキャフォールドをラップする、フラッターでコンテナ背景にグラデーションを設定するにはどうすればいいですか? 質問する

グラデーション背景のコンテナでスキャフォールドをラップする、フラッターでコンテナ背景にグラデーションを設定するにはどうすればいいですか? 質問する

Scaffoldを でラップしてContainer、 の下にあるグラデーション背景を取得したいと思いますAppBar。基本的にはフルスクリーンの背景です。ただし、私の試みは何も起こりません。機能を維持しながら、画面全体に広がるの上に配置するgradient別の方法はありますか?AppBargradient

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test',
      theme: ThemeData(
        primarySwatch: Colors.yellow,
      ),
      home: MyHomePage(title: 'Test'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topRight,
          end: Alignment.bottomLeft,
          stops: [0.1, 0.5, 0.7, 0.9],
          colors: [
            Colors.yellow[800],
            Colors.yellow[700],
            Colors.yellow[600],
            Colors.yellow[400],
          ],
        ),
      ),
      child: Scaffold(
        appBar: AppBar(
          title: Icon(Icons.menu),
          backgroundColor: Color(0x00000000),
          elevation: 0.0,
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'dummy text',
              ),
              Text(
                '5',
                style: Theme.of(context).textTheme.display1,
              ),
              FloatingActionButton(
                backgroundColor: Colors.white,
                foregroundColor: Colors.black45,
                elevation: 0.0,
                onPressed: () {},
                tooltip: 'Play',
                child: Icon(Icons.play_arrow),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

ベストアンサー1

このようにグラデーションを追加することもできますAppBar

素晴らしいアプリバーのプレビュー

new Scaffold(
      appBar: AppBar(
        title: Center(child: Text('Awesome AppBar')),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
                colors: [
                  const Color(0xFF3366FF),
                  const Color(0xFF00CCFF),
                ],
                begin: const FractionalOffset(0.0, 0.0),
                end: const FractionalOffset(1.0, 0.0),
                stops: [0.0, 1.0],
                tileMode: TileMode.clamp),
          ),
        ),
        child: ...,
      ),
      body: ...,
    );

LinearGradient パラメータ:

  • colors: グラデーションで使用する色の配列。この場合は、2 種類の青の色合いです。

  • begin, end: 最初の色と最後の色の位置。この場合は、

  • FractionalOffsetx と y の両方について、座標を 0 から 1 の範囲として扱うことができます。水平方向のグラデーションが必要なので、両方の測定値に同じ Y を使用し、x は 0.0 (左) から 1.0 (右) に変化します。

  • stops: この配列は色と同じサイズである必要があります。色の分布方法を定義します。[0.0, 1.0] は左から右に塗りつぶします。[0.0, 0.5] は左から半分のバーに色を塗りつぶします。

  • tileMode: ストップが領域全体を塗りつぶさない場合はどうするか。この場合、クランプを追加しました (最後に使用した色を再利用します)。ただし、グラデーションは 0.0 から 1.0 に変化するため、実際には必要ありません。

  • に子を追加することもできますContainer。例: ロゴ画像

        child: Align(
              alignment: Alignment.center,
              child: Image.asset(
                "images/logo.png",
                width: 128,
                height: 128,
              )),
        ),
    

お役に立てれば。

おすすめ記事