【问题标题】:Flutter CupertinoActionSheet: how to define Variable number of actionsFlutter CupertinoActionSheet:如何定义可变数量的动作
【发布时间】:2020-11-02 17:23:06
【问题描述】:

取决于 List distinctEmnen 中的条目数, 我想显示可变数量的菜单选项。 有没有可能实现这样的目标?

CupertinoActionSheet(
              title: Text( tjo),
              actions: [
                  CupertinoActionSheetAction(
                      child: Text( distinctEmnen[0]),
                        Navigator.of(context).pushNamed(distinctEmnen[0]);
                      }),
                  CupertinoActionSheetAction(
                      child: Text( distinctEmnen[1]),
                      onPressed: () { 
                        Navigator.of(context).pushNamed(distinctEmnen[1]);
                      }),

                  CupertinoActionSheetAction(
                      child: Text( distinctEmnen[n...]),
                      onPressed: () { 
                        Navigator.of(context).pushNamed(distinctEmnen[n...]);
                      }),

              ],
              cancelButton: CupertinoActionSheetAction(
                child: Text('Cancel'),
                onPressed: () => Navigator.of(context).pop(),
              ),
            ),

【问题讨论】:

    标签: flutter flutter-cupertino


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    第一步:你可以定义一个类Emnen
    第二步:初始化List<Emnen> distinctEmnen
    第三步:使用List<Widget>.generate(distinctEmnen.length,

    代码sn-p

    class Emnen {
      String title;
      String routeName;
    
      Emnen({this.title, this.routeName});
    }
    ...
    List<Emnen> distinctEmnen = [];
    ...
      @override
      void initState() {
        distinctEmnen = [
          Emnen(title: "1", routeName: "/1"),
          Emnen(title: "2", routeName: "/2")
        ];
        super.initState();
      }
    ...  
    CupertinoActionSheet(
          title: Text("tjo"),
          actions: List<Widget>.generate(
              distinctEmnen.length,
              (int index) => CupertinoActionSheetAction(
                  child: Text(distinctEmnen[index].title),
                  onPressed: () => Navigator.of(context)
                      .pushNamed(distinctEmnen[index].routeName))));
    

    工作演示

    完整代码

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    class Emnen {
      String title;
      String routeName;
    
      Emnen({this.title, this.routeName});
    }
    
    class CupertinoActionSheetApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) => CupertinoApp(
            initialRoute: "/",
            routes: {
              '/': (context) => HomePage(),
              '/1': (context) => FirstScreen(),
              '/2': (context) => SecondScreen(),
            },
          );
    }
    
    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      List<Emnen> distinctEmnen = [];
    
      @override
      void initState() {
        distinctEmnen = [
          Emnen(title: "1", routeName: "/1"),
          Emnen(title: "2", routeName: "/2")
        ];
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return Center(
          child: CupertinoButton(
              child: Text("show dialog"),
              onPressed: () {
                _showDialog(context);
              }),
        );
      }
    
      void _showDialog(BuildContext cxt) {
        showCupertinoModalPopup<int>(
            context: cxt,
            builder: (cxt) {
              var dialog = CupertinoActionSheet(
                  title: Text("tjo"),
                  actions: List<Widget>.generate(
                      distinctEmnen.length,
                      (int index) => CupertinoActionSheetAction(
                          child: Text(distinctEmnen[index].title),
                          onPressed: () => Navigator.of(context)
                              .pushNamed(distinctEmnen[index].routeName))));
    
              return dialog;
            });
      }
    }
    
    void main() {
      runApp(CupertinoActionSheetApp());
    }
    
    class FirstScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text("Cupertino App"),
          ),
          child: Center(
            child: Text("First"),
          ),
        );
      }
    }
    
    class SecondScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            middle: Text("Cupertino App"),
          ),
          child: Center(
            child: Text("Second"),
          ),
        );
      }
    }
    

    完整代码

    【讨论】:

    • 不错!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2013-08-07
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多