Flutter CupertinoActionSheet: how to define Variable number of actions

Issue

Depending on number of entries in List distinctEmnen,
I would like to show variable number of menu-choices.
Is it possible to achieve something like this?

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(),
              ),
            ),

Solution

You can copy paste run full code below
Step 1: You can define a class Emnen
Step 2: Init List<Emnen> distinctEmnen
Step 3: Use List<Widget>.generate(distinctEmnen.length,

code snippet

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))));

working demo

enter image description here

full code

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"),
      ),
    );
  }
}

full code

Answered By – chunhunghan

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *