ボタンをクリックしてフラッターで他のページに移動する方法は?[複製]

Aug 23 2020
Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        body: Container(
          padding: EdgeInsets.fromLTRB(10.0, 50.0, 10.0, 10.0),
          child: Column(
            children: [
              TextField(
                controller: user,
                decoration: InputDecoration(
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(20.0)),
                    hintText: "ENTER USER NAME"),
              ),
              SizedBox(
                height: 10.0,
              ),
              TextField(
                controller: pass,
                decoration: InputDecoration(
                    border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(20.0)),
                    hintText: "ENTER USER NAME"),
              ),
              FlatButton(
                  onPressed: () {
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) => Home()));
                  },
                  child: Text("NAVIGATOR"))
            ],
          ),
        ),
      ),
    );
  }

navigator.pushでエラーが発生する

「これは、プレス中に発生するエラーです」ジェスチャの処理中に次のアサーションがスローされました。

ナビゲーターを含まないコンテキストで要求されたナビゲーター操作。ナビゲーターからルートをプッシュまたはポップするために使用されるコンテキストは、ナビゲーターウィジェットの子孫であるウィジェットのコンテキストである必要があります。

回答

AmanVerma Aug 23 2020 at 14:33
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
  body: Container(
    padding: EdgeInsets.fromLTRB(10.0, 50.0, 10.0, 10.0),
    child: Column(
      children: [
        TextField(
          controller: user,
          decoration: InputDecoration(
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(20.0)),
              hintText: "ENTER USER NAME"),
        ),
        SizedBox(
          height: 10.0,
        ),
        TextField(
          controller: pass,
          decoration: InputDecoration(
              border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(20.0)),
              hintText: "ENTER USER NAME"),
        ),
        FlatButton(
            onPressed: () {
              Navigator.push(
                  context, MaterialPageRoute(builder: (context) => Home()));
            },
            child: Text("NAVIGATOR"))
      ],
    ),
  ),
)
);
}

プラットフォームに疑問を投げかけるときはいつでも、このようなコードを書いてください

また、のコードを提供してください Home()

これがHome()の変更されたコードです

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  HomeState createState() => HomeState();
}

class HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
     return Scaffold(
        body: Container(
          child: Column(
            children: [
              Text(
                "WELCOME",
                style: TextStyle(fontSize: 50.0),
              )
            ],
          ),
        ),
      );
  }
}