【问题标题】:Toolbar options in Action bar? (Flutter)操作栏中的工具栏选项? (扑)
【发布时间】:2020-09-25 03:04:24
【问题描述】:

有没有办法在操作栏中显示工具栏选项?这样:

我正在寻找一种在选择文本时添加共享选项的方法。

【问题讨论】:

    标签: flutter mobile


    【解决方案1】:

    AppBar widget 有一个可以使用的actions property

    当您检测到带有 IconButtons 数组的文本选择时,只需填充它即可。

    查看此 DartPad 要点:https://dartpad.dev/70fde86853b9ef9d957507f2f4aef0b3

    【讨论】:

      【解决方案2】:

      您可以使用AppBar 中的actions-Property 在工具栏中添加一些选项。 这是我从 Flutter 文档中获得的示例:

      home: Scaffold(
              appBar: AppBar(
                title: const Text('Basic AppBar'),
                actions: <Widget>[
                  // action button
                  IconButton(
                    icon: Icon(choices[0].icon),
                    onPressed: () {
                      _select(choices[0]);
                    },
                  ),
                  // action button
                  IconButton(
                    icon: Icon(choices[1].icon),
                    onPressed: () {
                      _select(choices[1]);
                    },
                  ),
      

      https://flutter.dev/docs/catalog/samples/basic-app-bar

      【讨论】:

        【解决方案3】:

        查看下面的代码:

        class MyHomePage extends StatefulWidget {
          MyHomePage({Key key, this.title}) : super(key: key);
        
          final String title;
        
          @override
          _MyHomePageState createState() => _MyHomePageState();
        }
        
        class _MyHomePageState extends State<MyHomePage> {
        
          Choice _selectedChoice = choices[0]; // The app's "state".
        
          void _select(Choice choice) {
            setState(() { // Causes the app to rebuild with the new _selectedChoice.
              _selectedChoice = choice;
            });
          }
        
          @override
          void initState() {
            // TODO: implement initState
            super.initState();
          }
        
        @override
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                title: const Text('Basic AppBar'),
                actions: <Widget>[
                  // action button
                  IconButton(
                    icon: Icon(choices[0].icon),
                    onPressed: () {
                      _select(choices[0]);
                    },
                  ),
                  // action button
                  IconButton(
                    icon: Icon(choices[1].icon),
                    onPressed: () {
                      _select(choices[1]);
                    },
                  ),
                  // overflow menu
                  PopupMenuButton<Choice>(
                    onSelected: _select,
                    itemBuilder: (BuildContext context) {
                      return choices.skip(2).map((Choice choice) {
                        return PopupMenuItem<Choice>(
                          value: choice,
                          child: Text(choice.title),
                        );
                      }).toList();
                    },
                  ),
                ],
              ),
              body: Padding(
            padding: const EdgeInsets.all(16.0),
            child: ChoiceCard(choice: _selectedChoice),
          ),
            );
          }
        }
        
        class Choice {
          const Choice({ this.title, this.icon });
          final String title;
          final IconData icon;
        }
        
        const List<Choice> choices = <Choice>[
          Choice(title: 'Car', icon: Icons.directions_car),
          Choice(title: 'Bicycle', icon: Icons.directions_bike),
          Choice(title: 'Boat', icon: Icons.directions_boat),
          Choice(title: 'Bus', icon: Icons.directions_bus),
          Choice(title: 'Train', icon: Icons.directions_railway),
          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.headline4;
            return Card(
              color: Colors.white,
              child: Center(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Icon(choice.icon, size: 128.0, color: textStyle.color),
                    Text(choice.title, style: textStyle),
                  ],
                ),
              ),
            );
          }
        }
        

        希望对你有帮助!!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-01-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-18
          • 1970-01-01
          • 2021-12-13
          相关资源
          最近更新 更多