build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Flutter 九宫格示例',
    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: Scaffold(
      appBar: AppBar(
        title: Text('九宫格布局'),
      ),
      body: SafeArea(
        child: GridView.builder(
          // 网格列数,九宫格通常为3列
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            // 主轴(水平)间距
            mainAxisSpacing: 8.0,
            // 交叉轴(垂直)间距
            crossAxisSpacing: 8.0,
            // 子元素宽高比
            childAspectRatio: 1.0,
          ),
          // 九宫格项目数量
          itemCount: 9,
          // 定义每个网格项的样式
          itemBuilder: (BuildContext context, int index) {
            return Container(
              decoration: BoxDecoration(
                // 背景颜色随索引变化,增加视觉区分
                color: Colors.primaries[index % Colors.primaries.length],
                // 设置边框
                border: Border.all(
                  color: Colors.blue,      // 边框颜色
                  width: 2.0,              // 边框宽度
                  style: BorderStyle.solid // 边框样式(默认是 solid)
                ),
                borderRadius: BorderRadius.circular(10.0)
              ),
              // 居中显示索引数字
              child: Center(
                child: Text(
                  'Item $index',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 18.0,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            );
          },
        ),
      ),
      // 底部导航栏(如 Tab 栏)
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: '我的'),
        ],
      ),
      // 侧边栏菜单
      drawer: Drawer(
        child: ListView(
          children: [
            DrawerHeader(child: Text('侧边栏头部')),
            ListTile(title: Text('菜单项1'), onTap: () {}),
            ListTile(title: Text('菜单项2'), onTap: () {}),
          ],
        ),
      ),
      // 悬浮按钮
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {},
      ),
      // 底部弹窗
      bottomSheet: SizedBox(
        //height: 100,
        child: Text('底部弹窗内容'),
      ),
    ),
  );
}