Home > other >  How to replace the pressing Button by the entered value button in flutter?
How to replace the pressing Button by the entered value button in flutter?

Time:10-03

i'm a new programmer in flutter and i have a question about the flutter hello world project How can I enter the desired value directly without pressing the button every time to access it For example, enter the number 40 without pressing 40 times on the button?

......

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

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter  ;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

CodePudding user response:

you can use TextFormFiled Widget to change value

 final TextEditingController mycontroller;


  TextFormField(
 onChanged: () {
        setState() {
             _counter = mycontroller.text;
        }
        ;
     
      },
      validator: validator,
      controller: mycontroller,
      keyboardType: TextInputType.number,
    
      decoration: InputDecoration(
        hintText: hinttext.tr,
        floatingLabelBehavior: FloatingLabelBehavior.always,
        label: Text(labeltext.tr),
        border: OutlineInputBorder(borderRadius: BorderRadius.circular(30)),
      
        contentPadding: const EdgeInsets.symmetric(horizontal: 15, vertical: 5),
      ),
    )

CodePudding user response:

 void _incrementCounter() {
    setState(() {
      _counter  = 40;
    });
  }
  • Related