Flutter_AppBar

AppBar的使用:

import 'package:flutter/material.dart';

class BasicAppBarSample extends StatefulWidget {
  @override
  _BasicAppBarSampleState createState() => new _BasicAppBarSampleState();
}

class _BasicAppBarSampleState extends State<BasicAppBarSample> {
  Choice _selectedChoice = choices[0]; // The app's "state".

  //选中这个Choice进行展示
  void _select(Choice choice) {
    setState(() {
      // Causes the app to rebuild with the new _selectedChoice.
      _selectedChoice = choice;
    });
  }

  @override
  Widget build(BuildContext context) {
    //返回的还是MaterialApp
    return new MaterialApp(
      //Scafflold意思是架子的意思
      home: new Scaffold(
        appBar: new AppBar(
          //app中的标题
          title: const Text('Basic AppBar'),
          //可以响应的动作
          actions: <Widget>[
            //图标按钮
            new IconButton(
              icon: new Icon(choices[0].icon),
              onPressed: () {
                _select(choices[0]);
              },
            ),
            new IconButton(
              // action button
              icon: new Icon(choices[1].icon),
              onPressed: () {
                _select(choices[1]);
              },
            ),
            //其他的放入一个弹出吧按钮来进行
            new PopupMenuButton<Choice>(
              // overflow menu
              //点击所执行的方法
              onSelected: _select,

              //创建Item的方法
              itemBuilder: (BuildContext context) {
                //跳过两个
                return choices.skip(2).map((Choice choice) {
                  ///将每个都映射为弹出菜单的一个按钮
                  return new PopupMenuItem<Choice>(
                    //对应的值是choice
                    value: choice,
                    child: new Text(choice.title),
                  );
                }).toList();
              },
            ),
          ],
        ),
        body: new Padding(
          ///内边距
          padding: const EdgeInsets.all(16.0),

          child: new ChoiceCard(choice: _selectedChoice),
        ),
      ),
    );
  }
}

///包含对应icon和文本的类
class Choice {
  const Choice({this.title, this.icon});

  final String title;
  final IconData icon;
}

///Choice的集合,是一个常量
const List<Choice> choices = const <Choice>[
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
];


///显示的图片
class ChoiceCard extends StatelessWidget {
  ///参数是可选的
  const ChoiceCard({Key key, this.choice}) : super(key: key);

  final Choice choice;

  @override
  Widget build(BuildContext context) {
    final TextStyle textStyle = Theme.of(context).textTheme.display1;
    return new Card(
      ///白色的
      color: Colors.white,
      ///
      child: new Center(
        child: new Column(
          ///主轴尺寸:
          mainAxisSize: MainAxisSize.min,
          ///横轴对齐方式:中心对齐
          crossAxisAlignment: CrossAxisAlignment.center,
          ///
          children: <Widget>[
            new Icon(choice.icon, size: 128.0, color: textStyle.color),
            new Text(choice.title, style: textStyle),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(new BasicAppBarSample());
}

此条目发表在flutter分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注