Flutter 颤振:IconButton未接收onPressed函数()
我在下面创建了一个自定义布局:Flutter 颤振:IconButton未接收onPressed函数(),flutter,Flutter,我在下面创建了一个自定义布局: class CustomLayout extends StatelessWidget { final Widget leading; final String title; final Widget body; final List<Widget> bottomBar; const CustomLayout({ Key key,Widget leading, String title, Widget body, this.
class CustomLayout extends StatelessWidget {
final Widget leading;
final String title;
final Widget body;
final List<Widget> bottomBar;
const CustomLayout({
Key key,Widget leading, String title, Widget body, this.bottomBar
}):leading = leading ?? const SizedBox(height: 0,),
title = title ?? "",
body = body ?? const SizedBox(),
super(key: key)
;
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Stack(
alignment: Alignment.topLeft,
children: [
/// Background
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/background.jpg"),
fit: BoxFit.cover
)
),
),
/// custom
Column(
children: [
/// SimpleAppbar
SizedBox(
height: (title == "")? 0 : 50,
width: MediaQuery.of(context).size.width,
child: Stack(
alignment: Alignment.centerLeft,
children: [
Center(
child: Text(
title,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 24
),
),
),
Container(
padding: const EdgeInsets.only(left:4.0,top: 4.0),
child: leading,
),
],
),
),
/// body
Expanded(child: body),
/// bottomBar
SizedBox(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: bottomBar == null ? [SizedBox()]:bottomBar,
),
)
],
)
],
),
),
);
}
}
无法按下前导参数,我搜索并得到了一些提示,这些提示可能是我的堆栈,但我已经检查了很多次。底部栏和主体参数仍然可以正常工作。救救我 通常,堆栈用于分层格式,因此您在自定义布局中使用了扩展到主体,这就是为什么它占据了整个屏幕的宽度和高度,并且位于主图标的顶部,这就是为什么图标从未被按下,所以我在您的布局中更改了一些内容请查看
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SampleApp(),
debugShowCheckedModeBanner: false,
);
}
}
class SampleApp extends StatefulWidget {
@override
_SampleAppState createState() => _SampleAppState();
}
class _SampleAppState extends State<SampleApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your app'),
),
body: Container(
child: CustomLayout(
leading: IconButton(
icon: Icon(
Icons.arrow_back,
color: Colors.black,
size: 20,
),
onPressed: () {
print('This is the tap');
},
),
body: Text('This is your body') // my code
),
));
}
}
class CustomLayout extends StatelessWidget {
final Widget leading;
final String title;
final Widget body;
final List<Widget> bottomBar;
const CustomLayout(
{Key key, Widget leading, String title, Widget body, this.bottomBar})
: leading = leading ??
const SizedBox(
height: 0,
),
title = title ?? "",
body = body ?? const SizedBox(),
super(key: key);
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Container(
child: Stack(
alignment: Alignment.topLeft,
children: [
/// Background
Container(
decoration: BoxDecoration(),
),
/// custom
Column(
children: [
/// SimpleAppbar
SizedBox(
height: (title == "") ? 0 : 50,
width: MediaQuery.of(context).size.width,
child: Stack(
alignment: Alignment.centerLeft,
children: [
Center(
child: Text(
title,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 24),
),
),
],
),
),
/// body
Expanded(child: body),
/// bottomBar
SizedBox(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: bottomBar == null ? [SizedBox()] : bottomBar,
),
),
],
),
Container(
padding: const EdgeInsets.only(left: 4.0, top: 5.0),
child: leading,
),
],
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:SampleApp(),
debugShowCheckedModeBanner:false,
);
}
}
类SampleApp扩展StatefulWidget{
@凌驾
_SampleAppState createState();
}
类_SampleAppState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“您的应用程序”),
),
主体:容器(
子:自定义布局(
领先:IconButton(
图标:图标(
Icons.arrow_back,
颜色:颜色,黑色,
尺码:20,
),
已按下:(){
打印(“这是水龙头”);
},
),
body:Text('这是你的body')//我的代码
),
));
}
}
类CustomLayout扩展了无状态小部件{
最终引导;
最后的字符串标题;
最终窗口小部件主体;
最终列表底部栏;
常量自定义布局(
{键,小部件前导,字符串标题,小部件主体,this.bottomBar})
:领先=领先??
常量大小框(
高度:0,,
),
标题=标题??“,
body=body??const SizedBox(),
超级(键:键);
@凌驾
小部件构建(构建上下文){
返回安全区(
孩子:脚手架(
主体:容器(
子:堆栈(
对齐:alignment.topLeft,
儿童:[
///背景
容器(
装饰:BoxDecoration(),
),
///习俗
纵队(
儿童:[
///SimpleAppbar
大小盒子(
高度:(标题==”)?0:50,
宽度:MediaQuery.of(context).size.width,
子:堆栈(
对齐:alignment.centerLeft,
儿童:[
居中(
子:文本(
标题
样式:TextStyle(
颜色:颜色,白色,
fontWeight:fontWeight.bold,
尺寸:24),,
),
),
],
),
),
///身体
扩展(子:体),
///底线
大小盒子(
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceAround,
子项:bottomBar==null?[SizedBox()]:bottomBar,
),
),
],
),
容器(
填充:仅限常量边集(左:4.0,顶:5.0),
孩子:领导,
),
],
),
),
),
);
}
}
让我知道它是否有效
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SampleApp(),
debugShowCheckedModeBanner: false,
);
}
}
class SampleApp extends StatefulWidget {
@override
_SampleAppState createState() => _SampleAppState();
}
class _SampleAppState extends State<SampleApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Your app'),
),
body: Container(
child: CustomLayout(
leading: IconButton(
icon: Icon(
Icons.arrow_back,
color: Colors.black,
size: 20,
),
onPressed: () {
print('This is the tap');
},
),
body: Text('This is your body') // my code
),
));
}
}
class CustomLayout extends StatelessWidget {
final Widget leading;
final String title;
final Widget body;
final List<Widget> bottomBar;
const CustomLayout(
{Key key, Widget leading, String title, Widget body, this.bottomBar})
: leading = leading ??
const SizedBox(
height: 0,
),
title = title ?? "",
body = body ?? const SizedBox(),
super(key: key);
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Container(
child: Stack(
alignment: Alignment.topLeft,
children: [
/// Background
Container(
decoration: BoxDecoration(),
),
/// custom
Column(
children: [
/// SimpleAppbar
SizedBox(
height: (title == "") ? 0 : 50,
width: MediaQuery.of(context).size.width,
child: Stack(
alignment: Alignment.centerLeft,
children: [
Center(
child: Text(
title,
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 24),
),
),
],
),
),
/// body
Expanded(child: body),
/// bottomBar
SizedBox(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: bottomBar == null ? [SizedBox()] : bottomBar,
),
),
],
),
Container(
padding: const EdgeInsets.only(left: 4.0, top: 5.0),
child: leading,
),
],
),
),
),
);
}
}