所以我有一個問題:我的問題有點復雜,但我試著向你解釋......所以當我點擊影像時,我的應用程式就像這樣,影像也會改變,標題也會改變(比如粥)你是能夠點擊標題并進入第二頁,但標題沒有改變這是我的問題,因為我正在使用串列


這里正在改變(第一個螢屏截圖):
class DicePage extends StatefulWidget {
const DicePage({Key? key}) : super(key: key);
@override
_DicePageState createState() => _DicePageState();
}
class _DicePageState extends State<DicePage> {
int PicNumber = 1;
void changeDiceNumber() {
setState(() {
PicNumber = Random().nextInt(4) 1;
});
}
final List<Sachen> infoBank = [
Sachen(title: "Schoko-Bowl", image: Image.asset("Images/frue1.png"), text: '-Mehl'),
Sachen(title: "Smoothie-Bowl", image: Image.asset("images/frue2.png"), text: '-Zucker'),
Sachen(title: "Porridge", image: Image.asset("images/frue3.png"), text: '-Milch'),
Sachen(title: "Porridge", image: Image.asset("images/frue4.png"), text: '-Eier'),
Sachen(title: "Schoko Mousse", image: Image.asset("images/frue5.png"),text: '-Butter'),
];
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Card(
margin: const EdgeInsets.fromLTRB(50, 35, 50, 0),
elevation: 15,
color: const Color(0xFF4caf50),
child: SizedBox(
height: 80,
width: 180,
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 18),
child: GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => Rezept('Dein Frühstück'),
),
);
},
child: Center(
child: Text(infoBank[PicNumber].title,
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
),
),
),
),
),
),
Row(
children: [
Expanded(
child: TextButton(
onPressed: () {
setState(() {
PicNumber ;
});
changeDiceNumber();
print('LeftDiceNumber = $PicNumber');
},
child: Container(
height: 415,
width: 350,
margin: const EdgeInsets.fromLTRB(10, 20, 10, 20),
decoration: BoxDecoration(
border: Border.all(
width: 3,
color: Colors.grey.shade700,
),
),
child: FittedBox(
child: infoBank[PicNumber].image,
fit: BoxFit.fill,
clipBehavior: Clip.hardEdge,
),
),
),
),
],
),
],
),
);
}
}
這里沒有改變它是第二個螢屏截圖的代碼:
import 'package:flutter/material.dart';
import 'Sachen.dart';
import 'dart:math';
class Rezept extends StatelessWidget{
final String rezept;
Rezept(this.rezept);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xFF4caf50),
title: Text(rezept),
foregroundColor: Colors.white,
titleTextStyle: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
body: RezeptPage(),
);
}
}
class RezeptPage extends StatefulWidget {
const RezeptPage ({Key? key}) : super(key: key);
@override
_RezeptPageState createState() => _RezeptPageState();
}
class _RezeptPageState extends State<RezeptPage> {
int PiNumber = 1;
void changeDiceNumber() {
setState(() {
PiNumber = Random().nextInt(4) 1;
});
}
final List<Sachen> infoBank = [
Sachen(title: "Schoko-Bowl", image: Image.asset("Images/frue1.png"), text: '-Mehl'),
Sachen(title: "Smoothie-Bowl", image: Image.asset("images/frue2.png"), text: '-Zucker'),
Sachen(title: "Porridge", image: Image.asset("images/frue3.png"), text: '-Milch'),
Sachen(title: "Porridge", image: Image.asset("images/frue4.png"), text: '-Eier'),
Sachen(title: "Schoko Mousse", image: Image.asset("images/frue5.png"),text: '-Butter'),
];
@override
Widget build(BuildContext context) {
return ListView(
children: <Widget>[
SizedBox(
child: Padding(
padding: EdgeInsets.fromLTRB(10, 15, 10, 15),
child: Card(
elevation: 8,
child: ListTile(
title: Center(
child: Text(infoBank[PiNumber].title,
style: const TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
),
SizedBox(
width: 350,
height: 500,
child: Card(
elevation: 5,
margin: EdgeInsets.fromLTRB(10, 5, 10, 20),
child: Padding(
padding: EdgeInsets.fromLTRB(15, 15, 10, 0),
child: RichText(text: const TextSpan(
text: 'Zutaten:',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
fontSize: 22.0,
decoration: TextDecoration.underline,
decorationThickness: 2.0,
),
children: <TextSpan>[
TextSpan(
text: '\n\n-Mehl' '\n-Zucker' '\n-Milch' '\n-Eier' '\n-Butter',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.normal,
decoration: TextDecoration.none,
),
),
TextSpan(
text: '\n\nZubereitung:',
),
TextSpan(
text: '\n\n-Zuerst vermischen wir alles' '\n-Anschlie?end wird es in der Pfanne angebraten' '\n-Zuletzt mit Schokolade bestreichen und genie?en',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.normal,
decoration: TextDecoration.none,
),
),
],
),
),
),
),
),
],
);
}
}
我希望你能理解我的問題并且已經謝謝你了
uj5u.com熱心網友回復:
您真正的問題是,如何將值(在您的情況下為 PiNumber)從一個螢屏傳遞到另一個螢屏。這就是您在推送到另一個螢屏時需要做的事情。這是一篇解釋其作業原理的文章:https ://fluttercorner.com/how-to-pass-value-from-one-screen-to-another-screen-using-navigator-in-flutter/
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/447448.html
