flutter - 我将InkWell小部件连续放置,但无法正常工作

标签 flutter dart flutter-layout

我将Inkwell小部件放在一行中,并使每个小部件都展开。这是为了使两者都占据垂直屏幕的一半。

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(    
      debugShowCheckedModeBanner: false,
      home: Scaffold(    
        body: Container(
          child: SafeArea(
            child: Column( 
              children: <Widget>[
                Expanded(
                  child: Row(     
                    children: <Widget>[ 
                      Expanded(
                        child: Container (
                          child: InkWell(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(builder: (context) => MyApp2()),
                              );
                            },
                          )
                        )
                      ),
                      Expanded (
                        child: Container(
                          color: Colors.grey
                        )
                      ),         
                    ],
                  )
                )
              ],
            )
          )
        )
      )
    );
  }

}

但是由于某种原因,InkWell白色小部件无法打开页面“MyApp2”。如何解决此问题?

最佳答案

您需要将MaterialApp小部件拆分为另一个类,如图所示。

import 'package:flutter/material.dart';
import 'MyApp2.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            child: SafeArea(
                child: Column(
      children: <Widget>[
        Expanded(
            child: Row(
          children: <Widget>[
            Expanded(child: Container(child: InkWell(
              onTap: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => MyApp2()),
                );
              },
            ))),
            Expanded(child: Container(color: Colors.grey)),
          ],
        ))
      ],
    ))));
  }
}

原因是您使用的是context的父级MaterialApp,上下文被传递给buildMyApp。结果,MyAppBuildContext没有以MaterialApp作为父代。
在您的情况下,由于MyApp需要MaterialApp作为父类才能在该类中进行导航,因此您可以将Scaffold提取到另一个类,并将MaterialApp保留在MyApp中。

关于flutter - 我将InkWell小部件连续放置,但无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60024626/

相关文章:

android - 断言失败 : line 6075 pos 12: 'child == _child' : is not true

button - 当一个按钮被按下时,让所有其他小部件平面按钮改变它们的颜色

javascript - 如何在 Dart 中运行 JavaScript?

flutter - 未处理的异常 : PlatformException(error, 无效选择开始 : 24, null,java.lang.IndexOutOfBoundsException:无效选择开始:24

flutter - "threads"在 Dart 中使用 Flutter for web

flutter - 如何在flutter中向GridView添加Header?

flutter - Flutter 的绝对位置

dart - 在 CupertinoNavigationBar 中,我如何在前导中显示除后退按钮之外的按钮?

firebase - Firestore在Flutter上查询的地方没有 “notEqualTo”条件

android - 如何在flutter中设置FloatingActionButton的背景透明?