Thursday, August 6, 2020

flutter

How to clear Flutter's Build cache?
run the next command on project folder 
flutter clean .


How to Call StatelessWidget with parameter ?

//MainBody(1); call MainBody and pass 1 to the class

class MainBody extends StatelessWidget {
  MainBody(this.yourData);

  final int yourData;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Padding(
        padding: const EdgeInsets.only(left: 20.0, right: 20.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new TimeCheck(yourData),
          ],
        ),
      ),
    );
  }
}


How to Call StatefulWidget with parameter?

//MyHomePage(title: 'MBRF 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> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ));
    
  }
}





SAMPLE Code

import 'package:flutter/material.dart';

void main(){



  runApp(MaterialApp(
    title: "Demo App",
    routes: {
      HomePage.routeName : (_) => HomePage(),
      Step1Page.routeName : (_) => Step1Page(),
      Step2Page.routeName : (_) => Step2Page(),
      Step3Page.routeName : (_) => Step3Page(),
    },
    home: SplashPage(),
    initialRoute: HomePage.routeName,
  ));
}

class SplashPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold();
  }
}


class HomePage extends StatelessWidget {

  static const routeName = "/home";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("Home Page"),
            RaisedButton(onPressed: (){
              Navigator.pushNamed(context, Step1Page.routeName);
            }, child: Text("Start Steps"),)
          ],
        ),
      ),
    );
  }
}


class Step1Page extends StatelessWidget {

  static const routeName = "/step1";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("Step1 Page"),
            RaisedButton(onPressed: (){
              Navigator.pushNamed(context, Step2Page.routeName);
            }, child: Text("Go Step2"),)
          ],
        ),
      ),
    );
  }
}



class Step2Page extends StatelessWidget {

  static const routeName = "/step2";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("Step2 Page"),
            RaisedButton(onPressed: (){
              Navigator.pushNamed(context, Step3Page.routeName);
            }, child: Text("Go Step3"),)
          ],
        ),
      ),
    );
  }
}

class Step3Page extends StatelessWidget {

  static const routeName = "/step3";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text("Step3 Page"),
            RaisedButton(onPressed: (){
              Navigator.popUntil(context, (Route<dynamic> route){
                bool shouldPop = false;
                if(route.settings.name == HomePage.routeName){
                  shouldPop = true;
                }
                return shouldPop;
              });
            }, child: Text("Go Home"),)
          ],
        ),
      ),
    );
  }
}
















Dialog


showDialog<bool>(
  context: context,
  builder: (c) => AlertDialog(
    title: Text('Warning'),
    content: Text('Do you want to register on new conference?'),
    actions: [
      FlatButton(
        child: Text('Yes'),
        onPressed: () =>   Navigator.popUntil(context, (Route<dynamic> route){
          bool shouldPop = false;
          print(route.settings.name);
          if(route.settings.name == "RegisterConfranceForm"){
            shouldPop = true;
          }
          return shouldPop;
        }) ,
      ),
      FlatButton(
        child: Text('No'),
        onPressed: () => Navigator.of(c).popUntil((route) => route.isFirst),
      ),
    ],
  ),


 Navigator.popUntil(context, (Route<dynamic> route){
            bool shouldPop = false;
            if(route.settings.name == HomePage.routeName){
              shouldPop = true;
            }
            return shouldPop;
          });

Flutter - Disable/Override Back Button with WillPopScope

https://www.woolha.com/tutorials/flutter-disable-override-back-button-with-willpopscope

No comments: