Mahender Kumar
Mahender Kumar

Reputation: 29

How to Navigate to Next Page in Dynamic List View - Flutter

I have a list of Categories and displaying it in ListTile using ListViewBuilder. I want to go to the particular category page which is tapped. example of code:

final category = [
'Category One',
'Category Two',
'Category Three',
],

I'm writing the Navigator.push something like below: but here I want to something dynamic. I hope u got it.

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

Upvotes: 2

Views: 5742

Answers (2)

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27147

you can use route to navigate easily.

Following Example may clear your idea about how you can achieve your desire output with 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,
                  ),
                ),
              ),
            );
          },
        ));
  }
}

Upvotes: 2

J. S.
J. S.

Reputation: 9625

If you have a Category Widget for each possibility then you can a Switch inside the MaterialPageRoute builder:

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();
  }
}));

Upvotes: 2

Related Questions