Search code examples
flutteronresume

call method in container flutter


I have class called A.dart in that I am usnign Interprogressbar as below

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. Once use completes process in class B.dart I ll pop the class B.dart. Again A.dart will be visible for the user. So my question us how can I restart the IntervalProgressBar with the updated value once user comes from B.dart to A,dart.


Solution

  • You can copy paste run full code below
    You can await Navigator.push then call 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

    enter image description here

    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);
                },
              ),
            ],
          )),
        );
      }
    }