Flutter-如何在不同的小部件上使用onPress在小部件之间进行切换

时间:2020-05-11 18:07:35

标签: flutter

curr_index中的category_selector.dart正在更改。我想要的是每次curr_index更改时刷新主窗口小部件。为什么它不起作用?

home.dart

import 'package:flutter/material.dart';
import 'attekintes.dart';
import 'category_selector.dart';
import 'szabadsag.dart';



class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  final tabs = [Attekintes(), Szabadsag()];
  var currentmenu = curr_index;
  var showingOriginalWidget = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).primaryColor,
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          iconSize: 30.0,
          color: Colors.white,
          onPressed: () {
            print(curr_index);
          },
        ),
        centerTitle: true,
        title: Text(
          'Home',
          style: TextStyle(
            fontSize: 28.0,
            fontWeight: FontWeight.bold,
          ),
        ),
        elevation: 5.0,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.search),
            iconSize: 30.0,
            color: Colors.white,
            onPressed: () {},
          ),
        ],
      ),
      body: Column(
        children: <Widget>[
          CategorySelector(),
          tabs[curr_index]
        ],
      ),
    );
  }
}

category_selector.dart

import 'package:flutter/material.dart';
  int curr_index=0;

class CategorySelector extends StatefulWidget {

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

class _CategorySelectorState extends State<CategorySelector> {
  static int selectedIndex = 0;
  final List<String> categories = ['First', 'Second', 'Third', 'Saját Adatok'];

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 90.0,
      color: Theme.of(context).primaryColor,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: categories.length,
        itemBuilder: (BuildContext context, int index) {
          return GestureDetector(
            onTap: () {
              setState(() {
                selectedIndex = index;
                curr_index=index;  // 
                print('Az index száma: $index');
              });
            },
            child: Padding(
              padding: EdgeInsets.symmetric(
                horizontal: 20.0,
                vertical: 30.0,
              ),
              child: Text(
                categories[index],
                style: TextStyle(
                  color: index == selectedIndex ? Colors.white : Colors.white60,
                  fontSize: 24.0,
                  fontWeight: FontWeight.bold,
                  letterSpacing: 1.2,
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}

当前应用程序用户界面:

pics about the app

1 个答案:

答案 0 :(得分:0)

您必须在CategorySelector()的构造函数中传递curr_index

例如:

首页正文:

__name__

CategorySelector窗口小部件

body: Column(
        children: <Widget>[
          CategorySelector(curr_index),
          tabs[curr_index]
        ],
      ),