Home > Software engineering >  Flutter - Can´t drag Slider
Flutter - Can´t drag Slider

Time:01-14

In Flutter i have a simple slider to select latitude and longitude, like this.

 import 'package:flutter/material.dart';
    
    class FormPage extends StatefulWidget {
      const FormPage({Key? key}) : super(key: key);
    
      @override
      _FormPage createState() => _FormPage();
    }
    
    class _FormPage extends State<FormPage> {
      @override
      Widget build(BuildContext context) {
        final _name = TextEditingController();
        final _email = TextEditingController();
        final _password = TextEditingController();
        String _language = 'ES';
        final _city = TextEditingController();
        double _latitude = 90.0;
        double _longitude = 180.0;
    
        final form = Column(mainAxisSize: MainAxisSize.min, children: [
          Text('New user', style: Theme.of(context).textTheme.headline4),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextFormField(
              controller: _name,
              decoration: const InputDecoration(labelText: 'Name'),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextFormField(
              controller: _email,
              decoration: const InputDecoration(labelText: 'Email'),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextFormField(
              controller: _password,
              obscureText: true,
              enableSuggestions: false,
              autocorrect: false,
              decoration: const InputDecoration(labelText: 'Password'),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextFormField(
              controller: _city,
              decoration: const InputDecoration(labelText: 'City'),
            ),
          ),
          DropdownButton<String>(
            value: _language,
            icon: const Icon(Icons.arrow_downward),
            elevation: 16,
            style: const TextStyle(color: Colors.deepPurple),
            underline: Container(
              height: 2,
              color: Colors.deepPurpleAccent,
            ),
            onChanged: (String? newValue) {
              setState(() {
                _language = newValue!;
              });
            },
            items:
                <String>['ES', 'EN'].map<DropdownMenuItem<String>>((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );
            }).toList(),
          ),
          Slider(
            value: _latitude,
            max: 90.0,
            min: -90.0,
            divisions: 1,
            label: _latitude.toString(),
            onChanged: (double newValue) {
              setState(() {
                _latitude = newValue; <- Doesn´t update value
              });
            },
          ),
          Slider(
            value: _longitude,
            max: 180.0,
            min: -180.0,
            divisions: 1,
            label: _longitude.toString(),
            onChanged: (double newValue) {
              setState(() {
                _longitude = newValue; <- Doesn´t update value
              });
            },
          )
        ]);
    
        return Scaffold(
          body: SafeArea(child: form),
          floatingActionButton: FloatingActionButton(
            onPressed: () => null,
            tooltip: 'Save',
            child: const Icon(Icons.check),
          ),
        );
      }
    }

The problem: The slider doesn´t update the state, so can´t change value. So, anybody see what´s wrong?

CodePudding user response:

You have to put your variables outside of you widget build function, otherwise everytime you call setstate your latitude and longtitude are reset to 90 and 180

import 'package:flutter/material.dart';

class FormPage extends StatefulWidget {
  const FormPage({Key? key}) : super(key: key);

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

class _FormPage extends State<FormPage> {
  final _name = TextEditingController();
  final _email = TextEditingController();
  final _password = TextEditingController();
  String _language = 'ES';
  final _city = TextEditingController();
  double _latitude = 90.0;
  double _longitude = 180.0;
  
  @override
  Widget build(BuildContext context) {

    final form = Column(mainAxisSize: MainAxisSize.min, children: [
      Text('New user', style: Theme.of(context).textTheme.headline4),
      Padding(
        padding: const EdgeInsets.all(8.0),
        child: TextFormField(
          controller: _name,
          decoration: const InputDecoration(labelText: 'Name'),
        ),
      ),
      Padding(
        padding: const EdgeInsets.all(8.0),
        child: TextFormField(
          controller: _email,
          decoration: const InputDecoration(labelText: 'Email'),
        ),
      ),
      Padding(
        padding: const EdgeInsets.all(8.0),
        child: TextFormField(
          controller: _password,
          obscureText: true,
          enableSuggestions: false,
          autocorrect: false,
          decoration: const InputDecoration(labelText: 'Password'),
        ),
      ),
      Padding(
        padding: const EdgeInsets.all(8.0),
        child: TextFormField(
          controller: _city,
          decoration: const InputDecoration(labelText: 'City'),
        ),
      ),
      DropdownButton<String>(
        value: _language,
        icon: const Icon(Icons.arrow_downward),
        elevation: 16,
        style: const TextStyle(color: Colors.deepPurple),
        underline: Container(
          height: 2,
          color: Colors.deepPurpleAccent,
        ),
        onChanged: (String? newValue) {
          setState(() {
            _language = newValue!;
          });
        },
        items:
        <String>['ES', 'EN'].map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(value),
          );
        }).toList(),
      ),
      Slider(
        value: _latitude,
        max: 90.0,
        min: -90.0,
        divisions: 1,
        label: _latitude.toString(),
        onChanged: (double newValue) {
          setState(() {
            _latitude = newValue; <- Doesn´t update value
          });
        },
      ),
      Slider(
        value: _longitude,
        max: 180.0,
        min: -180.0,
        divisions: 1,
        label: _longitude.toString(),
        onChanged: (double newValue) {
          setState(() {
            _longitude = newValue; <- Doesn´t update value
          });
        },
      )
    ]);

    return Scaffold(
      body: SafeArea(child: form),
      floatingActionButton: FloatingActionButton(
        onPressed: () => null,
        tooltip: 'Save',
        child: const Icon(Icons.check),
      ),
    );
  }
}
  •  Tags:  
  • Related