首页 > 解决方案 > 颤振 - 不能让两者同时工作

问题描述

我可以制作一个计数器应用程序。我可以导航到新屏幕。

我想要的是,在主视图和设置按钮中创建一个计数器。内部设置按钮 -> 更改屏幕视图和计数的计数器大小。并计算大小。这是不可能的,还是在错误?

但我不能同时做到:(

这是我的代码,我可以更改视图,但无法更改文本。

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Named Routes Demo',
    // Start the app with the "/" named route. In this case, the app starts
    // on the FirstScreen widget.
    initialRoute: '/',
    routes: {
      // When navigating to the "/" route, build the FirstScreen widget.
      '/': (context) => FirstScreen(),
      // When navigating to the "/second" route, build the SecondScreen widget.
      '/second': (context) => SecondScreen(),
    },
  ));
}

class FirstScreen extends StatelessWidget {

  String changetext = 'Change me !';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Screen'),
      ),
      body: Center(
        child: Container(
          child: Column(
            children: <Widget>[
              Text(changetext),
              RaisedButton(
                child: Text('Press Me To update Text !'),
                onPressed: (){
                  changetext = 'it has been changed';
                },
              ),
              RaisedButton(
                child: Text('Change View'),
                onPressed: (){
                  Navigator.pushNamed(context, '/second');
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            // Navigate back to the first screen by popping the current route
            // off the stack.
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}

有人可以帮我吗?

谢谢 :)

标签: flutter

解决方案


推荐阅读