Scaffold介绍

Scaffold 我们通常俗称为脚手架.在每一个页面中基本都需要用到Scaffold ,除非当你的页面不需要导航区,但仍希望您使用 Scaffold来作为每个页面的顶级组件.

Scaffold属性和说明

序列号字段属性描述
1appBarPreferredSizeWidget显示脚手架的顶部导航区
2bodyWidget显示脚手架的主要内容
3floatingActionButtonWidget悬浮按钮,位于右下角
4floatingActionButtonLocationFloatingActionButtonLocation决定悬浮按钮的位置
5floatingActionButtonAnimatorFloatingActionButtonAnimator决定悬浮按钮的动画
6persistentFooterButtonsList显示在脚手架底部的一组按钮(固定在底部不动的)
7drawerWidget左侧抽屉菜单组件
8onDrawerChangedDrawerCallback左侧抽屉菜单改变事件回调
9endDrawerWidget右侧抽屉菜单组件
10onEndDrawerChangedDrawerCallback右侧抽屉菜单改变事件回调
11bottomNavigationBarWidget底部导航条
12bottomSheet(可以作为评论时候的输入框)Widget持久在body下方,底部控件上方的控件
13backgroundColorColor脚手架背景颜色
14resizeToAvoidBottomInsetbool防止小组件重复
15primarybool脚手架是否延伸到顶部
16drawerDragStartBehaviorDragStartBehavior检测手势行为方式,与drawer配合使用
17extendBodybool是否延伸到底部
18extendBodyBehindAppBarbool是否延伸到顶部,用于做半透明、毛玻璃效果的主要控制属性
19drawerScrimColorColor侧边栏弹出时未遮盖层主页面的颜色
20drawerEdgeDragWidthdouble侧边栏弹出时未遮罩层的宽度
21drawerEnableOpenDragGesturebool左侧抽屉是否支持手势滑动
22endDrawerEnableOpenDragGesturebool右侧抽屉是否支持手势滑动
23restorationIdString状态还原标识符

Scaffold有下面几个主要属性:

1.appBar

显示脚手架的顶部导航栏

Scaffold(
  appBar: AppBar(
    title: Text("Scaffold"),
  ),
);

2.body

显示脚手架的主要内容

Scaffold(
  appBar: AppBar(
  	title: Text("Scaffold"),
  ),
  body: Center(
  	child: Text("body"),
  ),
);

3.floatingActionButton

悬浮按钮,默认位于右小角

Scaffold(
  appBar: AppBar(
  	title: Text("Scaffold"),
  ),
  body: Center(
  	child: Text("body"),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: (){
   		print("add");
    },
    child: Icon(Icons.add)
  ),
);

4.floatingActionButtonLocation

决定悬浮按钮的位置

Scaffold(
  appBar: AppBar(
  	title: Text("Scaffold"),
  ),
  body: Center(
  	child: Text("body"),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: (){
   		print("add");
    },
    child: Icon(Icons.add)
  ),
  floatingActionButtonLocation: FloatingActionButtonLocation.miniCenterDocked,
);

5.floatingActionButtonAnimator

决定悬浮按钮的动画

Scaffold(
  appBar: AppBar(
  	title: Text("Scaffold"),
  ),
  body: Center(
  	child: Text("body"),
  ),
  floatingActionButton: FloatingActionButton(
    onPressed: (){
   		print("add");
    },
    child: Icon(Icons.add)
  ),
  floatingActionButtonLocation: FloatingActionButtonLocation.miniCenterDocked,
  floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
);

6.persistentFooterButtons

显示在脚手架底部的一组按钮

Scaffold(
  appBar: AppBar(
  	title: Text("Scaffold"),
  ),
  body: Center(
  	child: Text("body"),
  ),
	persistentFooterButtons: [
    TextButton(onPressed: (){}, child: Text("Text1")),
    TextButton(onPressed: (){}, child: Text("Text2")),
  ],
);

7.drawer

左侧抽屉菜单组件,如果需要自定义可通过设置 Scaffold 的 key 来操作手动打开侧边栏

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
  	title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
  	child: Text("body"),
  ),
	drawer: Drawer(
    child: Center(child: Text("draw"),),
  )
);

8.onDrawerChanged

左侧抽屉菜单改变事件回调

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
  	title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
  	child: Text("body"),
  ),
	drawer: Drawer(
    child: Center(child: Text("draw"),),
  ),
  onDrawerChanged: (isOpen) {
  	print(isOpen);
  },
);

9.endDrawer

右侧抽屉菜单组件,功能和 drawer 一样,唯一的区别是一个在左侧,一个在右侧

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
  	title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
  	child: Text("body"),
  ),
	endDrawer: Drawer(
  	child: Center(child: Text("draw"),),
  ),
);

10.onEndDrawerChanged

右侧抽屉菜单改变事件回调,使用方式和 onDrawerChanged 一样

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
  	title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
  	child: Text("body"),
  ),
	endDrawer: Drawer(
  	child: Center(child: Text("draw"),),
  ),
  onEndDrawerChanged: (isOpen) {
    print(isOpen);
  },
);

11.bottomNavigationBar

底部导航条,常用于切换底部 item

int _currentIndex = 0;
  List<Widget> _pages = [
    Center(child: Text("tab1"),),
    Center(child: Text("tab2"),),
  ];

Scaffold(
  appBar: AppBar(
    title: Text("Scaffold"),
  ),
  body: _pages[_currentIndex],
  bottomNavigationBar: BottomNavigationBar(
    items: [
      BottomNavigationBarItem(
        label: "tab1",
        icon: Icon(Icons.settings)
      ),
      BottomNavigationBarItem(
        label: "tab2",
        icon: Icon(Icons.settings)
      )
    ],
    currentIndex: _currentIndex,
    onTap: (currentIndex) {
      setState(() {
        _currentIndex = currentIndex;
      });
    },
  ),
);

12.bottomSheet

持久在body下方,底部控件上方的控件

Scaffold(
  appBar: AppBar(
    title: Text("Scaffold"),
  ),
  body: _pages[_currentIndex],
	bottomSheet: Container(child: Row(
    children: [
      Expanded(child: TextField()),
      ElevatedButton(onPressed: (){}, child: Text("发送"))
    ],
  ),)
);

13.backgroundColor

脚手架背景颜色

Scaffold(
  appBar: AppBar(
    title: Text("Scaffold"),
  ),
  body: _pages[_currentIndex],
	backgroundColor: Colors.pink,
);

14.resizeToAvoidBottomInset

防止组件重复,当键盘弹起时会挡住组件,该值设置为 ture 可防止键盘遮挡

Scaffold(
  appBar: AppBar(
    title: Text("Scaffold"),
  ),
  body: _pages[_currentIndex],
	resizeToAvoidBottomInset: true,
);

15.primary

脚手架是否延伸到顶部

Scaffold(
  appBar: AppBar(
    title: Text("Scaffold"),
  ),
  body: _pages[_currentIndex],
	primary: true,
);

16.drawerDragStartBehavior

拖动行为方式,与  drawer 配合使用,用于打开和关闭抽屉的拖动行为将在检测到拖动手势时开始。 如果设置为 DragStartBehavior.down,它将在首次检测到 down 事件时开始。当拖动返回时会使用 DragStartBehavior.down 是有很明显的卡顿,建议使用 DragStartBehavior.start

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
  	title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
  	child: Text("body"),
  ),
	drawer: Drawer(
    child: Center(child: Text("draw"),),
  ),
  drawerDragStartBehavior: DragStartBehavior.start
);

17.extendBody

是否延伸到底部,主要用于做半透明效果

Scaffold(
  appBar: AppBar(
    title: Text("Scaffold"),
  ),
  body: _pages[_currentIndex],
	extendBody: true,
);

18.extendBodyBehindAppBar

是否延伸到顶部,用于做半透明、毛玻璃效果的主要控制属性

Scaffold(
  appBar: AppBar(
    title: Text("Scaffold"),
  ),
  body: _pages[_currentIndex],
	extendBodyBehindAppBar: true,
);

19.drawerScrimColor

侧边栏弹出时非遮盖层主页面的颜色

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
  	title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
  	child: Text("body"),
  ),
	drawer: Drawer(
    child: Center(child: Text("draw"),),
  ),
  drawerScrimColor: Colors.green,
);

20.drawerEdgeDragWidth

侧边栏弹出时非遮罩层的宽度,当滑动的距离小于该值时,遮罩层会弹出。默认值是20

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
    title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
    child: Text("body"),
  ),
  drawer: Drawer(
    child: Center(child: Text("draw"),),
  ),
  drawerScrimColor: Colors.green,
  drawerEdgeDragWidth: 100,
);

21.drawerEnableOpenDragGesture

左侧抽屉是否支持手势滑动,如果设置为 false ,将不能通过侧滑手势滑出,默认true

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
    title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
    child: Text("body"),
  ),
  drawer: Drawer(
    child: Center(child: Text("draw"),),
  ),
  drawerScrimColor: Colors.green,
  drawerEnableOpenDragGesture: false,
);

22.endDrawerEnableOpenDragGesture

右侧抽屉是否支持手势滑动,如果设置为 false ,将不能通过侧滑手势滑出,默认 true

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
    title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
    child: Text("body"),
  ),
  drawer: Drawer(
    child: Center(child: Text("draw"),),
  ),
  drawerScrimColor: Colors.green,
  drawerEnableOpenDragGesture: false,
  endDrawerEnableOpenDragGesture: false,
);

23.restorationId

状态还原标识符

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();

Scaffold(
  key: _scaffoldKey,
  appBar: AppBar(
    title: Text("Scaffold"),
    leading: IconButton(onPressed: (){
      _scaffoldKey.currentState.openDrawer();
    }, icon: Icon(Icons.menu_open)),
  ),
  body: Center(
    child: Text("body"),
  ),
	restorationId: "scaffold"
);
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐