要将AppBar固定在屏幕上方而不包含在网格中,可以使用Stack布局。以下是一个示例代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'AppBar Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: SafeArea(
child: Stack(
children: [
Positioned(
top: 0,
left: 0,
right: 0,
child: AppBar(
title: Text('AppBar Demo'),
),
),
GridView.count(
crossAxisCount: 2,
children: List.generate(6, (index) {
return Container(
color: Colors.blue,
margin: EdgeInsets.all(10),
);
}),
),
],
),
),
),
);
}
}
在这个示例中,我们使用了Stack布局来将AppBar和网格视图叠加在一起。通过设置AppBar的top属性为0,left属性为0,right属性为0,我们将其固定在屏幕的顶部。然后,我们将GridView放在AppBar下面,作为Stack的第二个子组件。