我有这段代码并在我的页面之一中运行它,但是我想在整个应用程序中使用这部分代码,但是我不知道怎么办?我不想使用Scafold,因为当我在其他页面中使用它时,由于运行2个脚手架而造成了问题,请帮助我如何在整个应用程序中使用此代码?
floatingActionButton: Container(
height: 100.0,
width: 100.0,
decoration: BoxDecoration(
borderRadius: new BorderRadius.all(Radius.circular(60.0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.black54,
blurRadius: 10.0,
offset: Offset(0.0, 0.75))
],
color: Colors.white,
),
child: FittedBox(
child: FloatingActionButton(
backgroundColor: Color(0xffffcd05),
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Image.asset(
"images/logo-express.png",
height: 200.0,
width: 300.0,
),
),
onPressed: () {
var Router = new MaterialPageRoute(builder: (BuildContext) {
return new FirstPage();
});
Navigator.of(context).push(Router);
}),
),
//shape:
// icon: new Icon(Icons.add,
// color: Color(0xffd4351c),),
// shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(100.0),)),
// child: Image.asset("images/logo-express.png",height:200.0,width: 400.0,),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
//clipBehavior:Clip.antiAlias,
color: Color(0xffd4351c),
//shape: CircularNotchedRectangle(),
child: new Row(
mainAxisSize: MainAxisSize.min,
//mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
//SizedBox(width: double.infinity, height: 70.0,),
Material(
child: SizedBox(
// width: double.infinity,
height: 60.0,
),
color: Color(0xffd4351c),
),
// SizedBox(),
FlatButton.icon(
onPressed: () {
Navigator.push(context, SlideRightRoute(page: ContactUs()));
},
icon: Icon(
Icons.phone_in_talk,
color: Colors.white,
),
label: new Text(
'Contact Us',
style: TextStyle(color: Colors.white),
),
padding: EdgeInsets.fromLTRB(10.0, 0, 0, 0),
),
FlatButton.icon(
onPressed: () {
Navigator.push(context, SlideLeftRoute(page: AboutUs()));
},
icon: Icon(
Icons.person,
color: Colors.white,
),
label: new Text(
'About US',
style: TextStyle(color: Colors.white),
),
padding: EdgeInsets.fromLTRB(200, 0, 0.0, 0),
),
],
)),
);
}
}
class SlideRightRoute extends PageRouteBuilder {
final Widget page;
SlideRightRoute({this.page})
: super(
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) =>
page,
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) =>
SlideTransition(
position: Tween<Offset>(
begin: const Offset(-1, 0),
end: Offset.zero,
).animate(animation),
child: child,
),
);
}
class SlideLeftRoute extends PageRouteBuilder {
final Widget page;
SlideLeftRoute({this.page})
: super(
pageBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
) =>
page,
transitionsBuilder: (
BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child,
) =>
SlideTransition(
position: Tween<Offset>(
begin: const Offset(1, 0),
end: Offset.zero,
).animate(animation),
child: child,
),
);
}
将其作为您的自定义小部件,
my_fab.dart:
import 'package:flutter/material.dart';
class MyFloatingActionButton extends StatelessWidget {
final VoidCallback onPressed;
const MyFloatingActionButton({Key key, this.onPressed}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
height: 100.0,
width: 100.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(60.0),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.black54,
blurRadius: 10.0,
offset: Offset(0.0, 0.75),
)
],
color: Colors.white,
),
child: FittedBox(
child: FloatingActionButton(
backgroundColor: Color(0xffffcd05),
child: Padding(
padding: const EdgeInsets.all(4.0),
child: Image.asset(
"images/logo-express.png",
height: 200.0,
width: 300.0,
),
),
onPressed: onPressed,
),
),
);
}
}
然后像这样使用它
floatingActionButton: MyFloatingActionButton(
onPressed: () {
var Router = new MaterialPageRoute(builder: (BuildContext) {
return new FirstPage();
});
Navigator.of(context).push(Router);
},
),