Flutter - Problem ze Slider w okienku Message Box (AlertDialog)

0

Witam

Piszę aplikację we Flutter. Używam GridView żeby utworzyć na ekranie 6 elementów. Po naciśnięciu każdego z nich ma pojawić się AlertDialog w którym umieszczam Slider. Dla każdego elementu GridView ma być inny Slider z innymi parametrami.
Mam już napisany kod, tylko problem jest taki że po wywołaniu okna AlertDialog Slider nie zmienia swojej pozycji. Żeby zmieniać stan suwaka wykorzystałem StatefulBuilder ale to nie pomogło.

Utworzyłem klasę w której zamieściłem parametry, które chcę przekazać do GridView. Umieściłem tam też Slider. Następnie zrobiłem funkcje przekazParametry w której umieściłem Listę konstruktorów. W każdym znajduje się inny Slider, który wywołuje w AlertDialog w ten sposób item.Slider.

Może ktoś ma pomysł co zrobiłem źle i jak to poprawić? Walczę z tym już długo ale nie mogę zrobić żeby Slider zmieniał pozycję. Każda wskazówka może się przydać.
Z góry dziękuję.

Mój Kod:

import 'package:flutter/cupertino(1).dart';
import 'package:flutter/material.dart';
import 'cart.dart';
import 'param_serwis.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        brightness: Brightness.dark, primarySwatch: Colors.blueGrey,
      ),
      home: const MyHomePage(title: 'Ustawienia serwisowe'),
    );
  }
}

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

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

class _MyHomePageState extends State<MyHomePage> {
  List<SerwisParam> _listaKontruktor = <SerwisParam>[];

  double _currentSliderValue = 20;
  double _currentValueCzOdP = 20;
  @override
  void initState() {
    super.initState();
    _przekazParametry();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title,
            style: const TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.bold,
                fontSize: 24.0)),
      ),
      drawer: Drawer(
        child: ListView(
          // Important: Remove any padding from the ListView.
          padding: EdgeInsets.zero,
          children: [
            const DrawerHeader(
              decoration: BoxDecoration(
                color: Colors.white10,
              ),
              child: Text("Menu",
                  style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 26.0)),
            ),
            ListTile(
              title: const Text('Ustawienia serwisowe'),
              tileColor: Colors.white10,
              onTap: () {
                // Navigator.push(
                //   context,
                //   MaterialPageRoute(builder: (context) => UstawieniaSerwis()),
                // );
              },
            ),
            ListTile(
              title: const Text('Zaawansowane'),
              tileColor: Colors.white10,
              onTap: () {
                // Navigator.push(
                //   context,
                //   MaterialPageRoute(builder: (context) => UstawieniaSerwis()),
                // );
              },
            ),
            ListTile(
              title: const Text('Alarmy'),
              tileColor: Colors.white10,
              onTap: () {
                // Update the state of the app
                // ...
                // Then close the drawer
                Navigator.pop(context);
              },
            ),
            ListTile(
              title: const Text('Informacje'),
              tileColor: Colors.white10,
              onTap: () {
                // Navigator.push(
                //   context,
                //   MaterialPageRoute(builder: (context) => UstawieniaSerwis()),
                // );
              },
            ),
          ],
        ), // Populate the Drawer in the next step.
      ),
      body: _buildGridView(),
    );
  }

  GridView _buildGridView() {
    return GridView.builder(
        padding: const EdgeInsets.all(4.0),
        gridDelegate:
        const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
        itemCount: _listaKontruktor.length,
        itemBuilder: (context, index) {
          var item = _listaKontruktor[index];
          return GestureDetector(
            child: Card(
              elevation: 4.0,
              color: Colors.white24,
              margin: const EdgeInsets.all(10),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(30),
              ),
              child: Stack(
                fit: StackFit.loose,
                alignment: Alignment.center,
                children: <Widget>[
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      const Padding(padding: EdgeInsets.only(top:20)),
                      Text(
                          item.name,
                          textAlign: TextAlign.center,
                          style: const TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                              fontSize: 24.0)
                      ),
                      const Padding(padding: EdgeInsets.only(top:20)),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          //const Padding(padding: EdgeInsets.only(right:10)),
                          const Text(
                              'Nast:   ',
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 30.0)
                          ),
                          Text(
                              item.aktNastawa,
                              textAlign: TextAlign.center,
                              style: const TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 30.0)
                          ),
                        ],
                      ),
                    ],
                  ),

                ],
              ),
            ),
            onTap: () {
              showDialog(
                  context: context,
                  builder:(context){
                    return AlertDialog(
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(30),
                      ),
                      title: Text(item.name,
                          textAlign: TextAlign.center,
                          style: const TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                              fontSize: 34.0)
                      ),
                      content: Text('Akt. Nast.: ' + item.aktNastawa,
                          textAlign: TextAlign.center,
                          style: const TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                              fontSize: 30.0)
                      ),
                      actions: <Widget>[
                        // usually buttons at the bottom of the dialog
                        StatefulBuilder(builder: (context, setState) {
                          return item.sliderNast;
                          //   Slider(
                          //   value: _currentSliderValue,
                          //   min: 0,
                          //   max: 50,
                          //   divisions: 85,
                          //   label: _currentSliderValue.round()
                          //       .toString(),
                          //   onChanged: (double value) {
                          //     setState(() {
                          //       _currentSliderValue = value;
                          //       // _currentSliderValue = value;
                          //     });
                          //   },
                          // );
                        }),
                        FlatButton(
                          child: const Text("Ustaw",
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 24.0)
                          ),
                          onPressed: () {
                            Navigator.of(context).pop();
                          },
                        ),
                      ],
                    );
                  });

            },
          );
        });
  }

  void _przekazParametry() {
    var list = <SerwisParam>[
      SerwisParam(
        name: 'Histereza',
        aktNastawa: '2',
        color: Colors.amber,
        sliderNast: Slider(
          value: _currentSliderValue,
          min: 0,
          max: 50,
          divisions: 85,
          label: _currentSliderValue.round()
              .toString(),
          onChanged: (double value) {
            setState(() {
              _currentSliderValue = value;
              // _currentSliderValue = value;
            });
          },
        ),
      ),
      SerwisParam(
        name: 'Czas odł. pompy',
        aktNastawa: '23',
        color: Colors.deepOrange,
        sliderNast: Slider(
          value: _currentValueCzOdP,
          min: 0,
          max: 80,
          divisions: 85,
          label: _currentValueCzOdP.round()
              .toString(),
          onChanged: (double value) {
            setState(() {
              _currentValueCzOdP = value;
              // _currentSliderValue = value;
            });
          },
        ),
      ),
      SerwisParam(
        name: 'Obniżka kotła/obiegu',
        aktNastawa: '20',
        color: Colors.brown,
        sliderNast: Slider(
          value: _currentSliderValue,
          min: 0,
          max: 70,
          divisions: 85,
          label: _currentSliderValue.round()
              .toString(),
          onChanged: (double value) {
            setState(() {
              _currentSliderValue = value;
              // _currentSliderValue = value;
            });
          },
        ),
      ),
      SerwisParam(
        name: 'Sygnalizacja dźwiękowa',
        aktNastawa: 'wył.',
        color: Colors.green,
        sliderNast: Slider(
          value: _currentSliderValue,
          min: 0,
          max: 50,
          divisions: 85,
          label: _currentSliderValue.round()
              .toString(),
          onChanged: (double value) {
            setState(() {
              _currentSliderValue = value;
              // _currentSliderValue = value;
            });
          },
        ),
      ),
      SerwisParam(
        name: 'Alarm użytkownika',
        aktNastawa: '50',
        color: Colors.purple,
        sliderNast: Slider(
          value: _currentSliderValue,
          min: 0,
          max: 50,
          divisions: 85,
          label: _currentSliderValue.round()
              .toString(),
          onChanged: (double value) {
            setState(() {
              _currentSliderValue = value;
              // _currentSliderValue = value;
            });
          },
        ),
      ),
      SerwisParam(
        name: 'Czas zał. pompy',
        aktNastawa: '35',
        color: Colors.blueGrey,
        sliderNast: Slider(
          value: _currentSliderValue,
          min: 0,
          max: 50,
          divisions: 85,
          label: _currentSliderValue.round()
              .toString(),
          onChanged: (double value) {
            setState(() {
              _currentSliderValue = value;
              // _currentSliderValue = value;
            });
          },
        ),
      ),
    ];

    //setState(() {
    _listaKontruktor = list;
    //});
  }
}

Klasa z parametrami do GridView:

import 'package:flutter/material.dart';

class SerwisParam {
  final String name;
  final String aktNastawa;
  final Color color;
  Slider sliderNast;

  SerwisParam({required this.name,required this.aktNastawa,required this.color, required this.sliderNast});
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1