Ad
How To Get Listview Button To Go To Random Route OnTap
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
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
Related Questions
- → How do you create a 12 or 24 mnemonics code for multiple cryptocurrencies (ETH, BTC and so on..)
- → Flutter: input text field don't work properly in a simple example..... where am I wrong?
- → Can I customize the code formatting of Dart code in Atom?
- → Is it possible to develop iOS apps with Flutter on a Linux virtual machine?
- → Display SnackBar in Flutter
- → JSON ObjectMapper in Flutter
- → Material flutter app source code
- → TabBarSelection No such method error
- → How do I set the animation color of a LinearProgressIndicator?
- → Add different routes/screens to Flutter app
- → Is there a way to get the size of an existing widget?
- → How to share a file using flutter
- → Is there an easy way to find particular text built from RichText in a Flutter test?
Ad