Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter code, To add or display the widget on press button or set state on press method [closed]

Tags:

flutter

dart

I am new to flutter and want to display the text widget after pressing the button.

   import 'package:flutter/material.dart';

    class dellecturer extends StatefulWidget {
      @override
      _dellecturerState createState() => _dellecturerState();
    }

    class _dellecturerState extends State<dellecturer> {
      @override
      Widget build(BuildContext context) {
        return Card(
          color: Colors.grey.shade300,
          margin: EdgeInsets.all(10.0),
        child: Container(
          margin: EdgeInsets.all(20),
          child: ListView(
            children: <Widget>[
             new Container(
               margin: EdgeInsets.only(top:13,bottom: 25),
               alignment: Alignment.topCenter,
                 child:Text("DELETE BY LECTURER ID",style: TextStyle(color: Colors.redAccent,fontWeight: FontWeight.bold,fontSize: 20.0),),
             ),
             new TextField(
                decoration:InputDecoration(border: OutlineInputBorder(borderSide: BorderSide(style: BorderStyle.solid),),labelText: "LECTURER ID",hintText: "Enter lecturer ID",prefixIcon: Icon(Icons.person)),
              ),
              Padding(padding: EdgeInsets.only(top:20),),
             new RaisedButton(child: Text("OK",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.white,fontSize: 15),),color: Colors.red,onPressed: (){Form(child: Search());},),//onclick search
            ],
          ),


        ),
        ); 

      }
    Widget Search(){  //should display
      setState(() {
      return Scaffold(
        body: Container(
        child: ListView(
          children: <Widget>[
            new Text("ID :1234",style: TextStyle(color: Colors.redAccent),),
             new Text("ID :1234",style: TextStyle(color: Colors.redAccent),),
              new Text("ID :1234",style: TextStyle(color: Colors.redAccent),),
          ],
        ),
        )
      );
      });

    }
    }
like image 622
Yogish Shenoy Avatar asked Mar 04 '23 21:03

Yogish Shenoy


1 Answers

I don't understand what are you trying to do exactly in code , but if you need to display a Text widget after pressing button you can simply define a bool variable to detect button click as below :

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: MyApp(),
    ));

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() {
    return new MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  bool pressed = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: <Widget>[
          pressed ? Text(" text is here ") : SizedBox(),
          RaisedButton(
            child: Text("show text"),
            onPressed: () {
              setState(() {
                pressed = true;
              });
            },
          )
        ],
      ),
    );
  }
}
like image 79
Hasan Avatar answered Mar 07 '23 18:03

Hasan