试图回到AppBar上的后退按钮以返回Main.dart,但出现错误。有什么线索让我错过了吗?
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
class ProductPage extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Stuff',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
var bannerItems = ["Building Material", "Vehicles", "Clothes", "Food and Drink","Outdoor Items"];
var bannerImage = [
"images/buildingmaterial.png",
"images/car.jpg",
"images/clothing.jpg",
"images/food.jpg",
"images/outdooritems.jpg"
];
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
var screenHeight = MediaQuery.of(context).size.height;
var screenWidth = MediaQuery.of(context).size.width;
Future<List<Widget>> createList() async {
List<Widget> items = new List<Widget>();
String dataString =
await DefaultAssetBundle.of(context).loadString("assets/data.json");
List<dynamic> dataJSON = jsonDecode(dataString);
dataJSON.forEach((object) {
String finalString= "";
List<dynamic> dataList = object["placeItems"];
dataList.forEach((item){
finalString = finalString + item + " | ";
});
items.add(Padding(padding: EdgeInsets.all(2.0),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10.0)),
boxShadow: [
BoxShadow(
color: Colors.black12,
spreadRadius: 2.0,
blurRadius: 5.0
),
]
),
margin: EdgeInsets.all(5.0),
child: Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(topLeft: Radius.circular(10.0),bottomLeft: Radius.circular(10.0)),
child: Image.asset(object["placeImage"],width: 80,height: 80,fit: BoxFit.cover,),
),
SizedBox(
width: 250,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(object["placeName"]),
Padding(
padding: const EdgeInsets.only(top: 2.0,bottom: 2.0),
child: Text(finalString,overflow: TextOverflow.ellipsis,style: TextStyle(fontSize: 12.0,color: Colors.black54,),maxLines: 1,),
),
Text("Min. Order: ${object["minOrder"]}",style: TextStyle(fontSize: 12.0,color: Colors.black54),)
],
),
),
)
],
),
),));
});
return items;
}
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => Main(),
)),
),
title: Text('Product Page'),
backgroundColor: Colors.deepOrange,
),
body: Container(
height: screenHeight,
width: screenWidth,
child: SafeArea(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
// IconButton(icon: Icon(Icons.arrow_back), onPressed: () {}),
//Text("Categories",
// style: TextStyle(fontSize: 50, fontFamily: "Samantha"),),
// IconButton(icon: Icon(Icons.person), onPressed: () {})
],
),
),
BannerWidgetArea(),
Container(
child: FutureBuilder(
initialData: <Widget>[Text("")],
future: createList(),
builder: (context,snapshot){
if(snapshot.hasData){
return Padding(
padding: EdgeInsets.all(8.0),
child: ListView(
primary: false,
shrinkWrap: true,
children: snapshot.data,
),
);
} else {
return CircularProgressIndicator();
}
}),
)
],
),
)),
),
floatingActionButton: FloatingActionButton(onPressed: (){},
backgroundColor: Colors.black,
child: Icon(MdiIcons.food,color: Colors.white,)),
);
}
}
class BannerWidgetArea extends StatelessWidget {
@override
Widget build(BuildContext context) {
var screenWidth = MediaQuery.of(context).size.width;
PageController controller =
PageController(viewportFraction: 0.8, initialPage: 1);
List<Widget> banners = new List<Widget>();
for (int x = 0; x < bannerItems.length; x++) {
var bannerView = Padding(
padding: EdgeInsets.all(10.0),
child: Container(
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
boxShadow: [
BoxShadow(
color: Colors.black38,
offset: Offset(2.0, 2.0),
blurRadius: 5.0,
spreadRadius: 1.0)
]),
),
ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
child: Image.asset(
bannerImage[x],
fit: BoxFit.cover,
),
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [Colors.transparent, Colors.black])),
),
Padding(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
bannerItems[x],
style: TextStyle(fontSize: 25.0, color: Colors.white),
),
],
),
)
],
),
),
);
banners.add(bannerView);
}
return Container(
width: screenWidth,
height: screenWidth * 9 / 16,
child: PageView(
controller: controller,
scrollDirection: Axis.horizontal,
children: banners,
),
);
}
}
Main.dart
import 'package:flutter/material.dart';
import 'package:mics_delta/privacypage.dart';
import 'package:mics_delta/productpage.dart';
import 'package:mics_delta/searchpage.dart';
import 'package:mics_delta/aboutus.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Stuff',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stuff'),
backgroundColor: Colors.blue,
),
drawer: Drawer(
child: ListView(
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
gradient: LinearGradient(colors: <Color>[
Colors.red[300],
Colors.red[100],
])
),
child: Container(
child: Column(
children: <Widget>[
Material(
borderRadius: BorderRadius.all(Radius.circular(40.0)),
child: Image.asset('images/Canada_flag.png', width: 100,height: 100,),
),
Text('Thats one good looking Flag', style: TextStyle(color: Colors.white, fontSize: 16.0),)
],
),
)),
Column(
children: <Widget>[
CustomListTile(Icons.search,'Search', () {
Navigator.push(context, MaterialPageRoute(builder: (context) => SearchPage()));
}),
],
),
Column(
children: <Widget>[
CustomListTile(Icons.fastfood,'Products', () {
Navigator.push(context, MaterialPageRoute(builder: (context) => ProductPage()));
}),
],
),
CustomListTile(Icons.add,'Add Products',()=>{}),
Column(
children: <Widget>[
CustomListTile(Icons.people,'About Us', () {
Navigator.push(context, MaterialPageRoute(builder: (context) => AboutUsPage()));
}),
],
),
Column(
children: <Widget>[
CustomListTile(Icons.vpn_lock,'Privacy', () {
Navigator.push(context, MaterialPageRoute(builder: (context) => PrivacyPage()));
}),
],
),
],
),)
);
}
}
class CustomListTile extends StatelessWidget{
IconData icon;
String text;
Function onTap;
CustomListTile(this.icon, this.text, this.onTap,);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(8.0, 0, 8.0, 0),
child: Container(
decoration: BoxDecoration(
border: Border(bottom: BorderSide(color: Colors.grey.shade400))
),
child: InkWell(
splashColor: Colors.orangeAccent,
onTap: onTap,
child: Container(
height: 50,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(icon),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(text, style: TextStyle(
fontSize: 16.0
),
),
),
],
),
Icon(Icons.arrow_right)
],
),
),
),
),
);
}
}
答案 0 :(得分:1)
与其将小部件再次放置在堆栈顶部,不如将其从堆栈中弹出。 检查下面的代码,它运行完美,
class ProductPage extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Stuff',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'This is the product page',
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.w500,
),
),
SizedBox(
width: 150,
height: 45,
child: FlatButton(
color: Colors.blue,
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => MyHomePage(),
),
),
child: Center(
child: Text('Go to home page'),
),
),
)
],
),
),
),
);
}
}
输出:
希望这会有所帮助。
更新:尝试一下。
IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomeScreen(),
),
),
),