我需要讓這個應用程式在顫動中使用 ListView 顯示歷史人物串列。到目前為止,我已經能夠列出歷史人物的名單。我的下一個任務是為名稱旁邊的每個歷史人物制作一個按鈕,這會將您帶到另一個螢屏,其中包含有關該特定歷史人物的資訊。這就是我到目前為止所擁有的,我在實作歷史人物名稱旁邊的按鈕時遇到了麻煩。我嘗試使用 IconButton 創建一個類,但不知道如何路由它/不斷收到錯誤,以及使用 IconButton 我將如何制作一個與它們旁邊的名稱一致的按鈕串列。接受任何想法。謝謝。
import "package:flutter/material.dart";
import "historicalfigureinfo.dart";
void main() {
runApp(const MaterialApp(home: Hwk3()));
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {
'/':(context) => const Hwk3(),
'/SantosDumont':(context)=>SantosDumont(),
'/PabloPicasso':(context)=>PabloPicasso(),
},
);
}
}
class Hwk3 extends StatefulWidget {
const Hwk3({Key? key}) : super(key: key);
@override
_Hwk3State createState() => _Hwk3State();
}
class _Hwk3State extends State<Hwk3> {
var nameArray = [
'Abraham Lincoln',
'Benito Juarez',
'Claude Monet',
'Charles Darwin',
'Deng Xiaoping',
'Frederick Chopin',
'George Washington Carver',
'Georgia O\'Keeffe',
'Mahatma Gandhi',
'Mark Twain',
'Muhammad Jinnah',
'Pablo Picasso',
'Santos Dumont'
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Historical Figures'),
),
body: ListView.builder(
itemCount: nameArray.length,
itemBuilder: (BuildContext context, index) {
return ListTile(
title: Text(nameArray[index]),
trailing: InfoButtons(historicalfigure: '/SantosDumont'));
},
),
);
}
}
class InfoButtons extends StatelessWidget {
final String historicalfigure;
InfoButtons({required this.historicalfigure});
//Hector Merejo
@override
Widget build(BuildContext context) {
return (IconButton(
icon: const Icon(Icons.info),
tooltip: "Press for more information on historical figure",
onPressed: () {
Navigator.of(context).pushNamed(historicalfigure);
}));
}
}
這是我使用上面的代碼得到的錯誤。如果單擊名稱旁邊的任何按鈕,也會出現此錯誤。我認為只有在單擊 Santos Dumont 時才會出現此錯誤,如果單擊其他名稱則不會發生任何事情。
Could not find a generator for route RouteSettings("/SantosDumont", null) in the _WidgetsAppState.
對不起,如果我的解釋令人困惑。再次感謝任何幫助。
uj5u.com熱心網友回復:
home如果您使用命名路由,則不能使用引數。這就是問題所在。請參閱相關檔案。顫振檔案
uj5u.com熱心網友回復:
這對我有用.....這篇文章幫助使用 Flutter 中的串列索引進行頁面路由
class _Hwk3State extends State<Hwk3> {
var nameArray = [
'Abraham Lincoln',
'Benito Juarez',
'Claude Monet',
'Charles Darwin',
'Deng Xiaoping',
'Frederick Chopin',
'George Washington Carver',
'Georgia O\'Keeffe',
'Mahatma Gandhi',
'Mark Twain',
'Muhammad Jinnah',
'Pablo Picasso',
'Santos Dumont'
];
List<Route> historicalfigures= [
MaterialPageRoute(builder: (_) => AbrahamLincoln()),
MaterialPageRoute(builder: (_) => BenitoJuarez()),
MaterialPageRoute(builder: (_) => ClaudeMonet()),
MaterialPageRoute(builder: (_) => CharlesDarwin()),
MaterialPageRoute(builder: (_) => DengXiaoping()),
];
@override
Widget build(BuildContext context) {
List<Route> myRoute = [];
return Scaffold(
appBar: AppBar(
title: const Text('Historical Figures'),
),
body: ListView.builder(
itemCount: nameArray.length,
itemBuilder: (BuildContext context, index) {
return ListTile(
title: Text(nameArray[index]),
trailing: Icon(Icons.info_outlined),
onTap: () {
Navigator.of(context).push(historicalfigures[index]);
},
);
},
),
);
}
}
uj5u.com熱心網友回復:
看來問題是您的主要功能。使用命名路由時,不要home在MaterialApp. 此外,在您的函式中,您呼叫的是Hwk3類,而不是MyApp類,這就是為什么永遠不會呼叫路由的原因MaterialApp。
從未使用過此類
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {
'/':(context) => const Hwk3(),
'/SantosDumont':(context)=>SantosDumont(),
'/PabloPicasso':(context)=>PabloPicasso(),
},
);
}
}
在您的主要功能中,您不需要實際 MaterialApp的. 你在叫Hwk3課。
runApp(const MaterialApp(home: Hwk3()));
因此,您可以復制以下代碼。
import "package:flutter/material.dart";
import "historicalfigureinfo.dart";
void main() {
runApp(
MaterialApp(
initialRoute: '/',
routes: {
'/':(context) => const Hwk3(),
'/SantosDumont':(context)=>SantosDumont(),
'/PabloPicasso':(context)=>PabloPicasso(),
},
)
);
}
class Hwk3 extends StatefulWidget {
const Hwk3({Key? key}) : super(key: key);
@override
_Hwk3State createState() => _Hwk3State();
}
class _Hwk3State extends State<Hwk3> {
var nameArray = [
'Abraham Lincoln',
'Benito Juarez',
'Claude Monet',
'Charles Darwin',
'Deng Xiaoping',
'Frederick Chopin',
'George Washington Carver',
'Georgia O\'Keeffe',
'Mahatma Gandhi',
'Mark Twain',
'Muhammad Jinnah',
'Pablo Picasso',
'Santos Dumont'
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Historical Figures'),
),
body: ListView.builder(
itemCount: nameArray.length,
itemBuilder: (BuildContext context, index) {
return ListTile(
title: Text(nameArray[index]),
trailing: InfoButtons(historicalfigure: '/SantosDumont'));
},
),
);
}
}
class InfoButtons extends StatelessWidget {
final String historicalfigure;
InfoButtons({required this.historicalfigure});
//Hector Merejo
@override
Widget build(BuildContext context) {
return (IconButton(
icon: const Icon(Icons.info),
tooltip: "Press for more information on historical figure",
onPressed: () {
Navigator.of(context).pushNamed(historicalfigure);
}));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/433747.html
