Flutter中如何修改AppBar内BackButton的默认图标?
因为BackButton组件的图标是直接读取当前Theme中的Icons.arrow_back,没法直接通过组件属性修改,所以可以用以下两种方式实现自定义图标:
方法一:用IconButton替代BackButton(推荐单页面修改)
直接把BackButton换成IconButton,自定义icon属性即可,同时保留你的返回逻辑:
appBar: AppBar( leading: IconButton( // 这里替换成你想要的图标,比如箭头向左的iOS风格图标 icon: Icon(Icons.arrow_back_ios, color: Colors.indigo), onPressed: () { Get.off(exercisePage()); }, ), shape: RoundedRectangleBorder( borderRadius: BorderRadius.only( bottomLeft: Radius.circular(30), bottomRight: Radius.circular(30)), ), automaticallyImplyLeading: false, title: Padding( padding: const EdgeInsets.only(left: 25), child: Text( 'Regular exercise', style: TextStyle( fontFamily: 'FontMain', fontSize: 22, color: Colors.indigo), ), ), backgroundColor: Color.fromARGB(255, 231, 235, 237), ),
你可以把Icons.arrow_back_ios换成任意你需要的图标,比如自定义SVG图标(用SvgPicture包裹),或者其他Material Icons。
方法二:通过Theme修改全局返回按钮图标
如果想要整个App的返回按钮都用自定义图标,可以在MaterialApp的theme中设置:
MaterialApp( theme: ThemeData( iconTheme: IconThemeData(color: Colors.indigo), // 修改全局返回按钮图标 appBarTheme: AppBarTheme( iconTheme: IconThemeData(color: Colors.indigo), actionsIconTheme: IconThemeData(color: Colors.indigo), // 设置返回按钮图标 leadingIcon: Icon(Icons.arrow_back_ios), ), ), home: YourHomePage(), );
这种方式会影响所有使用默认BackButton的页面,适合全局统一图标的场景。
内容的提问来源于stack exchange,提问作者Vinesh Thakkar
相关产品推荐
相关产品推荐

