gpt4 book ai didi

Flutter:IconButton 没有收到 onPressed 函数()

转载 作者:行者123 更新时间:2023-12-04 15:12:14 26 4
gpt4 key购买 nike

我在下面创建了一个自定义布局:

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,
),
)
],
)
],
),
),
);
}
}

并使用这个布局:

CustomLayout(
leading: IconButton(
icon: Icon(
Icons.arrow_back,
color: Colors.white,
),
onPressed: (){
Navigator.of(context).pop();
},
),
body: // my code
);

无法按下前导参数,我搜索并得到了一些提示,这些提示可能是我的堆栈,但我已经检查了很多次。底栏和主体参数仍然可以正常工作。帮我!............................................... ..................................................... ..................................................... ..................................................... ..................................................... .....................

最佳答案

通常,堆栈用于分层格式,因此您在自定义布局中使用了扩展到正文的内容,这就是为什么它会占据整个屏幕的宽度和高度,并且它位于前导图标的顶部,这就是图标从不显示的原因被按下,所以我改变了你布局中的一些东西,看看吧。

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,
),
],
),
),
),
);
}
}

如果有用请告诉我

关于Flutter:IconButton 没有收到 onPressed 函数(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64998822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com