Flutter:使用导航器pushNamed

时间:2019-12-05 13:55:48

标签: flutter dart

我正在使用带有路由器的MaterialApp导航到下一个屏幕,我正在通过传递参数将其用于导航到另一个屏幕:

onTap: () => Navigator.of(context).pushNamed(
                                DetailDebt.routedName,
                                arguments: {
                                  'receiverName': debtListView.receiverName,
                                  'amount': debtListView.amount,
                                  'dateReturn': debtListView.dateReturn,
                                  'pathImage': debtListView.imageReceiver,
                                  'signatureImage': debtListView.imageSignature,
                                },
                              ),

在下一个屏幕中,我将使变量存储如下:

final Map arguments = ModalRoute.of(context).settings.arguments as Map;
  return Scaffold(
      body: Container(
        child: Text(
          arguments['receiverName'],
        ),
      ),
    );

我的问题是,如何将参数存储在变量中,然后我可以这样称呼它:

// I want Store the Arguments in the variable , so i can call it something like this  : 
final Map arguments = ModalRoute.of(context).settings.arguments as Map;
final loadedArguments = arguments; // <= ???
  return Scaffold(
      body: Container(
        child: Text(
          loadedArguments.receiverName,
        ),
      ),
    );

如果需要,这是我的模特:

import 'dart:typed_data';

import 'package:hive/hive.dart';
part 'debt_model_hive.g.dart';

@HiveType()
class DebtModelHive extends HiveObject {
  @HiveField(0)
  String receiverName;
  @HiveField(1)
  int amount;
  @HiveField(2)
  DateTime dateReturn;
  @HiveField(3)
  String imageReceiver;
  @HiveField(4)
  Uint8List imageSignature;

  DebtModelHive({
    this.receiverName,
    this.amount,
    this.dateReturn,
    this.imageReceiver,
    this.imageSignature,
  });
  // Hive fields go here
}

谢谢

1 个答案:

答案 0 :(得分:1)

我将向您展示一个简单的示例,该示例如何将参数传递给页面。在这里,我用参数中的int值推送了一个新的命名路由:

onPressed: () {
  Navigator.of(context).pushNamed('my_route', arguments: 100);
},

在我的MaterialApp中,我设置了onGenerateRoute参数以生成具有给定参数的新路由:

onGenerateRoute: (settings) {
  switch (settings.name) {
    case 'my_route': return MaterialPageRoute(
      builder: (context) => HomePage(settings.arguments as int), // passing arguments
      settings: settings,
    );
    default: throw Exception('Unknown route');
  }
},

现在,我可以访问build方法内部的值了:

class HomePage extends StatefulWidget {
  final int arguments;

  HomePage(this.arguments, {Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int value;

  @override
  void initState() {
    super.initState();

    value = widget.arguments;
  }

  @override
  Widget build(BuildContext context) {
    return Text('Value from arguments: ${widget.arguments}');
  }
}

或者,您可以像这样在initState内通过route的参数设置值:

  int value;

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      setState(() {
        value = ModalRoute.of(this.context).settings?.arguments;
      });
      print('setting value from route arguments');
    });
  }

但是,它很脏,我不建议这样做。