Flutter-对话框开始新活动

时间:2018-07-04 13:16:12

标签: android dart flutter

在创建团队之后,我正在开发一个计算点数的应用程序。创建团队后,会弹出AlertDialog并显示名称。然后应该可以单击一个按钮以打开新活动。该活动不应与上一个活动相关。是否有人有想法,如何实现?

以下是对话框活动的代码段:

import 'dart:math';
import 'package:flutter/material.dart';

import 'punktezaehler.dart';

class Team_Dialog extends StatefulWidget{

  final List<String> team_namen;

  Team_Dialog(this.team_namen);

  @override
  State<StatefulWidget> createState() => new _TeamDialogState(team_namen);

}

class _TeamDialogState extends State<Team_Dialog>{

  final List<String> team_namen;
  _TeamDialogState(this.team_namen);

  @override
  Widget build(BuildContext context) {
    return new AlertDialog(
      content: new SingleChildScrollView(
        child: new ListBody(
            children: List.generate(1, (index){
              return Column(
                children: <Widget>[
                  new Row(
                    children: <Widget>[
                      Text("Team 1: ", style: TextStyle(fontFamily: "Roboto")),
                      Text(team_namen[0] + " und " + team_namen[1])
                    ],
                  ),
                  new Row(
                    children: <Widget>[
                      Text("Team 2: "),
                      Text(team_namen[2] + " und " + team_namen[3])
                    ],
                  )
                ],
              );
            })
        ),
      ),
      actions: <Widget>[
        new FlatButton(
            color: Colors.red,
            splashColor: Colors.red[900],
            onPressed: (){
              Navigator.of(context).pop();
            },
            child: new Text("Abort", style: TextStyle(color: Colors.white),)
        ),
        new IconButton(
            icon: Icon(Icons.shuffle),
            onPressed: (){
              shuffle(team_namen);
              setState(() {
              });
            }
        ),
        new FlatButton(
            color: Colors.green,
            splashColor: Colors.green[800],
            onPressed: () => , //After click it should start new Activity
            child: new Text("Start Game", style: TextStyle(color:  Colors.white))
        )
      ],
    );
  }



  List shuffle(List items) {
    var random = new Random();

    for (var i = items.length - 1; i > 0; i--) {

      var n = random.nextInt(i + 1);

      var temp = items[i];
      items[i] = items[n];
      items[n] = temp;
    }

    return items;
  }


}

如果有人有主意:D

2 个答案:

答案 0 :(得分:2)

实际上,当您谈论Flutter时,请考虑页面而不是活动。应该是这样的:

    [Execute SQL Task] Error: 
            Executing the query "SELECT CONVERT(BIGINT, N'aa1') AS Status
            ..." failed with the following error: 
            "Error converting data type nvarchar to bigint.". 
            Possible failure reasons: Problems with the query, 
            "ResultSet" property not set correctly, 
             parameters not set correctly,
             or connection not established correctly.

SecondScreen是另一个具有自己的Widget build(BuildContext context)方法的小部件,您可以在其中声明此页面上的内容。

如果您想返回,可以执行以下操作:

Navigator.push(context,
   MaterialPageRoute(builder: (context) => SecondScreen()),);

来源documentation

您还可以使用命名路线进行导航。示例:

 Navigator.pop(context);

类似的东西

MaterialApp(
  // Start the app with the "/" named route. In our case, the app will start
  // on the FirstScreen Widget
  initialRoute: '/',
  routes: {
    // When we navigate to the "/" route, build the FirstScreen Widget
    '/': (context) => FirstScreen(),
    // When we navigate to the "/second" route, build the SecondScreen Widget
    '/second': (context) => SecondScreen(),
  },
);

Documentation

答案 1 :(得分:1)

使用Navigator.push()Navigator.pushNamed()然后删除后退按钮不是一个好习惯。因为您要离开的页面将保留在页面堆栈中。

您实际应该使用的是Navigator.pushReplacement() ,如果您不希望用户能够返回上一页

如果您是通过对话框执行此操作,则应首先弹出对话框,然后推送下一页。