我是新手。我正在尝试创建自定义应用栏小部件,并将其导入页面。
但是我无法创建小部件。
`import 'package:flutter/material.dart';
class AppBar extends StatelessWidget{
@override
Widget build(BuildContext context){
return AppBar(
title: Text('Ordering'),
actions: <Widget>[
IconButton(
onPressed: _incrementCounter,
icon: Icon(Icons.add),
),
BadgeIconButton(
itemCount: _counter,
badgeColor: Color.fromRGBO(37, 134, 16, 1.0),
badgeTextColor: Colors.white,
icon: Icon(Icons.shopping_cart, size: 30.0,),
onPressed: () {}
),
],
);
} }'
答案 0 :(得分:4)
import 'package:flutter/material.dart';
class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {
CustomAppBar({Key key}) : preferredSize = Size.fromHeight(56.0), super(key: key);
@override
final Size preferredSize; // default is 56.0
@override
_CustomAppBarState createState() => _CustomAppBarState();
}
class _CustomAppBarState extends State<CustomAppBar>{
@override
Widget build(BuildContext context) {
return AppBar( title: Text("Sample App Bar") );
}
}
希望这会有所帮助
答案 1 :(得分:3)
finaldict = {22: [7, 9, 8]}
dict1 = {23: [7, 9, 8, 6, 12]}
答案 2 :(得分:1)
Widget build(BuildContext context) {
return new Scaffold(
appBar: setAppBar(),
body: new Container() // add rest of the UI
);
}
Widget setAppBar() {
return new AppBar(
//backgroundColor: Colors.blue,
//automaticallyImplyLeading: true
elevation: 0.0, // for elevation
titleSpacing: 0.0, // if you want remove title spacing with back button
title: UtilCommonWidget.addTextMedium('About US', Colors.white, 20.0, 1),
actions: <Widget>[
addAppBarActionWidgetProfile(icon, 30.0, 30.0, 15.0) // add your custom action widget
],//Action icon search as search icon, notification icon
leading: new Material( //Custom leading icon, such as back icon or other icon
color: Colors.transparent,
child: new InkWell(
onTap: () {
Navigator.of(context).pop();
},
splashColor: UniQueryColors.colorGradientEnd.withOpacity(.5),
child: new Container(
padding: const EdgeInsets.fromLTRB(12.0, 16.0, 16.0, 16.0),
child: UtilCommonWidget.addImage(Constant.iconBack, 19.0, 10.0))
),
)
);
}
希望,这会对您有所帮助。
答案 3 :(得分:0)
编辑为riftninja的答案:
import 'package:flutter/material.dart';
class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {
CustomAppBar({Key key, double height}) : preferredSize =
Size.fromHeight(height), super(key: key);
@override
//final Size preferredSize; // This didnot work for me.
Size get preferredSize => preferredSize; //This should work.
@override
_CustomAppBarState createState() => _CustomAppBarState();
}
class _CustomAppBarState extends State<CustomAppBar>{
@override
Widget build(BuildContext context) {
return AppBar( title: Text("Sample App Bar") );
}
}
这也适用于无状态小部件。
答案 4 :(得分:0)
AppBar实现类PreferredSizeWidget
,这意味着AppBar必须具有首选的大小。
preferredSize
变量是从PreferredSizeWidget中实现的,因此会被覆盖,您可以在此处设置您希望的高度。
class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {
CustomAppBar({Key key}) : preferredSize = Size.fromHeight(60.0), super(key: key);
@override
final Size preferredSize;
@override
_CustomAppBarState createState() => _CustomAppBarState();
}
class _CustomAppBarState extends State<CustomAppBar>{
@override
Widget build(BuildContext context) {
return AppBar(
title: Text('App Bar'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.add, color: Colors.white,),
),
],
);
}
}
并像这样使用
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: CustomAppBar()
);
}
答案 5 :(得分:0)
我使用自定义窗口小部件扩展了 AppBar 。 然后将我的参数传递给 super类。
class CustomAppBar extends AppBar {
CustomAppBar()
: super(
title: Text('MyApp'),
actions: [
IconButton(icon: Icon(Icons.search), onPressed: () {}),
],
);
}
答案 6 :(得分:0)
widget_appbar.dart
class WidgetAppBar extends StatelessWidget implements PreferredSizeWidget {
final Color? backgroundColor;
final Color? textIconColor;
final String? icon;
final String? title;
final double? height;
final List<Widget>? menuItem;
final bool hideBack;
WidgetAppBar({
this.backgroundColor = whiteColor,
this.textIconColor = headingColor,
this.icon,
this.title = '',
this.menuItem,
this.height: kToolbarHeight,
this.hideBack = false,
});
@override
Size get preferredSize => Size.fromHeight(height!);
@override
Widget build(BuildContext context) {
return AppBar(
actions: menuItem,
toolbarHeight: preferredSize.height,
iconTheme: IconThemeData(
color: textIconColor,
),
leading: hideBack
? Container()
: icon == null
? BackButton()
: IconButton(
icon: Image.asset(
icon!,
height: 18,
width: 18,
),
onPressed: () {
Navigator.pop(context, true);
},
),
title: Text(
title!,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: textIconColor,
),
),
backgroundColor: backgroundColor,
centerTitle: true,
);
}
}
如何使用?
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: WidgetAppBar(
icon: ic_back_black,
title: 'Toolbar Title',
),
body: SafeArea(
child: Container(),
),
);
}
答案 7 :(得分:0)
创建这个类。
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
final Widget child;
final double height;
CustomAppBar({
required this.child,
this.height = kToolbarHeight,
});
@override
Size get preferredSize => Size.fromHeight(height);
@override
Widget build(BuildContext context) {
return Container(
height: preferredSize.height,
color: Colors.red,
child: child,
);
}
}
用法:
Scaffold(
appBar: CustomAppBar(
height: 100,
child: Column(
children: [
FlutterLogo(size: 56),
SizedBox(height: 8),
Text('Flutter'),
],
),
),
)