面试题答案
一键面试在Flutter中,使用Material Design风格构建AppBar并添加自定义操作按钮,可通过AppBar
的actions
属性来实现。以下是示例代码:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('自定义AppBar操作按钮'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {
// 搜索按钮点击逻辑
},
),
IconButton(
icon: Icon(Icons.share),
onPressed: () {
// 分享按钮点击逻辑
},
),
],
),
body: Center(
child: Text('页面内容'),
),
),
);
}
}
在上述代码中,AppBar
的actions
属性接受一个Widget
列表,每个Widget
即为一个操作按钮。这里使用IconButton
作为示例,分别创建了搜索按钮和分享按钮,并可在onPressed
回调中添加相应的点击逻辑。