我在使用 Flutter 更改頁面時遇到問題。我創建了兩個 .dart 檔案,我想通過一個按鈕從第 1 頁切換到第 2 頁。有人能幫助我嗎?
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('Kontakt'),
icon: const Icon(Icons.person),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Kontakt()),
);
},
onLongPress: () {},
),
),
在第二頁我寫了這個:
class Kontakt extends StatelessWidget {
const Kontakt({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.teal,
body: ListView(
children: <Widget>[
Center(
child: ListView(
children: [
Container(
margin: const EdgeInsets.fromLTRB(20, 27, 20, 20),
child: const Image(
image: AssetImage('assets/ENLogo.png'),
width: 200,
height: 200,
fit: BoxFit.contain,
)),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('E-Mail'),
icon: const Icon(Icons.mail),
onPressed: () {
},
onLongPress: () {
},
),
),
],
),
),
],
)),
);
}}
我有什么要改變的,以便我可以瀏覽我的檔案?在 Android Studio 中,我找不到任何錯誤訊息或類似的東西。
我預先感謝您的支持。
uj5u.com熱心網友回復:
所以你需要MaterialApp()在第二頁洗掉小部件:->第一頁
import 'package:debounce/kontakt.dart';
import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('Kontakt'),
icon: const Icon(Icons.person),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Kontakt()),
);
},
onLongPress: () {},
),
),
),
],
),
),
);
}
}
->第二頁
import 'package:flutter/material.dart';
class Kontakt extends StatelessWidget {
const Kontakt({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.teal,
body: ListView(
children: <Widget>[
Center(
child: ListView(
children: [
Container(
margin: const EdgeInsets.fromLTRB(20, 27, 20, 20),
child: const Image(
image: AssetImage('assets/ENLogo.png'),
width: 200,
height: 200,
fit: BoxFit.contain,
)),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('E-Mail'),
icon: const Icon(Icons.mail),
onPressed: () {
},
onLongPress: () {
},
),
),
],
),
),
],
));
}
}
只需在主類中參考您的第一頁
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: HomeScreen(), //Reference your new page here
);
}
}
檢查下面的圖片


uj5u.com熱心網友回復:
感謝您的快速答復。我從你那里拿走了更正的代碼。不幸的是它仍然不起作用,在終端中我得到確認它可以作業,但在模擬器中我也停留在第一頁上。
你看到其他錯誤了嗎?
我將發送第一頁的整個代碼:
import 'package:flutter/material.dart';
import 'package:utile_dulci/kontakt.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
leading: const Icon(Icons.home),
title: const Center(
child: Text(
'Utile Dulci',
style:
TextStyle(color: Colors.white, fontSize: 30, shadows: <Shadow>[
Shadow(offset: Offset(0, 0), blurRadius: 30, color: Colors.black),
]),
)),
backgroundColor: Colors.teal[700],
actions: [
Container(
width: 10,
),
const Icon(Icons.favorite),
Container(
width: 20,
),
],
),
body: Center(
child: ListView(
children: [
Container(
margin: const EdgeInsets.fromLTRB(20, 27, 20, 20),
child: const Image(
image: AssetImage('assets/ENLogo.png'),
width: 200,
height: 200,
fit: BoxFit.contain,
)),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('Rezepte'),
icon: const Icon(Icons.restaurant_menu),
onPressed: () {},
onLongPress: () {},
),
),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('Workout'),
icon: const Icon(Icons.fitness_center),
onPressed: () {},
onLongPress: () {},
),
),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('Werkzeuge'),
icon: const Icon(Icons.handyman),
onPressed: () {},
onLongPress: () {},
),
),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('Kontakt'),
icon: const Icon(Icons.person),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Kontakt()),
);
},
onLongPress: () {},
),
),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('App-Info'),
icon: const Icon(Icons.info),
onPressed: () {},
onLongPress: () {},
),
),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('Weitere Apps'),
icon: const Icon(Icons.download),
onPressed: () {},
onLongPress: () {},
),
),
],
),
),
backgroundColor: Colors.teal[800],
),
);
}
}
“新”第二頁:
import 'package:flutter/material.dart';
class Kontakt extends StatelessWidget {
const Kontakt({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.teal,
body: ListView(
children: <Widget>[
Center(
child: ListView(
children: [
Container(
margin: const EdgeInsets.fromLTRB(20, 27, 20, 20),
child: const Image(
image: AssetImage('assets/ENLogo.png'),
width: 200,
height: 200,
fit: BoxFit.contain,
)),
Container(
margin: const EdgeInsets.fromLTRB(30, 7, 30, 7),
child: ElevatedButton.icon(
style: ElevatedButton.styleFrom(
shadowColor: Colors.black,
fixedSize: const Size(300, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)),
primary: Colors.teal[700],
onPrimary: Colors.white,
textStyle: const TextStyle(
fontSize: 25,
fontStyle: FontStyle.normal,
),
),
label: const Text('E-Mail'),
icon: const Icon(Icons.mail),
onPressed: () {
},
onLongPress: () {
},
),
),
],
),
),
],
));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/491919.html
