I'm trying to navigate to specific widget from the list of persons that I have defined in a json file. My json tree looks like this:
[ { "name": "Abraham", "widget": "AbrahamWidget" }, { "name": "Ross", "widget": "RossWidget", }, ] When I tap on any of the names, I handle the navigation with onTap to navigate to the SearchResultNameWidget where I check where to go with an if statement.
onTap: () { Navigator.push(context, MaterialPageRoute( builder: (context) => SearchResultNameWidget(data[index]), )); }, My SearchResultNameWidget is:
import 'package:flutter/material.dart'; import 'package:nameapp/persons/abraham.dart'; import 'package:nameapp/persons/ross.dart'; class SearchResultNameWidget extends StatelessWidget { SearchResultNameWidget(this.data); final data; @override Widget build(BuildContext context) { _person() { if (data["name"] == "Abraham") { return AbrahamWidget(); } else if (data["name"] == "Ross") { return RossWidget(); } } return _person(); } } It works fine and navigate to the specific widget of a person from the list. But is there a better way to do this? If I had a lost of persons, defining if statements for each of them is very inconvenient. Can I bring the widget value from the json to programatically navigate to the specific widget of that person?
没有评论:
发表评论