当我有BottomNavigatorBar时,不能使用setState。

问题描述 投票:2回答:1

我的问题是,如果我想从toggle更新视图,我不能,因为我需要为屏幕创建一个新的类。我想当我按下按钮时,更新我的视图,并将滑块从第一个元素移动到第二个元素,但我不知道如何做到这一点。如果我想从第一个元素移动到第二个元素的动作,我试过用打印,但我希望它是更多的互动,所以我希望滑块移动。我还使用了一个neumorphic包进行设计,使其更容易。

这是有滑块的地方,我想让滑块移动来实现互动。

screenCatalog() {
  return SingleChildScrollView(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        SizedBox(height: 40,),
        NeumorphicToggle(
          height: 30,
          selectedIndex: _selectedIndex,
          displayForegroundOnlyIfSelected: true,
          children: [ ToggleElement(
            background: Center(child: Text('Semestrul 1',
                style: TextStyle(fontWeight: FontWeight.w200)),),
            foreground: Center(child: Text('Semestrul 1',
                style: TextStyle(fontWeight: FontWeight.w400)),),
          ),
            ToggleElement(
              background: Center(child: Text('Semestrul 2',
                  style: TextStyle(fontWeight: FontWeight.w200)),),
              foreground: Center(child: Text('Semestrul 2',
                  style: TextStyle(fontWeight: FontWeight.w400)),),
            )
          ],
          thumb: Neumorphic(
            boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(10)),
            style: NeumorphicStyle(
              lightSource: LightSource.topLeft,
              intensity: 0.8,
              shape: NeumorphicShape.flat,
              depth: 3,
              color: Colors.grey[100],
            ),
          ),
          onChanged: (value) {
             //here I want to make the setState but i can't,it gives me error
          },
        ),
        SizedBox(height: 10,),
      ],
    ),
  );
}

这是整个代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:onefocus/services/customicons.dart';
import 'package:onefocus/data_processing/elev_processing_data.dart';

int _selectedIndex = 0;

class ElevHomePage extends StatefulWidget{
  @override
  _ElevHomePageState createState() => _ElevHomePageState();
}
class _ElevHomePageState extends State<ElevHomePage> {

  int _currentIndex = 0;

  List<Widget> tabs = [
    screenHome(),
    screenCatalog(),
    Center(child: Text('In WORK'),),
    Center(child: Text('In WORK'),)
  ];

  tapped(int tappedIndex) {
    setState(() {
      _currentIndex = tappedIndex;
    });
  }

  changed(int introducedIndex) {
    setState(() {
      _selectedIndex = introducedIndex;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: tabs[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: tapped,
          showSelectedLabels: false,
          showUnselectedLabels: false,
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
              icon: SvgPicture.asset(CustomIcons[0],
                width: 30,
                height: 30,),
              title: Text(''),
            ),
            BottomNavigationBarItem(
              icon: SvgPicture.asset(CustomIcons[1],
                width: 30,
                height: 30,),
              title: Text(''),
            ),
            BottomNavigationBarItem(
              icon: SvgPicture.asset(CustomIcons[2],
                  width: 30,
                  height: 30),
              title: Text(''),
            ),
            BottomNavigationBarItem(
              icon: SvgPicture.asset(CustomIcons[3],
                width: 30,
                height: 30,),
              title: Text(''),
            ),
          ]),
    );
  }
}
screenHome() {
  return SingleChildScrollView(
    child: Container(
      padding: EdgeInsets.only(left: 20, right: 20),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SizedBox(height: 100,),
          NeumorphicButton(
            onClick: () {},
            boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(20)),
            style: NeumorphicStyle(
              lightSource: LightSource.topLeft,
              intensity: 0.8,
              shape: NeumorphicShape.flat,
              depth: 3,
              color: Colors.grey[100],
            ),
            child: Column(
              children: <Widget>[
                SizedBox(height: 20,),
                Container(
                  child: Center(
                    child: Text(
                      'Matematica',
                      style: TextStyle(
                          fontSize: 35, fontWeight: FontWeight.w200),
                    ),
                  ),
                ),
                SizedBox(height: 8,),
                Container(padding: EdgeInsets.only(left: 15),
                    child: Row(
                        children: <Widget>[
                          Text('Ultima nota: 9',
                            style: TextStyle(
                                fontSize: 22, fontWeight: FontWeight.w200),
                          ),
                        ]
                    )
                ),
                SizedBox(height: 10,),
                Container(
                  padding: EdgeInsets.only(left: 15),
                  child:
                  Row(
                    children: <Widget>[
                      Text('Media: 10',
                        style: TextStyle(fontSize: 22, fontWeight: FontWeight
                            .w200),
                      ),
                      SizedBox(width: 5,),
                      Icon(Icons.arrow_upward, color: Colors.greenAccent,)
                    ],
                  ),
                ),
                SizedBox(height: 10,),
                Container(padding: EdgeInsets.only(left: 15),
                    child: Row(
                        children: <Widget>[
                          Text('Urmatoarea ora: 24.09.20 - 11:00',
                            style: TextStyle(
                                fontSize: 22, fontWeight: FontWeight.w200),
                          ),
                        ]
                    )
                ),
                Container(
                  padding: EdgeInsets.only(right: 10),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: <Widget>[
                      Icon(
                        Icons.arrow_forward, color: Colors.lightBlueAccent,),
                    ],
                  ),
                ),

                SizedBox(height: 10,),
              ],
            ),
          ),
          SizedBox(height: 15,),
          Text('Recente',
            style: TextStyle(fontSize: 19,
                fontWeight: FontWeight.w500,
                color: Colors.grey[400]),
          ),
          ListView.builder(
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemCount: recente.length,
              itemBuilder: (BuildContext context, int index) {
                return Column(

                  children: <Widget>[

                    Neumorphic(
                      boxShape: NeumorphicBoxShape.roundRect(
                          BorderRadius.circular(14)),
                      style: NeumorphicStyle(
                        lightSource: LightSource.topLeft,
                        intensity: 0.8,
                        shape: NeumorphicShape.flat,
                        depth: 3,
                        color: Colors.grey[100],
                      ),
                      child:
                      Container(
                        padding: EdgeInsets.only(top: 13, left: 13),
                        height: 60,
                        width: 370,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text(recente[index].event,
                              style: TextStyle(
                                  fontSize: 25, fontWeight: FontWeight.w200),
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: <Widget>[
                                Text(recente[index].data,
                                  style: TextStyle(fontSize: 15,
                                      fontWeight: FontWeight.w200),
                                ),
                                SizedBox(width: 10,)
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                    SizedBox(height: 30,),
                  ],);
              }
          ),
          SizedBox(height: 10,),
        ],
      ),
    ),
  );
}

screenCatalog() {
  return SingleChildScrollView(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        SizedBox(height: 40,),
        NeumorphicToggle(
          height: 30,
          selectedIndex: _selectedIndex,
          displayForegroundOnlyIfSelected: true,
          children: [ ToggleElement(
            background: Center(child: Text('Semestrul 1',
                style: TextStyle(fontWeight: FontWeight.w200)),),
            foreground: Center(child: Text('Semestrul 1',
                style: TextStyle(fontWeight: FontWeight.w400)),),
          ),
            ToggleElement(
              background: Center(child: Text('Semestrul 2',
                  style: TextStyle(fontWeight: FontWeight.w200)),),
              foreground: Center(child: Text('Semestrul 2',
                  style: TextStyle(fontWeight: FontWeight.w400)),),
            )
          ],
          thumb: Neumorphic(
            boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(10)),
            style: NeumorphicStyle(
              lightSource: LightSource.topLeft,
              intensity: 0.8,
              shape: NeumorphicShape.flat,
              depth: 3,
              color: Colors.grey[100],
            ),
          ),
          onChanged: (value) {
             //here I want to make the setState but i can't,it gives me error
          },
        ),
        SizedBox(height: 10,),
      ],
    ),
  );
}

flutter dart
1个回答
2
投票

问题就出在这里。

 NeumorphicToggle(
          height: 30,
          selectedIndex: _selectedIndex,
          displayForegroundOnlyIfSelected: true,
          children: [ ToggleElement(
            background: Center(child: Text('Semestrul 1',
                style: TextStyle(fontWeight: FontWeight.w200)),),
            foreground: Center(child: Text('Semestrul 1',
                style: TextStyle(fontWeight: FontWeight.w400)),),
          ),
            ToggleElement(
              background: Center(child: Text('Semestrul 2',
                  style: TextStyle(fontWeight: FontWeight.w200)),),
              foreground: Center(child: Text('Semestrul 2',
                  style: TextStyle(fontWeight: FontWeight.w400)),),
            )
          ],
          thumb: Neumorphic(
            boxShape: NeumorphicBoxShape.roundRect(BorderRadius.circular(10)),
            style: NeumorphicStyle(
              lightSource: LightSource.topLeft,
              intensity: 0.8,
              shape: NeumorphicShape.flat,
              depth: 3,
              color: Colors.grey[100],
            ),
          ),
          onChanged: (value) {
             //here I want to make the setState but i can't,it gives me error
          },
        ),

如果你使用底部导航条,你需要一个没有状态的部件列表。如果你想在页面中制作动画过渡,你将需要这个。

 StatefulBuilder(
             builder:(BuildContext context, StateSetter setState) {
return //Whatever you want to use here ();
},
),

在这之后,你就可以在你需要的地方使用setState来让你的应用程序更加动态。

© www.soinside.com 2019 - 2024. All rights reserved.