Home Php C# Sql C C++ Javascript Python Java Go Android Git Linux Asp.net Django .net Node.js Ios Xcode Cocoa Iphone Mysql Tomcat Mongodb Bash Objective-c Scala Visual-studio Apache Elasticsearch Jar Eclipse Jquery Ruby-on-rails Ruby Rubygems Android-studio Spring Lua Sqlite Emacs Ubuntu Perl Docker Swift Amazon-web-services Svn Html Ajax Xml Java-ee Maven Intellij-idea Rvm Macos Unix Css Ipad Postgresql Css3 Json Windows-server Vue.js Typescript Oracle Hibernate Internet-explorer Github Tensorflow Laravel Symfony Redis Html5 Google-app-engine Nginx Firefox Sqlalchemy Lucene Erlang Flask Vim Solr Webview Facebook Zend-framework Virtualenv Nosql Ide Twitter Safari Flutter Bundle Phonegap Centos Sphinx Actionscript Tornado Register | Login | Edit Tags | New Questions | 繁体 | 简体


10 questions online user: 60

63
votes
answers
8 views
+10

Flutter: Trying to bottom-center an item in a Column, but it keeps left-aligning

I'm trying to bottom-center a widget at the bottom of a Column, but it keeps aligning to the left.

return new Column(
  new Stack(
    new Positioned(
      bottom: 0.0, 
      new Center(
        new Container(),
      ),
    ),
  ),
); 

The existence of the Positioned forces the Container to the left, instead of centering. Removing the Positioned, however, puts the Container in the middle-center.

up vote 43 down vote accepted favorite
沙发
+430
+50

Align is the way to go is you have only one child.

If you have more, consider doing something like this :

return new Column(
  crossAxisAlignment: CrossAxisAlignment.center,
  mainAxisSize: MainAxisSize.max,
  mainAxisAlignment: MainAxisAlignment.end,
  children: <Widget>[
      //your elements here
  ],
);
+120

1) You can use an Align widget, with FractionalOffset.bottomCenter.

2) You can also set left: 0.0 and right: 0.0 in the Positioned.

+80

I have used this approach,

What i want is, A layout always in bottom but whenever Keyboard pops-up that layout also comes over it

enter image description here

body: Container(
        color: Colors.amber,
        height: double.maxFinite,
        child: new Stack(
          //alignment:new Alignment(x, y)
          children: <Widget>[
            new Positioned(
              child: myWidget(context, data.iconName, Colors.red),
            ),
            new Positioned(
              child: new Align(alignment: FractionalOffset.bottomCenter,child: myWidget(context, data.iconName, Colors.green)),
            )
          ],
        ),
      ),
0

Wrap your Container in SingleChildScrollView() widget. Then it will not come above when keyboard popus up.

0
votes
answers
32 views
+10

我可以在Android上啓動聯繫人應用程序,並實際獲得選定的聯繫人手機號碼撲動?

0

有沒有辦法啓動聯繫人應用程序,並獲取任何撲翼應用程序後選擇的聯繫人號碼。我可以在Android上啓動聯繫人應用程序,並實際獲得選定的聯繫人手機號碼撲動?

我已經成功地只能使用urlurl_launcher包,但無法找到一個方法來返回選定聯繫人回到應用程序啓動,

請可有人告訴我,我們如何能在撲手機做到這一點應用。

非常感謝, 馬希

+0

請記住,如果您願意,可以進行android/IOS調用。 – Darky

沙发
0
1

這是不可能的url_launcher包。已有一個contact_picker package可用於撲動,允許您選擇一個聯繫人。

希望有幫助!

+0

謝謝@Hemanth Raj你是一個明星隊友,我會嘗試這個包,讓你知道如果這個作品非常感謝你。 – Mahi

+0

啊,沒關係!只要接受答案,如果它有幫助。 –

0
votes
answers
24 views
+10

Flutter remove all routes

I want to develop a logout button that will send me to the log in route and remove all other routes from the Navigator. The documentation doesn't seem to explain how to make a RoutePredicate or have any sort of removeAll function.

87
votes
answers
17 views
+10

InkWell not showing ripple effect

Tapping the container triggers the onTap() handler but does not show any ink splash effect.

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new InkWell(
          onTap: (){print("tapped");},
          child: new Container(
            width: 100.0,
            height: 100.0,
            color: Colors.orange,
          ),
        ),
      ),
    );
  }
}

I tried putting the InkWell inside the Container as well but in vain.

up vote 53 down vote accepted favorite
沙发
+530
+50

I think adding a color to the container is covering over the ink effect

https://docs.flutter.io/flutter/material/InkWell/InkWell.html

This code seems to work

  body: new Center(
    child: new Container(
      child: new Material(
        child: new InkWell(
          onTap: (){print("tapped");},
          child: new Container(
            width: 100.0,
            height: 100.0,
          ),
        ),
        color: Colors.transparent,
      ),
      color: Colors.orange,
    ),
  ),

just click the middle square.

Edit: I found the bugreport. https://github.com/flutter/flutter/issues/3782

This is actually as expected, though we should update the docs to make it clearer.

What's going on is that the Material spec says that the splashes are actually ink on the Material. So when we splash, what we do is we literally have the Material widget do the splash. If you have something on top of the Material, we splash under it, and you can't see it.

I have wanted to add a "MaterialImage" widget which conceptually prints its image into the Material as well, so that then the splashes would be over the image. We could have a MaterialDecoration which does something similar for a Decoration. Or we could have Material itself take a decoration. Right now it takes a color, but we could extend that to taking a whole decoration. It's not clear whether it's really material-spec-compatible to have a material with a gradient, though, so I'm not sure whether we should do that.

On the short run, if you just need a workaround, you can put a Material on top of the container, with the material set to use the "transparency" type, and then put the ink well inside that.

--hixie

您可以將顏色放在材料本身上並省略容器。 - Herohtar 2月5日4:13

當CopsOnRoad發布他的答案時,我有點想通了。 - user1462442 2月5日12:19

你救了我一個輕微的頭痛 - Koen Van Looveren 4月21日9:04

@KoenVanLooveren嘗試CopsOnRoad的答案。我沒有編輯我的解決方案,因為CopsOnRoad的解決方案非常優雅 - user1462442 4月21日14:31

這就是我在一些重構之後使用的:D謝謝 - Koen Van Looveren 4月22日7:18

+300

Step 1. Use Material as parent for your InkWell, otherwise it won't show ripple effects.

Step 2. Give color to the Material widget, this is going to be the color you were using in your Container.

Step 3. Don't forget to use onTap() even if you don't have anything to handle.

Simple example:

Widget myWidget() {
  return Material( // needed
    color: Colors.orange,
    child: InkWell(
      onTap: () {}, // needed
      child: Container(width: 100.0, height: 100.0),
    ),
  );
}

源確認步驟3:github.com/flutter/flutter/blob / ...在每個處理程序中,他們檢查回調是否可用,否則沒有任何反應。 - 1819年11月19日晚上14:24

+20

InkWell() will never show the ripple effect until you add the

onTap : () {} 

parameter inside the InkWell as it starts listening to your taps only when you specify this parameter.

你自己嘗試過嗎? - CopsOnRoad 18年10月2日10:39

是的,我做了,它是真的。檢查源:github.com/flutter/flutter/blob / ...在每個處理程序中,他們檢查回調是否可用,否則沒有任何反應。 - 1819年11月19日晚上14:23

@Nightking,當你把你的代碼放在OP代碼中時,它真的顯示出漣漪效應嗎? - CopsOnRoad 18年12月13日18:49

+20

I ran into this same problem trying to create an alternating color of InkWell's in a ListView. In that particular case, there's a simple solution: wrap the alternates in a Container that uses a mostly transparent tint/brightness change -- the InkWell touch animation will still be visible beneath it. No Material needed. Note there are other issues when trying to work around this with a Materal -- e.g., it will override a DefaultTextStyle you're using with the default (it installs an AnimatedDefaultTextStyle) which is a huge pain.

0

A better way is to use the Ink widget instead of any other widget.

Instead of defining color inside container you can define it in Ink widget itself.

Below code will work.

Ink(
  color: Colors.orange,
  child: InkWell(
    child: Container(
      width: 100,
      height: 100,
    ),
    onTap: () {},
  ),
)
41
votes
answers
13 views
+10

How can I layout widgets based on the size of the parent?

Lets say you have a parent widget that might have variable size.

For example:

var container = new Container(
   height: 200.0, // Imagine this might change
   width: 200.0, // Imagine this might change
   // Imagine content in this container will 
   // depend on the parent container
   child: new Container(), 
);

And maybe you want to have the child of the parent container to render something different based on what the size that it's given.

Think of responsive design breakpoints, if the width is over X use this layout, if the width is under X use that layout.

What's the best way to do this in Flutter?

up vote 41 down vote accepted favorite
沙发
+410
+50

You will want to use the LayoutBuilder widget which will build at layout time and provides the parent widget's constraints.

The LayoutBuilder takes in a build() function which has the the standard BuildContext along with the BoxConstraints as parameters that can be used to help dynamically render widgets based on size.

Let's build a simple example of widget that renders "LARGE" if the parent width is greater than 200px and "SMALL" if the parent width is less or equal to that.

var container = new Container(
  // Toggling width from 100 to 300 will change what is rendered
  // in the child container
  width: 100.0,
  // width: 300.0
  child: new LayoutBuilder(
    builder: (BuildContext context, BoxConstraints constraints) {
      if(constraints.maxWidth > 200.0) {
        return new Text('BIG');
      } else {
        return new Text('SMALL');
      }
    }
  ),
);

謝謝,我一直在努力根據父窗口小部件的大小來佈局窗口小部件,首先我嘗試使用屏幕高度減去appbar高度和底部欄高度,這是愚蠢的,不適用於所有物理手機。 - Daniel Dai 3月28日1:59

65
votes
answers
17 views
+10

How can I dismiss an AlertDialog on a FlatButton click?

I have the following AlertDialog.

showDialog(
            context: context,
            child: new AlertDialog(
              title: const Text("Location disabled"),
              content: const Text(
                  """
Location is disabled on this device. Please enable it and try again.
                  """),
              actions: [
                new FlatButton(
                  child: const Text("Ok"),
                  onPressed: _dismissDialog,
                ),
              ],
            ),
        );

How can I make _dismissDialog() dismiss said AlertDialog?

up vote 46 down vote accepted favorite
沙发
+460
+50

Navigator.pop() should do the trick. You can also use that to return the result of the dialog (if it presented the user with choices)

謝謝,這很有效。調用Navigator.pop()會按預期關閉對話框。我當前的onPressed如下:onPressed :()=> Navigator.pop(context), - Gustash 2017年5月24日13:52

完美地工作 - Muhammad Faizan 18年12月27日5:02

+130
Navigator.of(context, rootNavigator: true).pop('dialog')

worked with me.

接受的答案導致我的整個頁面消失,這是隱藏對話的正確答案 - user969068 Feb 17 at 11:12

這是一個更好的方法來關閉對話框,我嘗試上面的解決方案,但它正在彈出我的其他視圖。 - Farhana 3月1日6:29

+40

You can use any of the following:

Navigator.of(context).pop();
Navigator.pop(context);
+20
Navigator.pop(_)

worked for me, but the Flutter Team's gallery contains an example using:

Navigator.of(context, rootNavigator: true).pop()

which also works, and I am tempted to follow their lead.

0

The accepted answer states how to dismiss a dialog using the Navigator Class. To dismiss a dialog without using Navigator you can set the onPressed event of the button to the following:

setState((){
  thisAlertDialog = null; 
});

In case the code above is not self-explanatory it is basically setting the Parent AlertDialog of the FlatButton to null, thus dismissing it.

28
votes
answers
17 views
+10

NativeScript Vs Flutter [closed]

I have started working on Native Script with Angular. The best part is the code sharing technique which i can use for Web & Mobile app both.

But one thing which i want ask here is that Angular is a framework which is developed by Google and Google also provides another framework for Native mobile app development i.e. Flutter

Why Google hasn't used the same Angular framework which Telerik has implemented with Native Script. I am sure that there must be some reason for it. I want to understand the reason.

If i can develop Web & Mobile app with a common skill i.e. Angular, then why i should learn/choose a new framework like Flutter.

I am not sure whether is it the right place where i should ask this question or not. If not, then please help me to find the right place.

沙发
+170

Flutter is not the main deal here, the real thing is Skia.

skia is a 2d rendering engine which allows the user to run apps in high performance, this way instead of using html(chrome web engine) or using the native look and feel(nativescript) skia allows us to draw on each pixel of the screen, which allows us to write games in flutter.

Google real intentions as they reviled in flutter Live 2018 are to create a coherent UI between all platforms:

  • desktop
  • web (hummingbird)
  • mobile (flutter)

all of those frameworks use skia for UI and the respective framework for interacting with the OS (browser, desktop, mobile), because skia is a rendering engine and we control every pixel we can assure that a widget will look the same on all type of skia frameworks.

On the other hand nativescript use a node engine which they load on the platform, and each time we try to use a component we call the system engine to place a component on the screen with the native look and feel, this allows us to use features straight from the virtual machine which runs java/swift, this is why in nativescript you can use android.[ANDROID_JAVA_CLASS] or ios.[SWIFT_CLASS].

nativescript main invention is around using native classes from inside nodejs engine which they are loading on runtime in-order to execute the specific platform code. When you are transforming nativescript mobile code to a PWA there is no need for this node engine because the browser has it's own engine this way we can remove unnecessary code from the PWA.

NOTE: nativescript is not an angular framework for mobile development its a nodejs framework for mobile development and Angular/vuejs are just frameworks which runs on top of nativescript core logic. Thats why if you want you can even write a react plugin for nativescript.

板凳
+110

In Nativescript, Code is not compiled to native code , it runs on separate thread that communites code with native component. The runtimes enable you to call APIs in the Android and iOS frameworks using JavaScript code. To do that they use JavaScript Virtual Machines - Google’s V8 for Android and WebKit’s JavaScriptCore implementation distributed with iOS 7.0+. While Flutter compiles code to ARM C/C++ library that is consumed by native components so may perform better.

I didn't get a chance to compare performance for the two, as for NativeScript you write code in Typescript/Javascript while Dart is used for Flutter and I'm very new to Dart.

NativeScript is an open source that Angular has also listed in resources and there is very good community for NativeScript now. If you are interested to list pro and cons of different frameworks, here is one very good article.

Note :Test NativeScript apps online with a code editor and run on your device. You can use either Angular or Pure JavaScript orTypeScript or Vue.js flavor to build the app.

On a Side note :Google has unveiled Flutter at the 2015 Dart developer summit, and NativeScript was backed by Progress from 2014. Maybe Google wanted to give developers others options to explore native applications with other frameworks.

If you look for available commands for Flutter, there is one command called fuchsia_reload to reload the operating new operating system Fuchsia that Google is working on, so it could be possible they want to provide early support for that.

UPDATE: If we want to share the same codebase for web and phone, NativeScript is an obvious choice. The Angular and NativeScript teams teamed up to create nativescript-schematics, a schematic collection that enables you to build both web and mobile apps from a single project.

You can install it using

npm i -g @nativescript/schematics

And even you can migrate from existing project to code sharing projects as described here.

地板
-10

a) Angular is not made by google.

instead

Angular is built by a team of engineers who share a passion for making web development feel effortless. We believe that writing beautiful apps should be joyful and fun. We're building a platform for the future.

source: https://angular.io/about?group=Angular

b) That's the thing about frameworks they all keep appearing and disappearing. Learn and use one till the next one comes along.

這個答案與{NS}或Flutter無關。:) - Narendra 4月23日22:02

78
votes
answers
18 views
+10

Flutter: Run method on Widget build complete

I would like to be able to run functions once a Widget has finished building/loading but I am unsure how. My current use case is to check if a user is authenticated and if not, redirect to a login view. I do not want to check before and push either the login view or the main view, it needs to happen after the main view has loaded. Is there anything I can use to do this?

沙发
+530

You could use

https://github.com/slightfoot/flutter_after_layout

which executes a function only one time after the layout is completed. Or just look at its implementation and add it to your code :-)

Which is basically

  void initState() {
    super.initState();
    WidgetsBinding.instance
        .addPostFrameCallback((_) => yourFunction(context));
  }

謝謝@thomas你的ans對我很有幫助。我在兩天之前就開始研究這個問題了 再次感謝你非常好的工作 - 拉文德拉Bhanderi 18年6月15日在10:45

請參閱@ anmol.majhail答案:WidgetsBinding.instance.addPostFrameCallback((_)=> yourFunciton(context)); 不再工作 - Pablo Insua 3月26日12:34

板凳
+190

UPDATE:

The Code Mentioned is no Longer Working now :

Not Working:

WidgetsBinding.instance
        .addPostFrameCallback((_) => yourFunction(context));

Working Code:

import 'package:flutter/scheduler.dart';

SchedulerBinding.instance.addPostFrameCallback((_) => yourFunction(context));

第二個不再有效。NoSuchMethodError(NoSuchMethodError:方法'addPostFrameCallback'在null上調用。接收者:null - Oliver Dixon Jun 2 at 17:13

對我來說都是工作。 - 雨果帕索斯6月5日19:55

地板
+40

If you are looking for ReactNative's componentDidMount equivalent, Flutter has it. It's not that simple but it's working just the same way. In Flutter, Widgets do not handle their events directly. Instead they use their State object to do that.

class MyWidget extends StatefulWidget{

  @override
  State<StatefulWidget> createState() => MyState(this);

  Widget build(BuildContext context){...} //build layout here

  void onLoad(BuildContext context){...} //callback when layout build done
}

class MyState extends State<MyWidget>{

  MyWidget widget;

  MyState(this.widget);

  @override
  Widget build(BuildContext context) => widget.build(context);

  @override
  void initState() => widget.onLoad(context);
}

State.initState immediately will be called once upon screen has finishes rendering the layout. And will never again be called even on hot reload if you're in debug mode, until explicitly reaches time to do so.

從我的示例中,您可以使用StatefulWidget類來處理它的State對象,就像StatelessWidget一樣,但我強烈不推薦它。我還沒有發現任何問題,但請盡量在State對像中實現一切 - jerinho 2月27日13:45

4楼
+20

Flutter 1.2 - dart 2.2

According with the official guidelines and sources if you want to be certain that also the last frame of your layout was drawned you can write for example:

import 'package:flutter/scheduler.dart';

void initState() {
   super.initState();
   if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks) {
        SchedulerBinding.instance.addPostFrameCallback((_) => yourFunction(context));
   }
}

對我來說,這不起作用,因為在initState()時間我得到SchedulerPhase與SchedulerPhase.idle值...它實際工作的是在build()中添加該檢查 - Alessio 4月24日6:38

55
votes
answers
14 views
+10

How to run code after some delay in Flutter?

I'd like to execute a function after a certain delay after my Widget is built. What's the idiomatic way of doing this in Flutter?

What I'm trying to achieve: I'd like to start with a default FlutterLogo Widget and then change its style property after some duration.

up vote 32 down vote accepted favorite
沙发
+320
+50

Figured it out

+230

You can use Future.delayed to run your code after some time. e.g.:

Future.delayed(const Duration(milliseconds: 500), () {

// Here you can write your code

  setState(() {
    // Here you can write your code for open new view
  });

});

In setState function, you can write a code which is related to app UI e.g. refresh screen data, change label text, etc.

72
votes
answers
8 views
+10

Navigator operation requested with a context that does not include a Navigator

I'm trying to start a new screen within an onTap but I get the following error:

Navigator operation requested with a context that does not include a Navigator.

The code I am using to navigate is:

onTap: () { Navigator.of(context).pushNamed('/settings'); },

I have set up a route in my app as follows:

routes: <String, WidgetBuilder>{
    '/settings': (BuildContext context) => new SettingsPage(),
},

I've tried to copy the code using the stocks sample application. I've looked at the Navigator and Route documentation and can't figure out how the context can be made to include a Navigator. The context being used in the onTap is referenced from the parameter passed into the build method:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

SettingsPage is a class as follows:

class SettingsPage extends Navigator {

Widget buildAppBar(BuildContext context) {
  return new AppBar(
    title: const Text('Settings')
  );
}

@override
Widget build(BuildContext context) {
  return new Scaffold(
    appBar: buildAppBar(context),
  );
 }
}
up vote 57 down vote accepted favorite
沙发
+570
+50

TLDR: Wrap the widget which needs to access to Navigator into a Builder or extract that sub-tree into a class. And use the new BuildContext to access Navigator.


This error is unrelated to the destination. It happens because you used a context that doesn't contain a Navigator instance as parent.

How do I create a Navigator instance then ?

This is usually done by inserting in your widget tree a MaterialApp or WidgetApp. Although you can do it manually by using Navigator directly but less recommended. Then, all children of such widget can access NavigatorState using Navigator.of(context).

Wait, I already have a MaterialApp/WidgetApp !

That's most likely the case. But this error can still happens when you use a context that is a parent of MaterialApp/WidgetApp.

This happens because when you do Navigator.of(context), it will start from the widget associated to the context used. And then go upward in the widget tree until it either find a Navigator or there's no more widget.

In the first case, everything is fine. In the second, it throws a

Navigator operation requested with a context that does not include a Navigator.

So, how do I fix it ?

First, let's reproduce this error :

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Center(
        child: RaisedButton(
          child: Text("Foo"),
          onPressed: () => Navigator.pushNamed(context, "/"),
        ),
      ),
    );
  }
}

This example creates a button that attempts to go to '/' on click but will instead throw an exception.

Notice here that in the

  onPressed: () => Navigator.pushNamed(context, "/"),

we used context passed by to build of MyApp.

The problem is, MyApp is actually a parent of MaterialApp. As it's the widget who instantiate MaterialApp! Therefore MyApp's BuildContext doesn't have a MaterialApp as parent!

To solve this problem, we need to use a different context.

In this situation, the easiest solution is to introduce a new widget as child of MaterialApp. And then use that widget's context to do the Navigator call.

There are a few ways to achieve this. You can extract home into a custom class :

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHome()
    );
  }
}

class MyHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
        child: RaisedButton(
          child: Text("Foo"),
          onPressed: () => Navigator.pushNamed(context, "/"),
        ),
      );
  }
}

Or you can use Builder :

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Builder(
        builder: (context) => Center(
              child: RaisedButton(
                child: Text("Foo"),
                onPressed: () => Navigator.pushNamed(context, "/"),
              ),
            ),
      ),
    );
  }
}

生成器解決了我的問題 - 在10月12日18:55 atich

感謝您對上下文問題的簡明扼要。我嘗試使用Builder並在MaterialApp下將我的自定義小部件用於家庭。為了利用Dart的BuilderContext詞法範圍,我意識到在MaterialApp下使用自定義小部件的方法更容易編碼。 - 於申8月3日'18在19:09

驚人的解釋!謝謝:) - Rohan Taneja 18年8月14日11:51

這應該是公認的解決方案,因為它實際上解釋了什麼是錯誤的。 - 克雷格·拉賓茲18年11月12日12:58

Builder是正確的解決方案 - kumail於18年12月26日9:07

+100

I set up this simple example for routing in a flutter app:

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new MyHomePage(),
      routes: <String, WidgetBuilder>{
        '/settings': (BuildContext context) => new SettingsPage(),
      },
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('TestProject'),
      ),
      body: new Center(
        child: new FlatButton(
          child: const Text('Go to Settings'),
          onPressed: () => Navigator.of(context).pushNamed('/settings')
        )
      )
    );
  }
}

class SettingsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('SettingsPage'),
        ),
        body: new Center(
            child: new Text('Settings')
        )
    );
  }
}

Note, that the SettingsPage extends StatelessWidget and not Navigator. I'm not able to reproduce your error.

Does this example help you in building your app? Let me know if I can help you with anything else.

是的,這有幫助!我已經設置了home:new Scaffold(appBar:新的AppBar(標題:新文本('GroupUp'),),在MaterialApp的構造函數中,而不是創建另一個Widget。(至少我認為這是問題) - juliusspencer May 16 '17在16:04

你在哪裡有你的onTap比? - Rainer Wittmann於2017年5月16日16:17

也許你正在遇到github.com/flutter/flutter/issues/15919 - Kira May 18 '18 at 5:08

+30

A complete and tested solution:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:my-app/view/main-view.dart';

class SplashView extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home: Builder(
          builder: (context) => new _SplashContent(),
        ),
        routes: <String, WidgetBuilder>{
          '/main': (BuildContext context) => new MainView()}
    );
  }
}

class _SplashContent extends StatefulWidget{

  @override
  _SplashContentState createState() => new _SplashContentState();
}

class _SplashContentState extends State<_SplashContent>
    with SingleTickerProviderStateMixin {

  var _iconAnimationController;
  var _iconAnimation;

  startTimeout() async {
    var duration = const Duration(seconds: 3);
    return new Timer(duration, handleTimeout);
  }

  void handleTimeout() {
    Navigator.pushReplacementNamed(context, "/main");
  }

  @override
  void initState() {
    super.initState();

    _iconAnimationController = new AnimationController(
        vsync: this, duration: new Duration(milliseconds: 2000));

    _iconAnimation = new CurvedAnimation(
        parent: _iconAnimationController, curve: Curves.easeIn);
    _iconAnimation.addListener(() => this.setState(() {}));

    _iconAnimationController.forward();

    startTimeout();
  }

  @override
  Widget build(BuildContext context) {
    return new Center(
        child: new Image(
          image: new AssetImage("images/logo.png"),
          width: _iconAnimation.value * 100,
          height: _iconAnimation.value * 100,
        )
    );
  }
}
+20

Hy guys, i have the same problem. This is occur for me. The solution what i found is very simple. Only what i did is in a simple code:

void main() {
  runApp(MaterialApp(
    home: YOURAPP() ,
    ),
  );
}

I hope was useful.