如何在Flutter中将页面移动到另一个页面



(首先,很抱歉英语不好。我正在使用翻译(
你好,我是Flutter的新手
我正试图移动页面。(主要针对NewMemory(,但它不起作用
我正在使用navigation&路线
当我点击图像(或图标(时,没有任何变化。我真的想转到另一页

这是我的代码。你能给我一些建议吗?

import 'package:flutter/material.dart';
import 'newmemory.dart';
var count = 0;
var height = 0.0;
var width = 0.0;
var devicePixelRatio = 0.0;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '행복저금통',
theme: new ThemeData(scaffoldBackgroundColor: const Color(0xff3d56a9)),
home: Scaffold(
// appBar: AppBar(
//   title: Text('main'),
// ),
body: Center(
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Positioned(
//공유버튼
child: IconButton(
icon: Icon(Icons.share_outlined),
color: Colors.black,
iconSize: 50,
onPressed: () {},
),
top: 30,
left: 5,
),
Positioned(
//카운트
child: Text("$count", //이후 함수로 변경
style: TextStyle(fontSize: 50, color: Colors.white)),
top: 50,
),
Positioned(
//유리병
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute<void>(
builder: (context) => NewMemory())); //!!!here!!!
},
child: Container(
height: 350,
width: 350,
child: Image.asset('images/glass.png'))),
top: 150,
),
Positioned(
//종이학
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute<void>(
builder: (context) => NewMemory()));
},
child: Container(
height: 300,
width: 300,
child: Image.asset('images/paper3.png'))),
bottom: -150,
),
Positioned(
//우측 아이콘
child: Column(
children: [
IconButton(
icon: Icon(Icons.settings),
color: Colors.black,
iconSize: 50,
onPressed: () {} //설정
),
IconButton(
icon: Icon(Icons.add_circle_outline),
color: const Color(0xffb9dcb3),
iconSize: 50,
onPressed: () {} //유리병 추가
),
IconButton(
icon: Icon(Icons.do_not_disturb),
color: Colors.redAccent,
iconSize: 50,
onPressed: () {} //유리병 삭제
),
],
),
top: 30,
right: 5,
)
],
),
),
),
);
}
}

import 'package:flutter/material.dart';
class NewMemory extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height: 300,
width: 300,
child: Image.asset('images/paper3.png'))),
);
}
}

避免将第一个屏幕小部件放在与MaterialApp小部件相同的有状态/无状态小部件中。

import 'package:flutter/material.dart';
import 'newmemory.dart';
var count = 0;
var height = 0.0;
var width = 0.0;
var devicePixelRatio = 0.0;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '행복저금통',
theme: new ThemeData(scaffoldBackgroundColor: const Color(0xff3d56a9)),
home: FirstPage(),
);
}
}

在同一文件中制作另一个小工具

class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
// appBar: AppBar(
//   title: Text('main'),
// ),
body: Center(
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Positioned(
//공유버튼
child: IconButton(
icon: Icon(Icons.share_outlined),
color: Colors.black,
iconSize: 50,
onPressed: () {},
),
top: 30,
left: 5,
),
Positioned(
//카운트
child: Text("$count", //이후 함수로 변경
style: TextStyle(fontSize: 50, color: Colors.white)),
top: 50,
),
Positioned(
//유리병
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NewMemory())); //!!!here!!!
},
child: Container(
height: 350,
width: 350,
child: Image.asset('images/glass.png'))),
top: 150,
),
Positioned(
//종이학
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NewMemory()));
},
child: Container(
height: 300,
width: 300,
child: Image.asset('images/paper3.png'))),
bottom: -150,
),
Positioned(
//우측 아이콘
child: Column(
children: [
IconButton(
icon: Icon(Icons.settings),
color: Colors.black,
iconSize: 50,
onPressed: () {} //설정
),
IconButton(
icon: Icon(Icons.add_circle_outline),
color: const Color(0xffb9dcb3),
iconSize: 50,
onPressed: () {} //유리병 추가
),
IconButton(
icon: Icon(Icons.do_not_disturb),
color: Colors.redAccent,
iconSize: 50,
onPressed: () {} //유리병 삭제
),
],
),
top: 30,
right: 5,
)
],
),
),
);
}
}

根据文档,导航语法为:

Navigator.push(
context,
MaterialPageRoute(builder: (context) => NewMemory()),
);

最新更新