Flutter Listview 为什么标题没有改变?
Flutter Listview why is the title not changing?
所以我有一个问题:
我的问题有点复杂,但我试着向你解释一下......
所以我的应用程序就像这样,当我点击图像时,图像会改变,标题也会改变(比如粥)
您可以点击标题进入第二页,但标题没有改变这是我的问题,因为我正在使用列表
这里是 chnaging(第一个截图):
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,
),
),
],
),
),
),
),
),
],
);
}
}
我希望你理解我的问题并且已经谢谢你
您真正的问题是,如何将值(在您的情况下为 PiNumber)从一个屏幕传递到另一个屏幕。这就是你在推送到另一个屏幕时需要做的。这是一篇解释其工作原理的文章:https://fluttercorner.com/how-to-pass-value-from-one-screen-to-another-screen-using-navigator-in-flutter/
所以我有一个问题:
我的问题有点复杂,但我试着向你解释一下......
所以我的应用程序就像这样,当我点击图像时,图像会改变,标题也会改变(比如粥)
您可以点击标题进入第二页,但标题没有改变这是我的问题,因为我正在使用列表
这里是 chnaging(第一个截图):
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,
),
),
],
),
),
),
),
),
],
);
}
}
我希望你理解我的问题并且已经谢谢你
您真正的问题是,如何将值(在您的情况下为 PiNumber)从一个屏幕传递到另一个屏幕。这就是你在推送到另一个屏幕时需要做的。这是一篇解释其工作原理的文章:https://fluttercorner.com/how-to-pass-value-from-one-screen-to-another-screen-using-navigator-in-flutter/