简体   繁体   English

容器颤振中的调用方法

[英]call method in container flutter

I have class called A.dart in that I am usnign Interprogressbar as below我有一个叫做 A.dart 的类,因为我是下面的 Interprogressbar

new Container(
            margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
            child: IntervalProgressBar(
                direction: IntervalProgressDirection.horizontal,
                max: count,
                progress: count1,
                intervalSize: 2,
                size: Size(600, 10),
                highlightColor: Colors.pink,
                defaultColor: Colors.grey,
                intervalColor: Colors.transparent,
                intervalHighlightColor: Colors.transparent,
                reverse: false,
                radius: 0)),

Once the user clicks a button I am navigating to another class from A.dart to a class called B.dart.一旦用户单击一个按钮,我就会导航到另一个类,从 A.dart 到一个名为 B.dart 的类。 Once use completes process in class B.dart I ll pop the class B.dart.一旦使用在类 B.dart 中完成过程,我将弹出类 B.dart。 Again A.dart will be visible for the user. A.dart 再次对用户可见。 So my question us how can I restart the IntervalProgressBar with the updated value once user comes from B.dart to A,dart.所以我的问题是,一旦用户从 B.dart 转到 A,dart,我该如何使用更新后的值重新启动 IntervalProgressBar。

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
You can await Navigator.push then call setState您可以await Navigator.push然后调用setState

void _gotoB() async {
    String parameter = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => RouteB()),
    );

    setState(() {
      count1 = int.tryParse(parameter);
    });
  }

...  
  
RaisedButton(
            child: Text('Go back'),
            onPressed: () {
              Navigator.pop(context, _textEditingController.text);
            },
          ),  

working demo工作演示

在此处输入图片说明

full code完整代码

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int count = 10;
  int count1 = 3;

  void _gotoB() async {
    String parameter = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => RouteB()),
    );

    setState(() {
      count1 = int.tryParse(parameter);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
                margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
                child: IntervalProgressBar(
                    direction: IntervalProgressDirection.horizontal,
                    max: count,
                    progress: count1,
                    intervalSize: 2,
                    size: Size(600, 10),
                    highlightColor: Colors.pink,
                    defaultColor: Colors.grey,
                    intervalColor: Colors.transparent,
                    intervalHighlightColor: Colors.transparent,
                    reverse: false,
                    radius: 0)),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _gotoB,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class RouteB extends StatefulWidget {
  @override
  _RouteBState createState() => _RouteBState();
}

class _RouteBState extends State<RouteB> {
  TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          body: Column(
        children: [
          TextField(
            controller: _textEditingController,
          ),
          RaisedButton(
            child: Text('Go back'),
            onPressed: () {
              Navigator.pop(context, _textEditingController.text);
            },
          ),
        ],
      )),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM