Flutter:IconButton未接收到onPressed函数()。

3
我创建了以下自定义布局:
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
);

领先参数无法按下,我搜索了一些提示,发现应该是我的栈但是我已经检查了很多次。底部条和正文参数仍然可以正常工作。帮帮我!

1个回答

3

一般情况下,栈用于分层格式,因此您已将其扩展到自定义布局的body中,这就是为什么它占据了整个屏幕的宽度和高度,并且在领先图标的顶部,这就是为什么该图标永远不会被按下的原因,因此我已更改了您的布局,请检查。

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

请告诉我它是否有效


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接