Кнопка флаттера, новая тема, копия не меняет цвет - PullRequest
0 голосов
/ 18 октября 2019

У меня есть маленький кусочек кода ниже. Должно быть показано «добавить иконку» синим цветом. Вместо этого он показывает кнопку с черным цветом (как accentColor в MaterialApp). Я что-то не так делаю в «плавающем действии»?

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

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final appName = "Custom Themes";

    return new MaterialApp(
      title: appName,
      theme: new ThemeData(
        brightness: Brightness.dark,
        primaryColor: Colors.red[300],
        accentColor: Colors.black,
      ),
      home: new MyHomePage(
        title: appName
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  MyHomePage({Key key, @required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: new Text(title),
      ),
      body: new Center(
        child: new Container(
          color: Theme.of(context).primaryColor,
          child: new Text(
            "Text with background color",
            style: Theme.of(context).textTheme.title,
          ),
        ),
      ),
    floatingActionButton: new Theme(
      data: Theme.of(context).copyWith(accentColor: Colors.blue),
      child: new FloatingActionButton(
        onPressed: null,
        child: new Icon(Icons.add),
      ),
    )
    );
  }

}

Я смотрел учебник (он трепетает в iOS), и там все работает как шарм. Тут с апреля 2019 года. Может быть, что-то изменилось с того времени?

1 Ответ

0 голосов
/ 21 октября 2019

Вы можете использовать

Theme.of(context).copyWith(
        colorScheme:
        Theme.of(context).colorScheme.copyWith(secondary: Colors.blue),
      )

подробный справочник https://flutter.dev/docs/cookbook/design/themes#complete-example

полный код

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    final appName = "Custom Themes";

    return  MaterialApp(
      title: appName,
      theme:  ThemeData(
        brightness: Brightness.dark,
        primaryColor: Colors.red[300],
        accentColor: Colors.black,
      ),
      home:  MyHomePage(
          title: appName
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  MyHomePage({Key key, @required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return  Scaffold(
        appBar: AppBar(
          title:  Text(title),
        ),
        body:  Center(
          child:  Container(
            color: Theme.of(context).primaryColor,
            child:  Text(
              "Text with background color",
              style: Theme.of(context).textTheme.title,
            ),
          ),
        ),
        floatingActionButton:  Theme(
          data: Theme.of(context).copyWith(
            colorScheme:
            Theme.of(context).colorScheme.copyWith(secondary: Colors.blue),
          ),
          child:  FloatingActionButton(
            onPressed: null,
            child:  Icon(Icons.add),
          ),
        )
    );
  }

}

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...