如何更改appBar后退按钮的颜色

时间:2018-07-24 22:12:27

标签: flutter

我不知道如何将appBar的自动后退按钮更改为其他颜色。它在脚手架下,我曾尝试对其进行研究,但我无法将其包裹住。

return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Image.asset(
          'images/.jpg',
          fit: BoxFit.fill,
        ),
        centerTitle: true,
      ),

9 个答案:

答案 0 :(得分:46)

您必须使用AppBar的iconTheme属性,如下所示:

  appBar: AppBar(
          iconTheme: IconThemeData(
            color: Colors.black, //change your color here
          ),
          title: Text("Sample"),
          centerTitle: true,
        ),
        body: Text("Sample body"),
      );

答案 1 :(得分:13)

您还可以通过'leading'使用您选择的小部件覆盖默认的后退箭头:

leading: new IconButton(
  icon: new Icon(Icons.arrow_back, color: Colors.orange),
  onPressed: () => Navigator.of(context).pop(),
), 

AppBar小部件所做的全部工作就是提供一个默认的“前导”小部件(如果未设置)。

答案 2 :(得分:5)

创建一个新按钮并为其添加颜色似乎更容易,这是我为任何想知道的人做的

Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
            color: Colors.black
        ),

答案 3 :(得分:5)

您还可以为应用程序全局设置前导图标颜色

MaterialApp(
  theme: ThemeData(
    appBarTheme: AppBarTheme(
      iconTheme: IconThemeData(
        color: Colors.green
      )
    )
  )
)

答案 4 :(得分:3)

AppBar(        
    automaticallyImplyLeading: false,
    leading: Navigator.canPop(context)
        ? IconButton(
            icon: Icon(
              Icons.arrow_back,
              color: Colors.black,
              size: 47,
            ),
            onPressed: () => Navigator.of(context).pop(),
          )
        : null,
);

答案 5 :(得分:1)

您可以自定义 AppBarWidget ,关键字很重要,也可以将自定义的 AppBarWidget 分配给 appBar 脚手架的属性:

import 'package:flutter/material.dart';

double _getAppBarTitleWidth(
    double screenWidth, double leadingWidth, double tailWidth) {
  return (screenWidth - leadingWidth - tailWidth);
}

class AppBarWidget extends StatelessWidget with PreferredSizeWidget {
  AppBarWidget(
      {Key key,
      @required this.leadingChildren,
      @required this.tailChildren,
      @required this.title,
      this.leadingWidth: 110,
      this.tailWidth: 30})
      : super(key: key);

  final List<Widget> leadingChildren;
  final List<Widget> tailChildren;
  final String title;
  final double leadingWidth;
  final double tailWidth;

  @override
  Widget build(BuildContext context) {
    // Get screen size
    double _screenWidth = MediaQuery.of(context).size.width;

    // Get title size
    double _titleWidth =
        _getAppBarTitleWidth(_screenWidth, leadingWidth, tailWidth);

    double _offsetToRight = leadingWidth - tailWidth;

    return AppBar(
      title: Row(
        children: [
          Container(
            width: leadingWidth,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: leadingChildren,
            ),
          ),
          Container(
            color: Colors.green,
            width: _titleWidth,
            padding: const EdgeInsets.only(left: 5.0, right: 5),
            child: Container(
              padding: EdgeInsets.only(right: _offsetToRight),
              color: Colors.deepPurpleAccent,
              child: Center(
                child: Text('$title'),
              ),
            ),
          ),
          Container(
            color: Colors.amber,
            width: tailWidth,
            child: Row(
              children: tailChildren,
            ),
          )
        ],
      ),
      titleSpacing: 0.0,
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}

以下是有关如何使用它的示例:

import 'package:flutter/material.dart';
import 'package:seal_note/ui/Detail/DetailWidget.dart';
import 'package:seal_note/ui/ItemListWidget.dart';

import 'Common/AppBarWidget.dart';
import 'Detail/DetailPage.dart';

class MasterDetailPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _MasterDetailPageState();
}

class _MasterDetailPageState extends State<MasterDetailPage> {
  @override
  Widget build(BuildContext context) { 
    return Scaffold(
      appBar: AppBarWidget(leadingChildren: [
        IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
          ),
        ),
        Text(
          '文件夹',
          style: TextStyle(fontSize: 14.0),
        ),
      ], tailChildren: [
        Icon(Icons.book),
        Icon(Icons.hd),
      ], title: '英语知识',leadingWidth: 140,tailWidth: 50,),
      body: Text('I am body'),
    );
  }
}

答案 6 :(得分:0)

更改CupertinoPageScaffold的主色

Theme(
  data: Theme.of(context).copyWith(
    cupertinoOverrideTheme: CupertinoThemeData(
      scaffoldBackgroundColor: Colors.white70,
      primaryColor: Styles.green21D877, // HERE COLOR OF LEADING
    ),
  ),
  child: CupertinoPageScaffold(
    navigationBar: CupertinoNavigationBar(
      brightness: Brightness.light,
      backgroundColor: Colors.white,
      middle: Text('Cupertino App Bar'),
    ),
    child: Container(
      child: Center(
        child: CupertinoActivityIndicator(),
      ),
    ),
  ),
)

答案 7 :(得分:0)

要自定义前导图标,您可能需要模仿Navigation小部件的功能,该小部件可以根据当前上下文正确处理显示后退按钮,抽屉图标或关闭图标的功能。这是一个替换默认图标的示例。

AppBar

此类使用import 'package:flutter/material.dart'; class TopBar extends StatelessWidget with PreferredSizeWidget { static const double _topBarHeight = 60; @override Widget build(BuildContext context) { return AppBar( toolbarHeight: _topBarHeight, title: Text('Title'), automaticallyImplyLeading: false, leading: _buildLeadingWidget(context), ); } @override Size get preferredSize => Size.fromHeight(_topBarHeight); Widget _buildLeadingWidget(BuildContext context) { final ScaffoldState scaffold = Scaffold.of(context); final ModalRoute<dynamic> parentRoute = ModalRoute.of(context); final bool canPop = ModalRoute.of(context)?.canPop ?? false; final bool hasDrawer = scaffold?.hasDrawer ?? false; final bool useCloseButton = parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog; Widget leading; if (hasDrawer) { leading = IconButton( icon: const Icon(Icons.menu_rounded), onPressed: Scaffold.of(context).openDrawer, tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip, ); } else { if (canPop) { if (useCloseButton) { leading = IconButton( color: Theme.of(context).colorScheme.onBackground, icon: Icon(Icons.close_rounded), onPressed: () => Navigator.of(context).maybePop()); } else { leading = IconButton( padding: EdgeInsets.all(0), iconSize: 38, icon: Icon(Icons.chevron_left_rounded), onPressed: Navigator.of(context).pop); } } } return leading; } } 作为混合,因此您可以用它代替PreferredSizeWidget中现有的AppBar小部件。请注意Scaffold方法,该方法仅在必要时显示后退按钮,如果存在抽屉则显示菜单按钮,如果显示全屏对话框则显示关闭按钮。

答案 8 :(得分:0)

全局设置返回按钮颜色

MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        appBarTheme: AppBarTheme(
          backgroundColor: Colors.white,
          iconTheme: IconThemeData(color: Colors.black), // set backbutton color here which will reflect in all screens. 
        ),
      ),
      home: LoginScreen(),
    );

还有,

更改SliverAppBar

 SliverAppBar(
            iconTheme: IconThemeData(
              color: Colors.white, //change your color here
            ),