简体   繁体   English

为什么 flutter 中的小部件在 Tab 上不起作用?

[英]why does widget in flutter not working onTab?

**When I click a widget the screen not changing until I save file again. **当我单击一个小部件时,屏幕不会改变,直到我再次保存文件。

this is the log when I click a widget: D/skia ( 5395): Errors: D/skia ( 5395): link failed but did not provide an info log D/skia ( 5395): Shader compilation error D/skia ( 5395): ------------------------ D/skia ( 5395): Errors: D/skia ( 5395): link failed but did not provide an info log**这是我单击小部件时的日志:D/skia(5395):错误:D/skia(5395):链接失败但未提供信息日志 D/skia(5395):着色器编译错误 D/skia(5395 ):------------------------ D/skia(5395):错误:D/skia(5395):链接失败但未提供信息日志**

this is the code这是代码

import 'package:flutter/material.dart';
import 'package:flutter_cubit/pages/navpages/bar_item_page.dart';
import 'package:flutter_cubit/pages/navpages/home_page.dart';
import 'package:flutter_cubit/pages/navpages/my_page.dart';
import 'package:flutter_cubit/pages/navpages/search_page.dart';

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

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

class _MainPageState extends State<MainPage> {
  List pages=[
    HomePage(),
    BarItemPage(),
    SearchPage(),
    MyPage()
  ];
  int currentIndex=0;
  void onTap(int index){
    currentIndex=index;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: pages[currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        unselectedFontSize: 0,
        selectedFontSize: 0,
        type: BottomNavigationBarType.fixed,
        backgroundColor: Colors.white,
        onTap: onTap,
        currentIndex: currentIndex,
        selectedItemColor: Colors.black54,
        unselectedItemColor: Colors.grey.withOpacity(0.5),
        showUnselectedLabels: false,
        showSelectedLabels: false,
        elevation: 0,

        items: [
          BottomNavigationBarItem(title: Text("Home"), icon: Icon(Icons.apps)),
          BottomNavigationBarItem(title: Text("Bar"), icon: Icon(Icons.bar_chart_sharp)),
          BottomNavigationBarItem(title: Text("Search"), icon: Icon(Icons.search)),
          BottomNavigationBarItem(title: Text("My"), icon: Icon(Icons.person)),
        ],
      ),
    );
  }
}


you are not calling setState.您没有调用 setState。 so change onTap to below code:所以将 onTap 更改为以下代码:

void onTap(int index){
  setState(() {
    currentIndex=index;
  }); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM