Ad

How To Get Listview Button To Go To Random Route OnTap

- 1 answer

I am trying to use solution from here How make button which open random page in Flutter? but instead of RaisedButton, do it with my Listview buttons.

Here's what I have so far after I added the RandomRouteGenerator from the other StackOverflow:

ListView(
      children: const <Widget>[
        Card(
          child: ListTile(
            leading: Text('🐔'),
            title: Text('Chicken'),
            onTap: () {
              Navigator.of(context).pushNamed(
                RouteGenerator.getRandomNameOfRoute());
            },
          ),
        ),

//... more Cards that would become other listview items

Here's the errors:

Compiler message:
lib/main.dart:163:28: Error: Not a constant expression.
              Navigator.of(context).pushNamed(
                           ^^^^^^^
lib/main.dart:163:25: Error: Method invocation is not a constant expression.
              Navigator.of(context).pushNamed(
                        ^^
lib/main.dart:164:32: Error: Method invocation is not a constant expression.
                RouteGenerator.getRandomNameOfRoute());
                               ^^^^^^^^^^^^^^^^^^^^
lib/main.dart:163:37: Error: Method invocation is not a constant expression.
              Navigator.of(context).pushNamed(
                                    ^^^^^^^^^
lib/main.dart:162:20: Error: Not a constant expression.
            onTap: () {
                   ^^

Thanks

Ad

Answer

You can copy paste run full code below
You can remove keyword const of children: const <Widget>[
code snippet

child: ListView(children: <Widget>[
            Card(

working demo

enter image description here

full code

import 'dart:math';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Random pages',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: 'start_page',
      onGenerateRoute: RouteGenerator.generateRoute,
    );
  }
}

class RouteGenerator {
  static List<String> myRandomPages = ['first_page', 'second_page'];

  static String getRandomNameOfRoute() {
    return myRandomPages[Random().nextInt(myRandomPages.length)];
  }

  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case 'start_page':
        return MaterialPageRoute(builder: (_) => StartPage());

      case 'first_page':
        return MaterialPageRoute(
            builder: (_) =>
                FirstPage()); // FirstPage - is just a Widget with your content

      case 'second_page':
        return MaterialPageRoute(
            builder: (_) => SecondPage()); // Also custom Widget

      //... other random or not pages

    }
  }
}

class StartPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Start page'),
        ),
        body: Column(
          children: [
            Expanded(
              child: ListView(children: <Widget>[
                Card(
                  child: ListTile(
                    leading: Text('🐔'),
                    title: Text('Chicken'),
                    onTap: () {
                      Navigator.of(context)
                          .pushNamed(RouteGenerator.getRandomNameOfRoute());
                    },
                  ),
                ),
              ]),
            ),
            Center(
              child: RaisedButton(
                child: Text('Go to random page'),
                onPressed: () => Navigator.of(context)
                    .pushNamed(RouteGenerator.getRandomNameOfRoute()),
              ),
            ),
          ],
        ));
  }
}

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("First Page");
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("Second Page");
  }
}
Ad
source: stackoverflow.com
Ad