如何通过主题小部件使用字体设置?

时间:2019-12-24 22:45:39

标签: flutter

我正在编写一个非常简单的优惠券应用程序,但是我在应用程序中使用主题时遇到了问题。

我正在尝试创建一个具有自己主题的优惠券屏幕,该主题与应用程序的主要主题无关。主题小部件使我可以非常轻松地设置Scaffold的设置,但是问题出在字体上。当我尝试设置body1字体的设置时,它可以工作,但是如果我创建body2设置并尝试通过Theme.of(context).textTheme.body2使用它,它将无法正常工作,并且会退回到应用程序的字体中定义的设置。主旋律。我不要我想在屏幕上使用隔离的主题设置。

经典优惠券屏幕

import 'package:flutter/material.dart';

class ClassicCouponScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Theme(
      data: ThemeData(
        scaffoldBackgroundColor: Colors.blue,
        textTheme: TextTheme(
          body1: TextStyle(color: Colors.blue) // These settings aren't applied, it doesn't work 
        )
      ),
      child: Scaffold(
        body: SafeArea(
          child: Container(
            padding: EdgeInsets.all(16.0),
            child: Column(
              children: [
                Text('Test', style: Theme.of(context).textTheme.body1,) // Here is the problem. Instead of settings defined in upper ThemeData, settings from theme.dart are applied. 
              ],
            ),
          ),
        ),
      ),
    );
  }
}

应用程序的主要主题

import 'package:flutter/material.dart';

ThemeData theme() {
  return ThemeData(
    appBarTheme: AppBarTheme(
      color: Colors.blueGrey[700],
      textTheme: TextTheme(
        title: TextStyle(fontFamily: 'Ubuntu', fontSize: 25.0)
      )
    ),
    tabBarTheme: TabBarTheme(
      labelStyle: TextStyle(fontFamily: 'Ubuntu'),
      unselectedLabelStyle: TextStyle(fontFamily: 'Ubuntu')
    ),
    indicatorColor: Colors.blueGrey[200],
    scaffoldBackgroundColor: Colors.blueGrey[600],
    textTheme: TextTheme(
      title: TextStyle(fontFamily: 'Ubuntu', fontSize: 25.0, color: Colors.white),
      body1: TextStyle(fontFamily: 'Ubuntu', fontSize: 19.0, color: Colors.white),
      body2: TextStyle(fontFamily: 'Ubuntu', fontSize: 10.0, color: Colors.white)
    )
  );
}

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以尝试以下解决方法。

final class SSSWebView: UIViewController {

    @IBOutlet weak var webVİew: WKWebView!
    var formHTML = String()

    @IBAction func menuSlider(_ sender: Any) {
          toggleSideMenuView()
      }
      let keychain = KeychainSwift()

    @IBOutlet weak var logButton: UIBarButtonItem!
    @IBAction func logoutButton(_ sender: Any) {

          let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
          let vc : MyNavigationController = mainStoryboard.instantiateViewController(withIdentifier: "loginNavControl") as! MyNavigationController
            vc.modalPresentationStyle = .fullScreen
            self.present(vc, animated: false, completion: nil)
      }

    override func viewDidLoad() {
        super.viewDidLoad()
          let loginKeychain = keychain.get("LoginSuccess")     
HTMLKodOku()
    }

    func HTMLKodOku() {
               let client = SQLClient.sharedInstance()!
                                     client.connect("...", username: "...", password: "...", database: "...") { success in
                                     client.execute("SELECT sayfa_icerik FROM ... WHERE ... = 22", completion: { (_ results: ([Any]?)) in
                                             for table in results as! [[[String:AnyObject]]] {
                                                 for row in table {
                                                    for (_, value) in row {
                                                         if let intVal = value as? String {
                                                   self.formHTML.append(String(intVal))
                                                         }
                                                     }
                                                 }
                                             }
                                       DispatchQueue.main.async {
                                self.webVİew.loadHTMLString(self.formHTML, baseURL: nil)
                                       }
                                             client.disconnect()
                                         })
                                     } } }