Flutter 如何在动态列表视图中导航到下一页-颤振

Flutter 如何在动态列表视图中导航到下一页-颤振,flutter,navigator,Flutter,Navigator,我有一个类别列表,并使用ListViewBuilder将其显示在ListTile中。我想转到点击的特定类别页面。代码示例: final category = [ 'Category One', 'Category Two', 'Category Three', ], 我正在编写导航器。按如下所示:但这里我想要动态的东西。我希望你得到它 onTap(){ Navigator.push(context, MaterialPageRoute(builder: (context)=>Catego

我有一个类别列表,并使用ListViewBuilder将其显示在ListTile中。我想转到点击的特定类别页面。代码示例:

final category = [
'Category One',
'Category Two',
'Category Three',
],
我正在编写导航器。按如下所示:但这里我想要动态的东西。我希望你得到它

onTap(){
Navigator.push(context, MaterialPageRoute(builder: (context)=>CategoryOne(),),);
}

如果您对每种可能性都有一个类别小部件,那么您可以在MaterialPageRoute生成器中设置一个开关:

Navigator.push(context, MaterialPageRoute(builder: (context) {
  switch (selectedCategory) {
    case 'Category One':
      return CategoryOne();
    break;
    case 'Category Two':
      return CategoryTwo();
    break;
    case 'Category Three':
      return CategoryThree();
      break;

    default:
      return CategoryOne();
  }
}));

如果您对每种可能性都有一个类别小部件,那么您可以在MaterialPageRoute生成器中设置一个开关:

Navigator.push(context, MaterialPageRoute(builder: (context) {
  switch (selectedCategory) {
    case 'Category One':
      return CategoryOne();
    break;
    case 'Category Two':
      return CategoryTwo();
    break;
    case 'Category Three':
      return CategoryThree();
      break;

    default:
      return CategoryOne();
  }
}));

您可以使用路线轻松导航

下面的示例可能会让您清楚如何使用route实现您的期望输出

import 'package:flutter/material.dart';
import 'package:master/them.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeGenerator.themeDataGenerator,
      routes: {
        '/': (context) => MyHomePage(),
        '/firstCategory': (context) => FirstCategory(),
        '/secondCategory': (context) => SecondCategory(),
      },
    );
  }
}

class SecondCategory extends StatelessWidget {
  const SecondCategory({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("second"),
    );
  }
}

class FirstCategory extends StatelessWidget {
  const FirstCategory({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(child: Text("First"));
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final category = [
    'firstCategory',
    'secondCategory',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Default"),
        ),
        body: ListView.builder(
          itemCount: 2,
          itemBuilder: (context, index) {
            return Center(
              child: Padding(
                padding: EdgeInsets.all(10.0),
                child: InkWell(
                  onTap: () {
                    Navigator.pushNamed(context, '/${category[index]}');
                  },
                  child: Text(
                    index.toString(),
                    textScaleFactor: 5.0,
                  ),
                ),
              ),
            );
          },
        ));
  }
}
导入“包装:颤振/材料.省道”;
导入“package:master/them.dart”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:ThemeGenerator.themeDataGenerator,
路线:{
“/”:(上下文)=>MyHomePage(),
“/firstCategory”:(上下文)=>firstCategory(),
“/secondCategory”:(上下文)=>secondCategory(),
},
);
}
}
类SecondCategory扩展了无状态小部件{
const SecondCategory({Key}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回容器(
子:文本(“第二”),
);
}
}
类FirstCategory扩展了无状态小部件{
const FirstCategory({Key}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回容器(子项:文本(“第一”));
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
最终类别=[
“第一类”,
“第二类”,
];
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“默认值”),
),
正文:ListView.builder(
物品计数:2,
itemBuilder:(上下文,索引){
返回中心(
孩子:填充(
填充:所有边缘设置(10.0),
孩子:InkWell(
onTap:(){
pushNamed(上下文“/${category[index]}”);
},
子:文本(
index.toString(),
textScaleFactor:5.0,
),
),
),
);
},
));
}
}

您可以使用路线轻松导航

下面的示例可能会让您清楚如何使用route实现您的期望输出

import 'package:flutter/material.dart';
import 'package:master/them.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeGenerator.themeDataGenerator,
      routes: {
        '/': (context) => MyHomePage(),
        '/firstCategory': (context) => FirstCategory(),
        '/secondCategory': (context) => SecondCategory(),
      },
    );
  }
}

class SecondCategory extends StatelessWidget {
  const SecondCategory({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("second"),
    );
  }
}

class FirstCategory extends StatelessWidget {
  const FirstCategory({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(child: Text("First"));
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final category = [
    'firstCategory',
    'secondCategory',
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Default"),
        ),
        body: ListView.builder(
          itemCount: 2,
          itemBuilder: (context, index) {
            return Center(
              child: Padding(
                padding: EdgeInsets.all(10.0),
                child: InkWell(
                  onTap: () {
                    Navigator.pushNamed(context, '/${category[index]}');
                  },
                  child: Text(
                    index.toString(),
                    textScaleFactor: 5.0,
                  ),
                ),
              ),
            );
          },
        ));
  }
}
导入“包装:颤振/材料.省道”;
导入“package:master/them.dart”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:ThemeGenerator.themeDataGenerator,
路线:{
“/”:(上下文)=>MyHomePage(),
“/firstCategory”:(上下文)=>firstCategory(),
“/secondCategory”:(上下文)=>secondCategory(),
},
);
}
}
类SecondCategory扩展了无状态小部件{
const SecondCategory({Key}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回容器(
子:文本(“第二”),
);
}
}
类FirstCategory扩展了无状态小部件{
const FirstCategory({Key}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
返回容器(子项:文本(“第一”));
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
最终类别=[
“第一类”,
“第二类”,
];
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“默认值”),
),
正文:ListView.builder(
物品计数:2,
itemBuilder:(上下文,索引){
返回中心(
孩子:填充(
填充:所有边缘设置(10.0),
孩子:InkWell(
onTap:(){
pushNamed(上下文“/${category[index]}”);
},
子:文本(
index.toString(),
textScaleFactor:5.0,
),
),
),
);
},
));
}
}

您可以在ListView的生成器函数中使用if语句。此外,我认为没有必要这样做,因为这将是非常麻烦的。您应该有一个类别模型,该模型具有标识每个类别的唯一ID,并且在类别屏幕上有一个单独的页面。对于每个类别ID,您可以在单个页面上显示该类别的相关数据。您可以在ListView的生成器函数中使用if语句。此外,我认为没有必要这样做,因为这将是非常麻烦的。您应该有一个类别模型,该模型具有标识每个类别的唯一ID,并且在类别屏幕上有一个单独的页面。对于每个类别ID,您可以在单个页面上显示该类别的相关数据。