首页 > 解决方案 > 如何根据 Flutter 中的条件导航到页面

问题描述

我想根据条件导航到页面。当我选择“许可证”并按下一步按钮时,它应该重定向到许可证页面。当我选择“未经许可”并按下一步按钮时,它应该将我重定向到未经许可的页面。

从下拉列表中选择“licence”/“unlicensed”值后,它应该使用该值来确定要重定向到哪个页面。

这是我到目前为止尝试过的一些代码:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';

class BspSignupPage extends StatefulWidget {         
  @override
  _BspSignupPageState createState() => _BspSignupPageState();
}

class _BspSignupPageState extends State<BspSignupPage> {
  bool bspcheck = false;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String _dropdownError;

  File _image;

  List<String> _colors = <String>[
    '',
    'Licensed / Register',
    'Unregistered',
  ];

  List<DropdownMenuItem<String>> _dropDownItem() {
    List<String> ddl = ["License/Registered", "UN-Registered"];
    return ddl
        .map((value) => DropdownMenuItem(
              value: value,
              child: Text(value),
            ))
        .toList();
  }

  Widget _buildbusinesstype() {
    String _selectedGender;
    return new FormBuilder(
        autovalidate: true,
        child: FormBuilderCustomField(
          attribute: "Business Type",
          validators: [
            FormBuilderValidators.required(),
          ],
          formField: FormField(
            builder: (FormFieldState<dynamic> field) {
              return InputDecorator(
                decoration: InputDecoration(
                    prefixIcon: Icon(Icons.merge_type),
                    errorText: field.errorText),
                //isEmpty: _color == '',
                child: new DropdownButtonHideUnderline(
                  child: new DropdownButton(
                    value: _selectedGender,
                    items: _dropDownItem(),
                    onChanged: (value) {
                      _selectedGender = value;
                    },
                    hint: Text('Select Business Type'),
                  ),
                ),
              );
            },
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("BSP Signup"),
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        centerTitle: true,
      ),
      bottomNavigationBar: Container(
        color: Colors.transparent,
        height: 56,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            new FlatButton.icon(
              icon: Icon(Icons.close),
              label: Text('Clear'),
              //  color: Colors.redAccent,
              textColor: Colors.black,
              // padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(7),
              ),
              onPressed: () {},
            ),
            new FlatButton.icon(
                icon: Icon(Icons.ac_unit),
                label: Text('Next'),
                color: Colors.amber,
                textColor: Colors.white,
                //padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),

                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(7),
                ),
                onPressed: () async {
                  if (_formKey.currentState.validate()) {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => BspSignupPage()));
                  }
                }),
          ],
        ),
      ),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Form(
          autovalidate: true,
          key: _formKey,
          child: Stack(
            children: <Widget>[
              SingleChildScrollView(
                padding: const EdgeInsets.all(30.0),
                child: new Container(
                  child: new Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _buildbusinesstype(),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

标签: flutterdrop-down-menunavigationflutter-layoutflutter-dependencies

解决方案


您可以使用状态变量获取下拉列表的值,

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
class BspSignupPage extends StatefulWidget {


  @override
  _BspSignupPageState createState() => _BspSignupPageState();
}

class _BspSignupPageState extends State<BspSignupPage> {
  bool bspcheck = false;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String _dropdownError;

  String _dropDownValue = '';

  File _image;

  List<String> _colors = <String>[
    '',
    'Licensed / Register',
    'Unregistered',
  ];

  List<DropdownMenuItem<String>> _dropDownItem() {
    List<String> ddl = ["License/Registered", "UN-Registered"];
    return ddl
        .map((value) => DropdownMenuItem(
              value: value,
              child: Text(value),
            ))
        .toList();
  }

  Widget _buildbusinesstype() {
    String _selectedGender;
    return new FormBuilder(
        autovalidate: true,
        child: FormBuilderCustomField(
          attribute: "Business Type",
          validators: [
            FormBuilderValidators.required(),
          ],
          formField: FormField(
            builder: (FormFieldState<dynamic> field) {
              return InputDecorator(
                decoration: InputDecoration(
                    prefixIcon: Icon(Icons.merge_type),
                    errorText: field.errorText),
                //isEmpty: _color == '',
                child: new DropdownButtonHideUnderline(
                  child: new DropdownButton(
                    value: _selectedGender,
                    items: _dropDownItem(),
                    onChanged: (value) {
                      _dropDownValue = value;
                    },
                    hint: Text('Select Business Type'),
                  ),
                ),
              );
            },
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text("BSP Signup"),
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        centerTitle: true,
      ),
      bottomNavigationBar: Container(
        color: Colors.transparent,
        height: 56,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            new FlatButton.icon(
              icon: Icon(Icons.close),
              label: Text('Clear'),
              //  color: Colors.redAccent,
              textColor: Colors.black,
              // padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(7),
              ),
              onPressed: () {},
            ),
            new FlatButton.icon(
                icon: Icon(Icons.ac_unit),
                label: Text('Next'),
                color: Colors.amber,
                textColor: Colors.white,
                //padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),

                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(7),
                ),
                onPressed: () async {
                  if (_formKey.currentState.validate()) {
                    // Now use if statement here to decide which route you want to go
                   if(_dropdDown == "SOME_VALUE"){
                    // Go to this route
                    }
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => BspSignupPage()));
                  }
                }),
          ],
        ),
      ),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Form(
          autovalidate: true,
          key: _formKey,
          child: Stack(
            children: <Widget>[
              SingleChildScrollView(
                padding: const EdgeInsets.all(30.0),
                child: new Container(
                  child: new Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      _buildbusinesstype(),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

推荐阅读