【问题标题】:Flutter Text WidgetFlutter 文本小部件
【发布时间】:2020-12-11 11:21:01
【问题描述】:

我有一个带有三个文本小部件的简单屏幕,当用户单击其中一个文本小部件时,文本应该变成黑色字体文本颜色,其他文本小部件颜色不应该改变。所以我在下面提供我的代码和如何我可以在我的代码上实现,以便只有点击的文本改变颜色。

Main.Dart 文件

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() {
  runApp(myApp());
}

class myApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "Demo",
      theme: ThemeData(primarySwatch: Colors.blue),
      home: HomeData(),
    );
  }
}

class HomeData extends StatefulWidget {
  @override
  _HomeDataState createState() => _HomeDataState();
}

class _HomeDataState extends State<HomeData> {
  Color _textColor1 = Colors.black;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "Demo",
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.all(0),
              child: InkWell(
                onTap: () {
                  filterText();
                  setState(() {
                    _textColor1 = Colors.black;
                  });
                },
                child: Text(
                  "All",
                  style: TextStyle(
                    color: Colors.black26,
                    fontSize: 20,
                  ),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 10),
              child: Text(
                "|",
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 20,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.all(0),
              child: InkWell(
                onTap: () {
                  filterText();
                },
                child: Text(
                  "QR Codes",
                  style: TextStyle(
                    color: Colors.black26,
                    fontSize: 20,
                  ),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 10),
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 10),
                child: Text(
                  "|",
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.all(0),
              child: InkWell(
                onTap: () {
                  filterText();
                },
                child: Text(
                  "Bar Codes",
                  style: TextStyle(
                    color: Colors.black26,
                    fontSize: 20,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void filterText() {
    String filterAllText = "All";
    setState(() {
      Fluttertoast.showToast(
          msg: "$filterAllText",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          backgroundColor: Colors.blue,
          textColor: Colors.white,
          fontSize: 14.0);
    });
  }
}

【问题讨论】:

    标签: flutter flutter-test flutter-widget


    【解决方案1】:

    你可以试试下面的代码:

    class _HomeDataState extends State<HomeData> {
      Color _textColor1 = Colors.black;
      Color _textColor2 = Colors.black26;
      bool _isCheckedAll = true;
      bool _isCheckedQr = false;
      bool _isCheckedBar = false;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              "Demo",
              style: TextStyle(color: Colors.white),
            ),
          ),
          body: Center(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: EdgeInsets.all(0),
                  child: InkWell(
                    onTap: () {
                      setState(() {
                        _isCheckedAll = true;
                        _isCheckedQr = false;
                        _isCheckedBar = false;
                      });
                    },
                    child: Text(
                      "All",
                      style: TextStyle(
                        color: _isCheckedAll ? _textColor1 : _textColor2,
                        fontSize: 20,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  child: Text(
                    "|",
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.all(0),
                  child: InkWell(
                    onTap: () {
                      setState(() {
                        _isCheckedAll = false;
                        _isCheckedQr = true;
                        _isCheckedBar = false;
                      });
                    },
                    child: Text(
                      "QR Codes",
                      style: TextStyle(
                        color: _isCheckedQr ? _textColor1 : _textColor2,
                        fontSize: 20,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 10),
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: 10),
                    child: Text(
                      "|",
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.all(0),
                  child: InkWell(
                    onTap: () {
                      setState(() {
                        _isCheckedAll = false;
                        _isCheckedQr = false;
                        _isCheckedBar = true;
                      });
                    },
                    child: Text(
                      "Bar Codes",
                      style: TextStyle(
                        color: _isCheckedBar ? _textColor1 : _textColor2,
                        fontSize: 20,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 2020-02-07
      • 2021-03-24
      • 2019-12-08
      • 2019-06-11
      • 2018-04-17
      • 2021-12-01
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多